Conditions | 52 |
Paths | > 20000 |
Total Lines | 281 |
Code Lines | 170 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 4 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
42 | */ |
||
43 | public function handle(KernelMessageUri $uri = null) |
||
44 | { |
||
45 | $this->uri = is_null($uri) ? new KernelMessageUri() : $uri; |
||
46 | |||
47 | // Handle Extension Request |
||
48 | if ($this->uri->segments->count()) { |
||
|
|||
49 | $this->handleExtensionRequest(); |
||
50 | } else { |
||
51 | $uriPath = urldecode( |
||
52 | parse_url($_SERVER[ 'REQUEST_URI' ], PHP_URL_PATH) |
||
53 | ); |
||
54 | |||
55 | $uriPathParts = explode('public/', $uriPath); |
||
56 | $uriPath = end($uriPathParts); |
||
57 | |||
58 | if ($uriPath !== '/') { |
||
59 | $uriString = $uriPath; // tobe removed |
||
60 | $uriSegments = array_filter(explode('/', $uriString)); // tobe removed |
||
61 | |||
62 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments( |
||
63 | array_filter(explode('/', $uriString))) |
||
64 | ); |
||
65 | } |
||
66 | |||
67 | unset($uriPathParts, $uriPath); |
||
68 | } |
||
69 | |||
70 | // Load app addresses config |
||
71 | $this->addresses = config()->loadFile('addresses', true); |
||
72 | |||
73 | if ($this->addresses instanceof KernelAddresses) { |
||
74 | // Domain routing |
||
75 | if (null !== ($domain = $this->addresses->getDomain())) { |
||
76 | if (is_array($domain)) { |
||
77 | $this->uri->segments->exchangeArray( |
||
78 | array_merge($domain, $this->uri->segments->getArrayCopy()) |
||
79 | ); |
||
80 | $domain = $this->uri->segments->first(); |
||
81 | } |
||
82 | |||
83 | if (false !== ($app = modules()->getApp($domain))) { |
||
84 | $this->registerModule($app); |
||
85 | } elseif (false !== ($module = modules()->getModule($domain))) { |
||
86 | $this->registerModule($module); |
||
87 | } |
||
88 | } elseif (false !== ($subdomain = $this->uri->getSubdomain())) { |
||
89 | if (false !== ($app = modules()->getApp($subdomain))) { |
||
90 | $this->registerModule($app); |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | |||
95 | // App and Module routing |
||
96 | if ($numOfUriSegments = $this->uri->segments->count()) { |
||
97 | if (empty($app)) { |
||
98 | if (false !== ($module = modules()->getModule($this->uri->segments->first()))) { |
||
99 | $this->registerModule($module); |
||
100 | |||
101 | if ($module->getType() === 'APP') { |
||
102 | $this->uri->segments->shift(); |
||
103 | $this->handleAppRequest($module); |
||
104 | } else { |
||
105 | $this->handleSegmentsRequest(); |
||
106 | } |
||
107 | } |
||
108 | } else { |
||
109 | $this->handleAppRequest($app); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | // Try to translate from uri string |
||
114 | if (false !== ($action = $this->addresses->getTranslation($this->uri->segments->__toString()))) { |
||
115 | if ( ! $action->isValidHttpMethod(input()->server('REQUEST_METHOD')) && ! $action->isAnyHttpMethod()) { |
||
116 | output()->sendError(405); |
||
117 | } else { |
||
118 | // Checks if action closure is an array |
||
119 | if (is_array($closureSegments = $action->getClosure())) { |
||
120 | $this->uri->segments->exchangeArray($closureSegments); |
||
121 | |||
122 | if (false !== ($module = modules()->getModule($this->uri->segments->first()))) { |
||
123 | $this->registerModule($module); |
||
124 | |||
125 | if ($module->getType() === 'APP') { |
||
126 | $this->uri->segments->shift(); |
||
127 | $this->handleAppRequest($module); |
||
128 | } else { |
||
129 | $this->handleSegmentsRequest(); |
||
130 | } |
||
131 | } else { |
||
132 | $this->handleSegmentsRequest(); |
||
133 | } |
||
134 | } else { |
||
135 | if (false !== ($parseSegments = $action->getParseUriString($this->uri->segments->__toString()))) { |
||
136 | $uriSegments = $parseSegments; |
||
137 | } else { |
||
138 | $uriSegments = []; |
||
139 | } |
||
140 | |||
141 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
142 | $uriString = $this->uri->segments->__toString(); |
||
143 | |||
144 | $this->parseAction($action, $uriSegments); |
||
145 | if ( ! empty(services()->has('controller'))) { |
||
146 | return true; |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | |||
152 | // Try to get route from controller & page |
||
153 | if ($numOfUriSegments = $this->uri->segments->count()) { |
||
154 | $uriSegments = $this->uri->segments->getArrayCopy(); |
||
155 | $uriString = $this->uri->segments->__toString(); |
||
156 | |||
157 | for ($i = 0; $i <= $numOfUriSegments; $i++) { |
||
158 | $uriRoutedSegments = array_slice($uriSegments, 0, ($numOfUriSegments - $i)); |
||
159 | $modules = modules()->getArrayCopy(); |
||
160 | |||
161 | foreach ($modules as $module) { |
||
162 | $controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
||
163 | |||
164 | if ($module->getNamespace() === 'O2System\Framework\\') { |
||
165 | $controllerNamespace = 'O2System\Framework\Http\Controllers\\'; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Try to find requested controller |
||
170 | */ |
||
171 | if (class_exists($controllerClassName = $controllerNamespace . implode('\\', |
||
172 | array_map('studlycase', $uriRoutedSegments)))) { |
||
173 | |||
174 | if ($controllerClassName::$inherited) { |
||
175 | $uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
||
176 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
177 | $uriSegments); |
||
178 | |||
179 | break; |
||
180 | } else { |
||
181 | $uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
||
182 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
183 | $uriSegments); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Try to find requested page |
||
189 | */ |
||
190 | if (false !== ($pagesDir = $module->getResourcesDir('pages'))) { |
||
191 | if ($controllerClassName = $this->getPagesControllerClassName()) { |
||
192 | |||
193 | /** |
||
194 | * Try to find from database |
||
195 | */ |
||
196 | $modelClassName = str_replace('Controllers', 'Models', $controllerClassName); |
||
197 | |||
198 | if (class_exists($modelClassName)) { |
||
199 | models()->load($modelClassName, 'controller'); |
||
200 | |||
201 | if (false !== ($page = models('controller')->find($uriString, 'segments'))) { |
||
202 | if (isset($page->content)) { |
||
203 | presenter()->partials->offsetSet('content', $page->content); |
||
204 | |||
205 | $this->setController( |
||
206 | (new KernelControllerDataStructure($controllerClassName)) |
||
207 | ->setRequestMethod('index') |
||
208 | ); |
||
209 | |||
210 | return true; |
||
211 | break; |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Try to find from page file |
||
218 | */ |
||
219 | |||
220 | foreach (['.phtml', '.vue'] as $pageExtension) { |
||
221 | $pageFilePath = $pagesDir . implode(DIRECTORY_SEPARATOR, |
||
222 | array_map('dash', $uriRoutedSegments)) . $pageExtension; |
||
223 | |||
224 | if (is_file($pageFilePath)) { |
||
225 | presenter()->page->setFile($pageFilePath); |
||
226 | break; |
||
227 | } else { |
||
228 | $pageFilePath = str_replace($pageExtension, |
||
229 | DIRECTORY_SEPARATOR . 'index' . $pageExtension, $pageFilePath); |
||
230 | if (is_file($pageFilePath)) { |
||
231 | presenter()->page->setFile($pageFilePath); |
||
232 | break; |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | |||
237 | if (presenter()->page->file instanceof SplFileInfo) { |
||
238 | $this->setController( |
||
239 | (new KernelControllerDataStructure($controllerClassName)) |
||
240 | ->setRequestMethod('index') |
||
241 | ); |
||
242 | |||
243 | return true; |
||
244 | break; |
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | } |
||
249 | |||
250 | // break the loop if the controller has been set |
||
251 | if (services()->has('controller')) { |
||
252 | return true; |
||
253 | break; |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | |||
258 | if (class_exists($controllerClassName = modules()->top()->getDefaultControllerClassName())) { |
||
259 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
260 | $this->uri->segments->getArrayCopy()); |
||
261 | |||
262 | return true; |
||
263 | } |
||
264 | |||
265 | // Let's the framework do the rest when there is no controller found |
||
266 | // the framework will redirect to PAGE 404 |
||
267 | } |
||
268 | |||
269 | // ------------------------------------------------------------------------ |
||
270 | |||
271 | protected function handleExtensionRequest() |
||
272 | { |
||
273 | $lastSegment = $this->uri->segments->last(); |
||
274 | |||
275 | if (strpos($lastSegment, '.json') !== false) { |
||
276 | output()->setContentType('application/json'); |
||
277 | $lastSegment = str_replace('.json', '', $lastSegment); |
||
278 | $this->uri->segments->pop(); |
||
279 | $this->uri->segments->push($lastSegment); |
||
280 | } elseif (strpos($lastSegment, '.xml') !== false) { |
||
281 | output()->setContentType('application/xml'); |
||
282 | $lastSegment = str_replace('.xml', '', $lastSegment); |
||
283 | $this->uri->segments->pop(); |
||
284 | $this->uri->segments->push($lastSegment); |
||
285 | } elseif (strpos($lastSegment, '.js') !== false) { |
||
286 | output()->setContentType('application/x-javascript'); |
||
287 | $lastSegment = str_replace('.js', '', $lastSegment); |
||
288 | $this->uri->segments->pop(); |
||
289 | $this->uri->segments->push($lastSegment); |
||
290 | } elseif (strpos($lastSegment, '.css') !== false) { |
||
291 | output()->setContentType('text/css'); |
||
292 | $lastSegment = str_replace('.css', '', $lastSegment); |
||
293 | $this->uri->segments->pop(); |
||
294 | $this->uri->segments->push($lastSegment); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Router::handleAppRequest |
||
300 | * |
||
301 | * @param \O2System\Framework\Containers\Modules\DataStructures\Module $app |
||
302 | */ |
||
303 | public function handleAppRequest(FrameworkModuleDataStructure $app) |
||
304 | { |
||
305 | // Find App module |
||
306 | foreach(['modules', 'plugins'] as $additionalSegment) { |
||
307 | if (false !== ($module = modules()->getModule([ |
||
308 | $app->getParameter(), |
||
309 | $additionalSegment, |
||
310 | $this->uri->segments->first(), |
||
311 | ]))) { |
||
312 | $this->uri->segments->shift(); |
||
313 | |||
314 | $this->registerModule($module); |
||
315 | $this->handleSegmentsRequest(); |
||
316 | break; |
||
317 | } |
||
318 | } |
||
319 | } |
||
320 | |||
321 | // ------------------------------------------------------------------------ |
||
322 | |||
323 | /** |
||
534 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.