WebmanMiddleware.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace hg\apidoc\middleware;
  3. use hg\apidoc\providers\BaseService;
  4. use hg\apidoc\utils\ConfigProvider;
  5. use support\Db;
  6. use Webman\MiddlewareInterface;
  7. use Webman\Http\Response;
  8. use Webman\Http\Request;
  9. class WebmanMiddleware implements MiddlewareInterface
  10. {
  11. use BaseService;
  12. public function process(Request $request, callable $handler) : Response
  13. {
  14. $this->initConfig();
  15. $params = $request->all();
  16. $config = ConfigProvider::get();
  17. $config['request_params'] = $params;
  18. ConfigProvider::set($config);
  19. $response = $request->method() == 'OPTIONS' ? response('') : $handler($request);
  20. if (!empty($config['allowCrossDomain'])){
  21. // 给响应添加跨域相关的http头
  22. $response->withHeaders([
  23. 'Access-Control-Allow-Credentials' => 'true',
  24. 'Access-Control-Allow-Origin' => $request->header('origin', '*'),
  25. 'Access-Control-Allow-Methods' => $request->header('access-control-request-method', '*'),
  26. 'Access-Control-Allow-Headers' => $request->header('access-control-request-headers', '*'),
  27. ]);
  28. }
  29. return $response;
  30. }
  31. static function getApidocConfig()
  32. {
  33. $config = config('plugin.hg.apidoc.app.apidoc');
  34. if (!(!empty($config['auto_url']) && !empty($config['auto_url']['filter_keys']))){
  35. $config['auto_url']['filter_keys'] = ['app','controller'];
  36. }
  37. $config['app_frame'] = "webman";
  38. return $config;
  39. }
  40. static function registerRoute($route)
  41. {
  42. return "";
  43. }
  44. static function databaseQuery($sql)
  45. {
  46. return Db::select($sql);
  47. }
  48. static function getRootPath()
  49. {
  50. return BASE_PATH."/";
  51. }
  52. static function getRuntimePath()
  53. {
  54. return BASE_PATH."/runtime/";
  55. }
  56. static function setLang($locale)
  57. {
  58. locale($locale);
  59. }
  60. static function getLang($lang): string
  61. {
  62. return $lang;
  63. }
  64. static function handleResponseJson($res)
  65. {
  66. return json($res);
  67. }
  68. static function getTablePrefix(){
  69. $driver = config('database.default');
  70. $table_prefix=config('database.connections.'.$driver.'.prefix');
  71. return $table_prefix;
  72. }
  73. }