1 | <?php |
||
29 | class Dispatcher extends AbstractMiddleware implements MiddlewareInterface |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $namespace; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $action; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $controller; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $args = []; |
||
51 | |||
52 | /** |
||
53 | * Handles a Request and updated the response |
||
54 | * |
||
55 | * @param ServerRequestInterface $request |
||
56 | * @param ResponseInterface $response |
||
57 | * |
||
58 | * @return ResponseInterface |
||
59 | */ |
||
60 | 10 | public function handle( |
|
84 | |||
85 | |||
86 | 2 | protected function checkView( |
|
87 | ServerRequestInterface $request, |
||
88 | ControllerNotFoundException $caught |
||
89 | ) { |
||
90 | /** @var Route $route */ |
||
91 | 2 | $route = $request->getAttribute('route'); |
|
92 | 2 | $def = $route->attributes; |
|
93 | 2 | $viewName = "{$def['controller']}/{$def['action']}.twig"; |
|
94 | 2 | $paths = Template::getPaths(); |
|
95 | 2 | foreach ($paths as $path) { |
|
96 | 2 | $filename = str_replace('//', '/', "{$path}/{$viewName}"); |
|
97 | 2 | if (is_file($filename)) { |
|
98 | 2 | return; |
|
99 | } |
||
100 | } |
||
101 | 2 | throw $caught; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Sets the data values into request |
||
106 | * |
||
107 | * @param ControllerInterface $controller |
||
108 | * @param ServerRequestInterface $request |
||
109 | * |
||
110 | * @return ServerRequestInterface|static |
||
111 | */ |
||
112 | 4 | protected function setViewVars( |
|
113 | ControllerInterface $controller, ServerRequestInterface $request |
||
114 | ) { |
||
115 | 4 | $key = $controller::REQUEST_ATTR_VIEW_DATA; |
|
116 | 4 | $data = $request->getAttribute($key, []); |
|
117 | 4 | $request = $request->withAttribute( |
|
118 | $key, |
||
119 | 4 | array_merge($data, $controller->getViewVars()) |
|
120 | ); |
||
121 | 4 | return $request; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * Creates the controller with provided class name |
||
126 | * |
||
127 | * @param string $controller |
||
128 | * |
||
129 | * @return ControllerInterface |
||
130 | */ |
||
131 | 10 | protected function createController($controller) |
|
132 | { |
||
133 | 10 | $this->checkClass($controller); |
|
134 | 8 | $handler = Application::container()->make($controller); |
|
135 | 8 | if (! $handler instanceof ControllerInterface) { |
|
136 | 2 | throw new InvalidControllerException( |
|
137 | 2 | "The class '{$controller}' does not implement ControllerInterface." |
|
138 | ); |
||
139 | } |
||
140 | 6 | return $handler; |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Check if class exists |
||
145 | * |
||
146 | * @param string $className |
||
147 | * @return $this|self|Dispatcher |
||
148 | */ |
||
149 | 10 | protected function checkClass($className) |
|
150 | { |
||
151 | 10 | if ($className == '\\' || !class_exists($className)) { |
|
152 | 2 | throw new ControllerNotFoundException( |
|
153 | 2 | "The controller '{$className}' was not found." |
|
154 | ); |
||
155 | } |
||
156 | 8 | return $this; |
|
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param Route $route |
||
161 | * |
||
162 | * @return $this|self|Dispatcher |
||
163 | */ |
||
164 | 10 | protected function setAttributes(Route $route) |
|
165 | { |
||
166 | 10 | $this->namespace = array_key_exists('namespace', $route->attributes) |
|
167 | 8 | ? $route->attributes['namespace'] |
|
168 | 2 | : null; |
|
169 | 10 | $this->controller = array_key_exists('controller', $route->attributes) |
|
170 | 10 | ? ucfirst($this->normalize($route->attributes['controller'])) |
|
171 | : null; |
||
172 | 10 | $this->action = array_key_exists('action', $route->attributes) |
|
173 | 8 | ? $this->normalize($route->attributes['action']) |
|
174 | 2 | : null; |
|
175 | 10 | $this->args = array_key_exists('args', $route->attributes) |
|
176 | 2 | ? $route->attributes['args'] |
|
177 | 8 | : []; |
|
178 | 10 | return $this; |
|
179 | } |
||
180 | |||
181 | /** |
||
182 | * Normalize controller/action names |
||
183 | * |
||
184 | * @param string $name |
||
185 | * @return string |
||
186 | */ |
||
187 | 10 | protected function normalize($name) |
|
194 | |||
195 | /** |
||
196 | * Check if action is defined in the controller |
||
197 | * |
||
198 | * @param string $className |
||
199 | * |
||
200 | * @return Dispatcher |
||
201 | */ |
||
202 | 6 | protected function checkAction($className) |
|
212 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.