@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | ControllerContainerBuilder $builder, |
34 | 34 | Client $http, |
35 | 35 | $interface, |
36 | - $prefix='/') |
|
36 | + $prefix = '/') |
|
37 | 37 | { |
38 | 38 | $this->container = $builder->build($interface); |
39 | 39 | $this->http = $http; |
@@ -48,11 +48,11 @@ discard block |
||
48 | 48 | |
49 | 49 | $request = $this->createRequest($method, $route, $args); |
50 | 50 | |
51 | - if(MultiRpc::isRunning()){ |
|
51 | + if (MultiRpc::isRunning()) { |
|
52 | 52 | $op = $this->http->sendAsync($request); |
53 | 53 | $res = MultiRpc::wait($op); |
54 | 54 | return $this->mapResponse($method, $route, $res, $args); |
55 | - }else{ |
|
55 | + }else { |
|
56 | 56 | $res = $this->http->send($request); |
57 | 57 | return $this->mapResponse($method, $route, $res, $args); |
58 | 58 | } |
@@ -69,25 +69,25 @@ discard block |
||
69 | 69 | $params = $route->getRequestHandler()->getParamMetas(); |
70 | 70 | //TODO 支持 query、content、path以外的其他参数, 如cookie,path等 |
71 | 71 | $request = []; |
72 | - foreach ($params as $pos=>$param){ |
|
73 | - if(!array_key_exists($pos, $args) && $param->isOptional){ |
|
72 | + foreach ($params as $pos=>$param) { |
|
73 | + if (!array_key_exists($pos, $args) && $param->isOptional) { |
|
74 | 74 | $args[$pos] = $param->default; |
75 | 75 | } |
76 | 76 | array_key_exists($pos, $args) or \PhpBoot\abort( |
77 | 77 | $this->container->getClassName()." $actionName missing param {$param->name}"); |
78 | 78 | |
79 | - if(!$param->isPassedByReference){ |
|
79 | + if (!$param->isPassedByReference) { |
|
80 | 80 | ArrayHelper::set($request, $param->source, $args[$pos]); |
81 | 81 | } |
82 | 82 | } |
83 | 83 | |
84 | - if(isset($request['request'])){ |
|
84 | + if (isset($request['request'])) { |
|
85 | 85 | $request = $request['request']; |
86 | 86 | } |
87 | 87 | $uri = $route->getUri(); |
88 | - foreach($route->getPathParams() as $path){ |
|
89 | - if(isset($request[$path])){ |
|
90 | - $uri = str_replace('{'.$path.'}', urlencode($request[$path]) , $uri); |
|
88 | + foreach ($route->getPathParams() as $path) { |
|
89 | + if (isset($request[$path])) { |
|
90 | + $uri = str_replace('{'.$path.'}', urlencode($request[$path]), $uri); |
|
91 | 91 | unset($request[$path]); |
92 | 92 | } |
93 | 93 | } |
@@ -97,53 +97,53 @@ discard block |
||
97 | 97 | $body = null; |
98 | 98 | $headers = []; |
99 | 99 | |
100 | - if(isset($request['query'])){ |
|
100 | + if (isset($request['query'])) { |
|
101 | 101 | $query += $request['query']; |
102 | 102 | } |
103 | 103 | unset($request['query']); |
104 | 104 | |
105 | - if(isset($request['headers'])){ |
|
105 | + if (isset($request['headers'])) { |
|
106 | 106 | $headers += $request['headers']; |
107 | 107 | } |
108 | 108 | unset($request['headers']); |
109 | 109 | |
110 | - if(isset($request['cookies'])){ |
|
110 | + if (isset($request['cookies'])) { |
|
111 | 111 | $cookies = []; |
112 | - foreach ($request['cookies'] as $k=>$v){ |
|
112 | + foreach ($request['cookies'] as $k=>$v) { |
|
113 | 113 | $cookies[] = "$k=$v"; |
114 | 114 | } |
115 | 115 | $headers['Cookie'] = implode('; ', $cookies); |
116 | 116 | } |
117 | 117 | unset($request['cookies']); |
118 | 118 | |
119 | - if(isset($request['request'])){ |
|
120 | - if($body === null){ |
|
119 | + if (isset($request['request'])) { |
|
120 | + if ($body === null) { |
|
121 | 121 | $body = []; |
122 | 122 | } |
123 | 123 | $body += $request['request']; |
124 | 124 | } |
125 | 125 | unset($request['request']); |
126 | 126 | |
127 | - if(isset($request['files'])){ |
|
127 | + if (isset($request['files'])) { |
|
128 | 128 | \PhpBoot\abort(new \UnexpectedValueException("sending request with files is not support")); |
129 | 129 | } |
130 | - if(in_array($httpMethod, ['GET', 'OPTION'])){ |
|
131 | - foreach ($request as $k => $v){ |
|
132 | - if(!in_array($k, ['query', 'request', 'files', 'cookies', 'headers'])){ |
|
130 | + if (in_array($httpMethod, ['GET', 'OPTION'])) { |
|
131 | + foreach ($request as $k => $v) { |
|
132 | + if (!in_array($k, ['query', 'request', 'files', 'cookies', 'headers'])) { |
|
133 | 133 | $query[$k] = $v; |
134 | 134 | } |
135 | 135 | } |
136 | - }else{ |
|
137 | - foreach ($request as $k => $v){ |
|
138 | - if(!in_array($k, ['query', 'request', 'files', 'cookies', 'headers'])){ |
|
139 | - if($body === null){ |
|
136 | + }else { |
|
137 | + foreach ($request as $k => $v) { |
|
138 | + if (!in_array($k, ['query', 'request', 'files', 'cookies', 'headers'])) { |
|
139 | + if ($body === null) { |
|
140 | 140 | $body = []; |
141 | 141 | } |
142 | 142 | $body[$k] = $v; |
143 | 143 | } |
144 | 144 | } |
145 | 145 | } |
146 | - if($body !== null){ |
|
146 | + if ($body !== null) { |
|
147 | 147 | $body = json_encode($body, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
148 | 148 | } |
149 | 149 | $uri = $this->uriPrefix.ltrim($uri, '/').'?'.http_build_query($query); |
@@ -156,7 +156,7 @@ discard block |
||
156 | 156 | } |
157 | 157 | |
158 | 158 | |
159 | - public function mapResponse($actionName, Route $route, ResponseInterface $response, $requestArg=[]) |
|
159 | + public function mapResponse($actionName, Route $route, ResponseInterface $response, $requestArg = []) |
|
160 | 160 | { |
161 | 161 | $response = \Symfony\Component\HttpFoundation\Response::create( |
162 | 162 | (string)$response->getBody(), |
@@ -165,9 +165,9 @@ discard block |
||
165 | 165 | ); |
166 | 166 | $namedArgs = []; |
167 | 167 | |
168 | - foreach ($route->getRequestHandler()->getParamMetas() as $pos=>$param){ |
|
168 | + foreach ($route->getRequestHandler()->getParamMetas() as $pos=>$param) { |
|
169 | 169 | |
170 | - if($param->isPassedByReference){ |
|
170 | + if ($param->isPassedByReference) { |
|
171 | 171 | $namedArgs[$param->name] = &$requestArg[$pos]; |
172 | 172 | } |
173 | 173 | } |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | //TODO 远端接口没有抛出异常,但设置了 status( status 不是200)时如何处理 |
179 | 179 | $handler = $route->getResponseHandler(); |
180 | 180 | |
181 | - if($response->getStatusCode() >= 200 && $response->getStatusCode() <300){ |
|
181 | + if ($response->getStatusCode()>=200 && $response->getStatusCode()<300) { |
|
182 | 182 | |
183 | 183 | |
184 | 184 | $returns = $handler->getMappings(); |
@@ -195,30 +195,30 @@ discard block |
||
195 | 195 | 'params'=>$namedArgs |
196 | 196 | ]; |
197 | 197 | |
198 | - foreach ($returns as $map=>$return){ |
|
198 | + foreach ($returns as $map=>$return) { |
|
199 | 199 | |
200 | 200 | $data = \JmesPath\search($map, $buffer); |
201 | - if(!$return->container){ |
|
201 | + if (!$return->container) { |
|
202 | 202 | continue; |
203 | 203 | } |
204 | 204 | $data = $return->container->make($data, false); |
205 | 205 | ArrayHelper::set($mapping, $return->source, $data); |
206 | 206 | } |
207 | 207 | |
208 | - }else{ |
|
208 | + }else { |
|
209 | 209 | |
210 | 210 | //TODO 如果多个 异常对应同一个 statusCode 怎么处理 |
211 | 211 | $exceptions = $route->getExceptionHandler()->getExceptions(); |
212 | 212 | |
213 | 213 | $errName = null; |
214 | - foreach ($exceptions as $err){ |
|
214 | + foreach ($exceptions as $err) { |
|
215 | 215 | |
216 | 216 | $renderer = $this->app->get(ExceptionRenderer::class); |
217 | 217 | $exec = $renderer->render( |
218 | 218 | $this->app->make($err, ['message'=>(string)$response->getContent()]) |
219 | 219 | ); |
220 | 220 | |
221 | - if( $exec->getStatusCode() == $response->getStatusCode()){ |
|
221 | + if ($exec->getStatusCode() == $response->getStatusCode()) { |
|
222 | 222 | throw $exec; |
223 | 223 | } |
224 | 224 | } |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | |
227 | 227 | }; |
228 | 228 | |
229 | - if(isset($mapping['return'])){ |
|
229 | + if (isset($mapping['return'])) { |
|
230 | 230 | return $mapping['return']; |
231 | 231 | }; |
232 | 232 | } |
@@ -21,12 +21,12 @@ discard block |
||
21 | 21 | * ] |
22 | 22 | * |
23 | 23 | */ |
24 | - return MultiRequest::run($threads, function($promises){ |
|
24 | + return MultiRequest::run($threads, function($promises) { |
|
25 | 25 | $res = []; |
26 | - foreach (Promise\settle($promises)->wait() as $i){ |
|
27 | - if(isset($i['reason'])){ |
|
26 | + foreach (Promise\settle($promises)->wait() as $i) { |
|
27 | + if (isset($i['reason'])) { |
|
28 | 28 | $res[] = [null, new RpcException($i['reason'])]; |
29 | - }else{ |
|
29 | + }else { |
|
30 | 30 | $res[] = [$i['value'], null]; |
31 | 31 | } |
32 | 32 | } |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | }); |
35 | 35 | } |
36 | 36 | |
37 | - public static function isRunning(){ |
|
37 | + public static function isRunning() { |
|
38 | 38 | return MultiRequest::isRunning(); |
39 | 39 | } |
40 | 40 |
@@ -45,9 +45,9 @@ discard block |
||
45 | 45 | } |
46 | 46 | class AnnotationReader implements \ArrayAccess |
47 | 47 | { |
48 | - static public function createDocBlockFactory(){ |
|
48 | + static public function createDocBlockFactory() { |
|
49 | 49 | $fqsenResolver = new FqsenResolver(); |
50 | - $tagFactory = new StandardTagFactory($fqsenResolver,[]); |
|
50 | + $tagFactory = new StandardTagFactory($fqsenResolver, []); |
|
51 | 51 | $descriptionFactory = new DescriptionFactory($tagFactory); |
52 | 52 | $tagFactory->addService($descriptionFactory); |
53 | 53 | $tagFactory->addService(new TypeResolver($fqsenResolver)); |
@@ -72,22 +72,22 @@ discard block |
||
72 | 72 | self::assertAnnotationEnabled(); |
73 | 73 | $rfl = new \ReflectionClass($className) or \PhpBoot\abort("load class $className failed"); |
74 | 74 | $fileName = $rfl->getFileName(); |
75 | - $key = str_replace('\\','.',self::class).md5($fileName.$className); |
|
75 | + $key = str_replace('\\', '.', self::class).md5($fileName.$className); |
|
76 | 76 | $oldData = null; |
77 | - $cache = new CheckableCache($localCache?:new ApcCache()); |
|
77 | + $cache = new CheckableCache($localCache ?: new ApcCache()); |
|
78 | 78 | $res = $cache->get('ann:'.$key, null, $oldData, false); |
79 | - if($res === null){ |
|
80 | - try{ |
|
79 | + if ($res === null) { |
|
80 | + try { |
|
81 | 81 | $meta = self::readWithoutCache($className); |
82 | - $cache->set($key, $meta, 0, $fileName?new ClassModifiedChecker($className):null); |
|
82 | + $cache->set($key, $meta, 0, $fileName ? new ClassModifiedChecker($className) : null); |
|
83 | 83 | return $meta; |
84 | - }catch (\Exception $e){ |
|
85 | - $cache->set($key, $e->getMessage(), 0, $fileName?new ClassModifiedChecker($className):null); |
|
84 | + }catch (\Exception $e) { |
|
85 | + $cache->set($key, $e->getMessage(), 0, $fileName ? new ClassModifiedChecker($className) : null); |
|
86 | 86 | throw $e; |
87 | 87 | } |
88 | - }elseif(is_string($res)){ |
|
88 | + }elseif (is_string($res)) { |
|
89 | 89 | \PhpBoot\abort($res); |
90 | - }else{ |
|
90 | + }else { |
|
91 | 91 | return $res; |
92 | 92 | } |
93 | 93 | } |
@@ -104,19 +104,19 @@ discard block |
||
104 | 104 | $reader->class->name = $className; |
105 | 105 | |
106 | 106 | //method annotations |
107 | - foreach ($rfl->getMethods() as $i){ |
|
107 | + foreach ($rfl->getMethods() as $i) { |
|
108 | 108 | $block = self::readAnnotationBlock($i->getDocComment()); |
109 | 109 | $block->name = $i->getName(); |
110 | - $reader->methods[$i->getName()]=$block; |
|
110 | + $reader->methods[$i->getName()] = $block; |
|
111 | 111 | } |
112 | 112 | //property annotations |
113 | - foreach ($rfl->getProperties() as $i){ |
|
113 | + foreach ($rfl->getProperties() as $i) { |
|
114 | 114 | if ($i->isStatic()) { |
115 | 115 | continue; |
116 | 116 | } |
117 | 117 | $block = self::readAnnotationBlock($i->getDocComment()); |
118 | 118 | $block->name = $i->getName(); |
119 | - $reader->properties[$i->getName()]=$block; |
|
119 | + $reader->properties[$i->getName()] = $block; |
|
120 | 120 | } |
121 | 121 | while ($rfl = $rfl->getParentClass()) { |
122 | 122 | foreach ($rfl->getProperties(\ReflectionProperty::IS_PRIVATE) as $i) { |
@@ -125,7 +125,7 @@ discard block |
||
125 | 125 | } |
126 | 126 | $block = self::readAnnotationBlock($i->getDocComment()); |
127 | 127 | $block->name = $i->getName(); |
128 | - $reader->properties[$i->getName()]=$block; |
|
128 | + $reader->properties[$i->getName()] = $block; |
|
129 | 129 | } |
130 | 130 | } |
131 | 131 | return $reader; |
@@ -134,7 +134,7 @@ discard block |
||
134 | 134 | static private function readAnnotationBlock($doc) |
135 | 135 | { |
136 | 136 | $annBlock = new AnnotationBlock(); |
137 | - if(empty($doc)){ |
|
137 | + if (empty($doc)) { |
|
138 | 138 | return $annBlock; |
139 | 139 | } |
140 | 140 | $docFactory = AnnotationReader::createDocBlockFactory(); |
@@ -149,8 +149,8 @@ discard block |
||
149 | 149 | $annTag->parent = $annBlock; |
150 | 150 | $annTag->description = strval($desc); |
151 | 151 | $annTag->name = $tag->getName(); |
152 | - $annTag->children=[]; |
|
153 | - if($desc){ |
|
152 | + $annTag->children = []; |
|
153 | + if ($desc) { |
|
154 | 154 | $output = new AnnotationTagsOutput(); |
155 | 155 | $desc->render($output); |
156 | 156 | foreach ($output->tags as $child) { |
@@ -174,11 +174,11 @@ discard block |
||
174 | 174 | * @var AnnotationBlock[] |
175 | 175 | */ |
176 | 176 | |
177 | - public $methods=[]; |
|
177 | + public $methods = []; |
|
178 | 178 | /** |
179 | 179 | * @var AnnotationBlock[] |
180 | 180 | */ |
181 | - public $properties=[]; |
|
181 | + public $properties = []; |
|
182 | 182 | |
183 | 183 | public function offsetExists($offset) |
184 | 184 | { |
@@ -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'){ |
|
21 | + if ($key == 'content') { |
|
22 | 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 |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | use PhpBoot\ORM\ModelWithObject; |
6 | 6 | use PhpBoot\Utils\Logger; |
7 | 7 | |
8 | -if (! function_exists('PhpBoot\abort')) { |
|
8 | +if (!function_exists('PhpBoot\abort')) { |
|
9 | 9 | /** |
10 | 10 | * 抛出异常, 并记录日志 |
11 | 11 | * @param string|\Exception $error |
@@ -13,29 +13,29 @@ discard block |
||
13 | 13 | * @param string $level "error"|"warning"|"info"|"debug"|null |
14 | 14 | * @throws \Exception |
15 | 15 | */ |
16 | - function abort($error = '', $context=[], $level='warning') |
|
16 | + function abort($error = '', $context = [], $level = 'warning') |
|
17 | 17 | { |
18 | - if(is_object($context)){ |
|
18 | + if (is_object($context)) { |
|
19 | 19 | $context = get_object_vars($context); |
20 | 20 | } |
21 | - if($error instanceof \Exception){ |
|
21 | + if ($error instanceof \Exception) { |
|
22 | 22 | $e = $error; |
23 | 23 | $message = "exception '".get_class($error)."' with message {$error->getMessage()}"; |
24 | - }else{ |
|
24 | + }else { |
|
25 | 25 | $e = new \RuntimeException($error); |
26 | 26 | $message = $error; |
27 | 27 | } |
28 | 28 | $trace = $e->getTrace(); |
29 | 29 | |
30 | - if($e->getFile() == __FILE__){ |
|
30 | + if ($e->getFile() == __FILE__) { |
|
31 | 31 | $file = $trace[0]['file']; |
32 | 32 | $line = $trace[0]['line']; |
33 | - }else{ |
|
33 | + }else { |
|
34 | 34 | $file = $e->getFile(); |
35 | 35 | $line = $e->getLine(); |
36 | 36 | } |
37 | - if($level){ |
|
38 | - Logger::$level($message, $context +['@file'=>$file, '@line'=>$line]); |
|
37 | + if ($level) { |
|
38 | + Logger::$level($message, $context + ['@file'=>$file, '@line'=>$line]); |
|
39 | 39 | } |
40 | 40 | throw $e; |
41 | 41 | } |
@@ -61,9 +61,9 @@ discard block |
||
61 | 61 | */ |
62 | 62 | function models(DB $db, $entity) |
63 | 63 | { |
64 | - if(is_object($entity)){ |
|
64 | + if (is_object($entity)) { |
|
65 | 65 | return $db->getApp()->make(ModelWithObject::class, ['db'=>$db, 'entity'=>$entity]); |
66 | - }else{ |
|
66 | + }else { |
|
67 | 67 | return $db->getApp()->make(ModelWithClass::class, ['db'=>$db, 'entityName'=>$entity]); |
68 | 68 | } |
69 | 69 | } |
@@ -28,11 +28,11 @@ discard block |
||
28 | 28 | public function create() |
29 | 29 | { |
30 | 30 | $data = []; |
31 | - foreach ($this->getColumns() as $column){ |
|
32 | - if(isset($this->object->$column)){ |
|
33 | - if(is_array($this->object->$column) || is_object($this->object->$column)){ |
|
31 | + foreach ($this->getColumns() as $column) { |
|
32 | + if (isset($this->object->$column)) { |
|
33 | + if (is_array($this->object->$column) || is_object($this->object->$column)) { |
|
34 | 34 | $data[$column] = json_encode($this->object->$column); |
35 | - }else{ |
|
35 | + }else { |
|
36 | 36 | $data[$column] = $this->object->$column; |
37 | 37 | } |
38 | 38 | |
@@ -48,18 +48,18 @@ discard block |
||
48 | 48 | * @param array $columns columns to update. if columns is empty array, update all of the columns |
49 | 49 | * @return int rows updated |
50 | 50 | */ |
51 | - public function update(array $columns=[]) |
|
51 | + public function update(array $columns = []) |
|
52 | 52 | { |
53 | 53 | $data = []; |
54 | 54 | $pk = $this->entity->getPK(); |
55 | - foreach ($this->getColumns() as $column){ |
|
56 | - if(count($columns) && !in_array($column, $columns)){ |
|
55 | + foreach ($this->getColumns() as $column) { |
|
56 | + if (count($columns) && !in_array($column, $columns)) { |
|
57 | 57 | continue; |
58 | 58 | } |
59 | - if($pk != $column && isset($this->object->$column)){ |
|
60 | - if(is_array($this->object->$column) || is_object($this->object->$column)){ |
|
59 | + if ($pk != $column && isset($this->object->$column)) { |
|
60 | + if (is_array($this->object->$column) || is_object($this->object->$column)) { |
|
61 | 61 | $data[$column] = json_encode($this->object->$column); |
62 | - }else{ |
|
62 | + }else { |
|
63 | 63 | $data[$column] = $this->object->$column; |
64 | 64 | } |
65 | 65 | } |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | protected function getColumns() |
86 | 86 | { |
87 | 87 | $columns = []; |
88 | - foreach ($this->entity->getProperties() as $p){ |
|
88 | + foreach ($this->entity->getProperties() as $p) { |
|
89 | 89 | $columns[] = $p->name; |
90 | 90 | } |
91 | 91 | return $columns; |
@@ -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]; |
@@ -53,12 +53,12 @@ discard block |
||
53 | 53 | */ |
54 | 54 | public function getMappingBySource($source) |
55 | 55 | { |
56 | - foreach ($this->mappings as $k=>$v){ |
|
57 | - if($v->source == $source){ |
|
56 | + foreach ($this->mappings as $k=>$v) { |
|
57 | + if ($v->source == $source) { |
|
58 | 58 | return [$k, $v]; |
59 | 59 | } |
60 | 60 | } |
61 | - return [null,null]; |
|
61 | + return [null, null]; |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | |
@@ -75,15 +75,15 @@ discard block |
||
75 | 75 | 'params'=>$params |
76 | 76 | ]; |
77 | 77 | |
78 | - if($return instanceof Response){ //直接返回Response时, 对return不再做映射 |
|
78 | + if ($return instanceof Response) { //直接返回Response时, 对return不再做映射 |
|
79 | 79 | return $return; |
80 | 80 | } |
81 | 81 | $mappings = $this->getMappings(); |
82 | 82 | |
83 | 83 | $output = []; |
84 | - foreach($mappings as $key=>$map){ |
|
84 | + foreach ($mappings as $key=>$map) { |
|
85 | 85 | $val = \JmesPath\search($map->source, $input); |
86 | - if(substr($key, 0, strlen('response.')) == 'response.'){ |
|
86 | + if (substr($key, 0, strlen('response.')) == 'response.') { |
|
87 | 87 | $key = substr($key, strlen('response.')); |
88 | 88 | } |
89 | 89 | ArrayHelper::set($output, $key, $val); |
@@ -21,15 +21,15 @@ discard block |
||
21 | 21 | */ |
22 | 22 | public function __invoke(ControllerContainer $container, $ann, EntityContainerBuilder $entityBuilder) |
23 | 23 | { |
24 | - if(!$ann->parent || !$ann->parent->parent){ |
|
24 | + if (!$ann->parent || !$ann->parent->parent) { |
|
25 | 25 | Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()} should be used with parent param/return"); |
26 | 26 | return; |
27 | 27 | } |
28 | 28 | $target = $ann->parent->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 param/return"); |
32 | - return ; |
|
32 | + return; |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | $params = new AnnotationParams($ann->description, 2); |
@@ -38,20 +38,20 @@ discard block |
||
38 | 38 | |
39 | 39 | $handler = $route->getResponseHandler(); |
40 | 40 | |
41 | - if ($ann->parent->name == 'return'){ |
|
41 | + if ($ann->parent->name == 'return') { |
|
42 | 42 | list($target, $return) = $handler->getMappingBySource('return'); |
43 | - if($return){ |
|
43 | + if ($return) { |
|
44 | 44 | $handler->eraseMapping($target); |
45 | 45 | $handler->setMapping($params[0], $return); |
46 | 46 | } |
47 | 47 | |
48 | - }elseif($ann->parent->name == 'param'){ |
|
48 | + }elseif ($ann->parent->name == 'param') { |
|
49 | 49 | list($paramType, $paramName, $paramDoc) = ParamAnnotationHandler::getParamInfo($ann->parent->description); |
50 | 50 | |
51 | 51 | $paramMeta = $route->getRequestHandler()->getParamMeta($paramName); |
52 | - if($paramMeta->isPassedByReference){ |
|
52 | + if ($paramMeta->isPassedByReference) { |
|
53 | 53 | list($target, $ori) = $handler->getMappingBySource('params.'.$paramName); |
54 | - if($ori){ |
|
54 | + if ($ori) { |
|
55 | 55 | $handler->eraseMapping($target); |
56 | 56 | } |
57 | 57 | //输出绑定 |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | ContainerFactory::create($entityBuilder, $paramMeta->type) |
64 | 64 | ) |
65 | 65 | ); |
66 | - }else{ |
|
66 | + }else { |
|
67 | 67 | $paramMeta->source = $params[0]; |
68 | 68 | } |
69 | 69 | } |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | 'DELETE' |
44 | 44 | ]) or \PhpBoot\abort(new AnnotationSyntaxException("unknown method http $httpMethod in {$container->getClassName()}::$target")); |
45 | 45 | //获取方法参数信息 |
46 | - $rfl = new \ReflectionClass($container->getClassName()); |
|
46 | + $rfl = new \ReflectionClass($container->getClassName()); |
|
47 | 47 | $method = $rfl->getMethod($target); |
48 | 48 | $methodParams = $method->getParameters(); |
49 | 49 | |
@@ -68,9 +68,9 @@ discard block |
||
68 | 68 | $routeParser = new Std(); |
69 | 69 | $uri = $params->getParam(1); |
70 | 70 | $info = $routeParser->parse($uri); //0.4和1.0返回值不同, 不兼容 |
71 | - if(isset($info[0])){ |
|
72 | - foreach ($info[0] as $i){ |
|
73 | - if(is_array($i)) { |
|
71 | + if (isset($info[0])) { |
|
72 | + foreach ($info[0] as $i) { |
|
73 | + if (is_array($i)) { |
|
74 | 74 | $route->addPathParam($i[0]); |
75 | 75 | } |
76 | 76 | } |
@@ -79,31 +79,31 @@ discard block |
||
79 | 79 | $hasRefParam = false; |
80 | 80 | //设置参数列表 |
81 | 81 | $paramsMeta = []; |
82 | - foreach ($methodParams as $param){ |
|
82 | + foreach ($methodParams as $param) { |
|
83 | 83 | $paramName = $param->getName(); |
84 | 84 | $source = "request.$paramName"; |
85 | - if($route->hasPathParam($paramName)){ //参数来自路由 |
|
85 | + if ($route->hasPathParam($paramName)) { //参数来自路由 |
|
86 | 86 | $source = "request.$paramName"; |
87 | - }elseif($httpMethod == 'GET'){ |
|
87 | + }elseif ($httpMethod == 'GET') { |
|
88 | 88 | $source = "request.$paramName"; //GET请求显示指定来自query string |
89 | 89 | } |
90 | 90 | $paramClass = $param->getClass(); |
91 | - if($paramClass){ |
|
91 | + if ($paramClass) { |
|
92 | 92 | $paramClass = $paramClass->getName(); |
93 | 93 | } |
94 | 94 | $entityContainer = ContainerFactory::create($entityBuilder, $paramClass); |
95 | 95 | $meta = new ParamMeta($paramName, |
96 | 96 | $source, |
97 | - $paramClass?:'mixed', |
|
97 | + $paramClass ?: 'mixed', |
|
98 | 98 | $param->isOptional(), |
99 | - $param->isOptional()?$param->getDefaultValue():null, |
|
99 | + $param->isOptional() ? $param->getDefaultValue() : null, |
|
100 | 100 | $param->isPassedByReference(), |
101 | 101 | null, |
102 | 102 | '', |
103 | 103 | $entityContainer |
104 | 104 | ); |
105 | 105 | $paramsMeta[] = $meta; |
106 | - if($meta->isPassedByReference){ |
|
106 | + if ($meta->isPassedByReference) { |
|
107 | 107 | $hasRefParam = true; |
108 | 108 | $responseHandler->setMapping('response.content.'.$meta->name, new ReturnMeta( |
109 | 109 | 'params.'.$meta->name, |
@@ -114,16 +114,16 @@ discard block |
||
114 | 114 | } |
115 | 115 | |
116 | 116 | $requestHandler->setParamMetas($paramsMeta); |
117 | - if(!$hasRefParam){ |
|
118 | - $responseHandler->setMapping('response.content', new ReturnMeta('return','mixed','', new MixedTypeContainer())); |
|
119 | - }else{ |
|
117 | + if (!$hasRefParam) { |
|
118 | + $responseHandler->setMapping('response.content', new ReturnMeta('return', 'mixed', '', new MixedTypeContainer())); |
|
119 | + }else { |
|
120 | 120 | //当存在引用参数作为输出时, 默认将 return 数据绑定的到 data 下, 以防止和引用参数作为输出重叠 |
121 | - $responseHandler->setMapping($this->returnTarget, new ReturnMeta('return','mixed','', new MixedTypeContainer())); |
|
121 | + $responseHandler->setMapping($this->returnTarget, new ReturnMeta('return', 'mixed', '', new MixedTypeContainer())); |
|
122 | 122 | } |
123 | 123 | |
124 | 124 | |
125 | 125 | $container->addRoute($target, $route); |
126 | 126 | } |
127 | 127 | |
128 | - public $returnTarget='response.content.data'; |
|
128 | + public $returnTarget = 'response.content.data'; |
|
129 | 129 | } |
130 | 130 | \ No newline at end of file |