@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | use EnableDIAnnotations; |
16 | 16 | |
17 | 17 | protected $settings = array( |
18 | - 'origin' => '*', // Wide Open! |
|
18 | + 'origin' => '*', // Wide Open! |
|
19 | 19 | 'allowMethods' => 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS', |
20 | 20 | ); |
21 | 21 | protected function setOrigin(Request $req, Response $rsp) { |
@@ -66,7 +66,7 @@ discard block |
||
66 | 66 | } |
67 | 67 | } |
68 | 68 | else { // Otherwise, use request headers |
69 | - $allowHeaders = $req->headers->get("Access-Control-Request-Headers", null ,false); |
|
69 | + $allowHeaders = $req->headers->get("Access-Control-Request-Headers", null, false); |
|
70 | 70 | } |
71 | 71 | if (isset($allowHeaders)) { |
72 | 72 | $rsp->headers->set('Access-Control-Allow-Headers', $allowHeaders); |
@@ -99,9 +99,9 @@ discard block |
||
99 | 99 | $response = new Response("", 200); |
100 | 100 | } |
101 | 101 | else { |
102 | - try{ |
|
102 | + try { |
|
103 | 103 | $response = $next($request); |
104 | - }catch(\Exception $e){ |
|
104 | + }catch (\Exception $e) { |
|
105 | 105 | $response = $this->exceptionRenderer->render($e); |
106 | 106 | } |
107 | 107 | } |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | */ |
28 | 28 | public function eraseMapping($target) |
29 | 29 | { |
30 | - if(!isset($this->mappings[$target])){ |
|
30 | + if (!isset($this->mappings[$target])) { |
|
31 | 31 | return null; |
32 | 32 | } |
33 | 33 | $ori = $this->mappings[$target]; |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | */ |
42 | 42 | public function getMapping($target) |
43 | 43 | { |
44 | - if(!array_key_exists($target, $this->mappings)){ |
|
44 | + if (!array_key_exists($target, $this->mappings)) { |
|
45 | 45 | return null; |
46 | 46 | } |
47 | 47 | return $this->mappings[$target]; |
@@ -60,15 +60,15 @@ discard block |
||
60 | 60 | 'params'=>$params |
61 | 61 | ]; |
62 | 62 | |
63 | - if($return instanceof Response){ //直接返回Response时, 对return不再做映射 |
|
63 | + if ($return instanceof Response) { //直接返回Response时, 对return不再做映射 |
|
64 | 64 | return $return; |
65 | 65 | } |
66 | 66 | $mappings = $this->getMappings(); |
67 | 67 | |
68 | 68 | $output = []; |
69 | - foreach($mappings as $key=>$map){ |
|
69 | + foreach ($mappings as $key=>$map) { |
|
70 | 70 | $val = \JmesPath\search($map->source, $input); |
71 | - if(substr($key, 0, strlen('response.')) == 'response.'){ |
|
71 | + if (substr($key, 0, strlen('response.')) == 'response.') { |
|
72 | 72 | $key = substr($key, strlen('response.')); |
73 | 73 | } |
74 | 74 | ArrayHelper::set($output, $key, $val); |
@@ -15,19 +15,19 @@ |
||
15 | 15 | { |
16 | 16 | $response = new Response(); |
17 | 17 | $response->headers->set('Content-Type', 'application/json'); |
18 | - foreach ($output as $key=>$value){ |
|
18 | + foreach ($output as $key=>$value) { |
|
19 | 19 | //TODO 支持自定义格式输出 |
20 | 20 | //TODO 支持更多的输出目标 |
21 | - if($key == 'content'){ |
|
22 | - if(is_array($value) || is_object($value)){ |
|
21 | + if ($key == 'content') { |
|
22 | + if (is_array($value) || is_object($value)) { |
|
23 | 23 | $value = json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
24 | 24 | } |
25 | 25 | $response->setContent($value); |
26 | - }elseif($key == 'headers'){ |
|
27 | - foreach ($value as $k=>$v){ |
|
26 | + }elseif ($key == 'headers') { |
|
27 | + foreach ($value as $k=>$v) { |
|
28 | 28 | $response->headers->set($k, $v); |
29 | 29 | } |
30 | - }else{ |
|
30 | + }else { |
|
31 | 31 | \PhpBoot\abort(new \UnexpectedValueException("Unexpected output target $key")); |
32 | 32 | } |
33 | 33 |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | |
20 | 20 | class ControllerContainerBuilder extends ContainerBuilder |
21 | 21 | { |
22 | - static $DEFAULT_ANNOTATIONS=[ |
|
22 | + static $DEFAULT_ANNOTATIONS = [ |
|
23 | 23 | [ClassAnnotationHandler::class, 'class'], |
24 | 24 | [PathAnnotationHandler::class, "class.children[?name=='path']"], |
25 | 25 | [RouteAnnotationHandler::class, "methods.*.children[?name=='route'][]"], |
@@ -43,9 +43,9 @@ discard block |
||
43 | 43 | Cache $cache, |
44 | 44 | array $annotations = null) |
45 | 45 | { |
46 | - if($annotations){ |
|
46 | + if ($annotations) { |
|
47 | 47 | parent::__construct($annotations, $cache); |
48 | - }else{ |
|
48 | + }else { |
|
49 | 49 | parent::__construct(self::$DEFAULT_ANNOTATIONS, $cache); |
50 | 50 | } |
51 | 51 |
@@ -14,11 +14,11 @@ |
||
14 | 14 | */ |
15 | 15 | public function render(\Exception $e) |
16 | 16 | { |
17 | - if($e instanceof HttpException){ |
|
17 | + if ($e instanceof HttpException) { |
|
18 | 18 | return new Response($e->getMessage(), $e->getStatusCode()); |
19 | - } if($e instanceof \InvalidArgumentException){ |
|
19 | + } if ($e instanceof \InvalidArgumentException) { |
|
20 | 20 | return new Response($e->getMessage(), Response::HTTP_BAD_REQUEST); |
21 | - }else{ |
|
21 | + }else { |
|
22 | 22 | return new Response($e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR); |
23 | 23 | } |
24 | 24 | } |
@@ -50,10 +50,10 @@ |
||
50 | 50 | * @param callable $call |
51 | 51 | * @return \Symfony\Component\HttpFoundation\Response |
52 | 52 | */ |
53 | - public function handler(Application $app, callable $call){ |
|
54 | - try{ |
|
53 | + public function handler(Application $app, callable $call) { |
|
54 | + try { |
|
55 | 55 | return $call(); |
56 | - }catch (\Exception $e){ |
|
56 | + }catch (\Exception $e) { |
|
57 | 57 | $renderer = $app->get(ExceptionRenderer::class); //TODO 放在这里是否合适 |
58 | 58 | return $renderer->render($e); |
59 | 59 | } |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | /** |
16 | 16 | * @param ParamMeta[] $paramMates |
17 | 17 | */ |
18 | - public function __construct(array $paramMates=[]){ |
|
18 | + public function __construct(array $paramMates = []) { |
|
19 | 19 | $this->paramMetas = $paramMates; |
20 | 20 | } |
21 | 21 | |
@@ -26,32 +26,32 @@ discard block |
||
26 | 26 | * @param array $reference |
27 | 27 | * @return void |
28 | 28 | */ |
29 | - public function handle(Application $app, Request $request, array &$params, array &$reference){ |
|
29 | + public function handle(Application $app, Request $request, array &$params, array &$reference) { |
|
30 | 30 | |
31 | 31 | $vld = new Validator(); |
32 | 32 | $req = ['request'=>$request]; |
33 | 33 | $requestArray = new ArrayAdaptor($req); |
34 | 34 | $inputs = []; |
35 | - foreach ($this->paramMetas as $k=>$meta){ |
|
36 | - if($meta->isPassedByReference){ |
|
35 | + foreach ($this->paramMetas as $k=>$meta) { |
|
36 | + if ($meta->isPassedByReference) { |
|
37 | 37 | // param PassedByReference is used to output |
38 | 38 | continue; |
39 | 39 | } |
40 | 40 | $source = \JmesPath\search($meta->source, $requestArray); |
41 | - if ($source !== null){ |
|
41 | + if ($source !== null) { |
|
42 | 42 | $source = ArrayAdaptor::strip($source); |
43 | - if($source instanceof ParameterBag){ |
|
43 | + if ($source instanceof ParameterBag) { |
|
44 | 44 | $source = $source->all(); |
45 | 45 | } |
46 | - if($meta->container){ |
|
46 | + if ($meta->container) { |
|
47 | 47 | $inputs[$meta->name] = $meta->container->make($source); |
48 | - }else{ |
|
48 | + }else { |
|
49 | 49 | $inputs[$meta->name] = $source; |
50 | 50 | } |
51 | - if($meta->validation){ |
|
51 | + if ($meta->validation) { |
|
52 | 52 | $vld->rule($meta->validation, $meta->name); |
53 | 53 | } |
54 | - }else{ |
|
54 | + }else { |
|
55 | 55 | $meta->isOptional or \PhpBoot\abort(new BadRequestHttpException("param $source is required")); |
56 | 56 | $inputs[$meta->name] = $meta->default; |
57 | 57 | } |
@@ -66,10 +66,10 @@ discard block |
||
66 | 66 | ); |
67 | 67 | |
68 | 68 | $pos = 0; |
69 | - foreach ($this->paramMetas as $meta){ |
|
70 | - if($meta->isPassedByReference){ |
|
69 | + foreach ($this->paramMetas as $meta) { |
|
70 | + if ($meta->isPassedByReference) { |
|
71 | 71 | $params[$pos] = &$reference[$meta->name]; |
72 | - }else{ |
|
72 | + }else { |
|
73 | 73 | $params[$pos] = $inputs[$meta->name]; |
74 | 74 | } |
75 | 75 | $pos++; |
@@ -77,15 +77,15 @@ discard block |
||
77 | 77 | } |
78 | 78 | } |
79 | 79 | |
80 | - public function getParamNames(){ |
|
81 | - return array_map(function($meta){return $meta->name;}, $this->paramMetas); |
|
80 | + public function getParamNames() { |
|
81 | + return array_map(function($meta) {return $meta->name; }, $this->paramMetas); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | /** |
85 | 85 | * 获取参数列表 |
86 | 86 | * @return ParamMeta[] |
87 | 87 | */ |
88 | - public function getParamMetas(){ |
|
88 | + public function getParamMetas() { |
|
89 | 89 | return $this->paramMetas; |
90 | 90 | } |
91 | 91 | |
@@ -94,9 +94,9 @@ discard block |
||
94 | 94 | * @param $name |
95 | 95 | * @return ParamMeta|null |
96 | 96 | */ |
97 | - public function getParamMeta($name){ |
|
98 | - foreach ($this->paramMetas as $meta){ |
|
99 | - if($meta->name == $name){ |
|
97 | + public function getParamMeta($name) { |
|
98 | + foreach ($this->paramMetas as $meta) { |
|
99 | + if ($meta->name == $name) { |
|
100 | 100 | return $meta; |
101 | 101 | } |
102 | 102 | } |
@@ -21,17 +21,17 @@ discard block |
||
21 | 21 | $paramType = null; |
22 | 22 | $paramName = null; |
23 | 23 | $paramDoc = ''; |
24 | - if(substr($text, 0, 1) == '$'){ //带$前缀的是变量 |
|
24 | + if (substr($text, 0, 1) == '$') { //带$前缀的是变量 |
|
25 | 25 | $params = new AnnotationParams($text, 2); |
26 | 26 | $paramName = substr($params->getParam(0), 1); |
27 | 27 | $paramDoc = $params->getRawParam(1, ''); |
28 | - }else{ |
|
28 | + }else { |
|
29 | 29 | $params = new AnnotationParams($text, 3); |
30 | - if ($params->count() >=2 && substr($params->getParam(1), 0, 1) == '$'){ |
|
30 | + if ($params->count()>=2 && substr($params->getParam(1), 0, 1) == '$') { |
|
31 | 31 | $paramType = $params->getParam(0); //TODO 检测类型是否合法 |
32 | 32 | $paramName = substr($params->getParam(1), 1); |
33 | 33 | $paramDoc = $params->getRawParam(2, ''); |
34 | - }else{ |
|
34 | + }else { |
|
35 | 35 | \PhpBoot\abort(new AnnotationSyntaxException("@param $text syntax error")); |
36 | 36 | } |
37 | 37 | } |
@@ -44,15 +44,15 @@ discard block |
||
44 | 44 | */ |
45 | 45 | public function __invoke(ControllerContainer $container, $ann, EntityContainerBuilder $entityBuilder) |
46 | 46 | { |
47 | - if(!$ann->parent){ |
|
47 | + if (!$ann->parent) { |
|
48 | 48 | //Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()} should be used with parent route"); |
49 | 49 | return; |
50 | 50 | } |
51 | 51 | $target = $ann->parent->name; |
52 | 52 | $route = $container->getRoute($target); |
53 | - if(!$route){ |
|
53 | + if (!$route) { |
|
54 | 54 | //Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()}::$target should be used with parent route"); |
55 | - return ; |
|
55 | + return; |
|
56 | 56 | } |
57 | 57 | $className = $container->getClassName(); |
58 | 58 | |
@@ -61,18 +61,18 @@ discard block |
||
61 | 61 | $paramMeta = $route->getRequestHandler()->getParamMeta($paramName); |
62 | 62 | $paramMeta or \PhpBoot\abort(new AnnotationSyntaxException("$className::$target param $paramName not exist ")); |
63 | 63 | //TODO 检测声明的类型和注释的类型是否匹配 |
64 | - if($paramType){ |
|
65 | - $paramMeta->type = TypeHint::normalize($paramType, $className);//or \PhpBoot\abort(new AnnotationSyntaxException("{$container->getClassName()}::{$ann->parent->name} @{$ann->name} syntax error, param $paramName unknown type:$paramType ")); |
|
64 | + if ($paramType) { |
|
65 | + $paramMeta->type = TypeHint::normalize($paramType, $className); //or \PhpBoot\abort(new AnnotationSyntaxException("{$container->getClassName()}::{$ann->parent->name} @{$ann->name} syntax error, param $paramName unknown type:$paramType ")); |
|
66 | 66 | $container = ContainerFactory::create($entityBuilder, $paramMeta->type); |
67 | 67 | $paramMeta->container = $container; |
68 | 68 | } |
69 | 69 | $paramMeta->description = $paramDoc; |
70 | 70 | |
71 | 71 | $responseHandler = $route->getResponseHandler(); |
72 | - if($paramMeta->isPassedByReference && $responseHandler){ |
|
72 | + if ($paramMeta->isPassedByReference && $responseHandler) { |
|
73 | 73 | $mappings = $responseHandler->getMappings(); |
74 | - foreach ($mappings as $k => $v){ |
|
75 | - if($v->source == 'params.'.$paramMeta->name){ |
|
74 | + foreach ($mappings as $k => $v) { |
|
75 | + if ($v->source == 'params.'.$paramMeta->name) { |
|
76 | 76 | $v->description = $paramMeta->description; |
77 | 77 | $v->type = $paramMeta->type; |
78 | 78 | $v->container = $paramMeta->container; |
@@ -21,20 +21,20 @@ discard block |
||
21 | 21 | */ |
22 | 22 | public function __invoke(ControllerContainer $container, $ann, EntityContainerBuilder $entityBuilder) |
23 | 23 | { |
24 | - if(!$ann->parent){ |
|
24 | + if (!$ann->parent) { |
|
25 | 25 | //Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()} should be used with parent route"); |
26 | 26 | return; |
27 | 27 | } |
28 | 28 | $target = $ann->parent->name; |
29 | 29 | $route = $container->getRoute($target); |
30 | - if(!$route){ |
|
30 | + if (!$route) { |
|
31 | 31 | //Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()}::$target should be used with parent route"); |
32 | - return ; |
|
32 | + return; |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | $params = new AnnotationParams($ann->description, 2); |
36 | 36 | $type = $doc = null; |
37 | - if(count($params)>0){ |
|
37 | + if (count($params)>0) { |
|
38 | 38 | $type = TypeHint::normalize($params[0], $container->getClassName()); |
39 | 39 | } |
40 | 40 | $doc = $params->getRawParam(1, ''); |
@@ -43,10 +43,10 @@ discard block |
||
43 | 43 | $meta = $route |
44 | 44 | ->getResponseHandler() |
45 | 45 | ->getMapping('response.content'); |
46 | - if($meta){ |
|
46 | + if ($meta) { |
|
47 | 47 | $meta->description = $doc; |
48 | 48 | $meta->type = $type; |
49 | - $meta->container = $type == 'void'?null:ContainerFactory::create($entityBuilder, $type); |
|
49 | + $meta->container = $type == 'void' ? null : ContainerFactory::create($entityBuilder, $type); |
|
50 | 50 | } |
51 | 51 | } |
52 | 52 | } |
53 | 53 | \ No newline at end of file |