Conditions | 42 |
Paths | > 20000 |
Total Lines | 227 |
Code Lines | 129 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
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 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments( |
||
60 | array_filter(explode('/', $uriPath))) |
||
|
|||
61 | ); |
||
62 | } |
||
63 | |||
64 | unset($uriPathParts, $uriPath); |
||
65 | } |
||
66 | |||
67 | // Load app addresses config |
||
68 | $this->addresses = config()->loadFile('addresses', true); |
||
69 | |||
70 | if ($this->addresses instanceof KernelAddresses) { |
||
71 | // Domain routing |
||
72 | if (null !== ($domain = $this->addresses->getDomain())) { |
||
73 | if (is_array($domain)) { |
||
74 | $this->uri->segments->exchangeArray( |
||
75 | array_merge($domain, $this->uri->segments->getArrayCopy()) |
||
76 | ); |
||
77 | $domain = $this->uri->segments->first(); |
||
78 | } |
||
79 | |||
80 | if (false !== ($app = modules()->getApp($domain))) { |
||
81 | $this->registerModule($app); |
||
82 | } elseif (false !== ($module = modules()->getModule($domain))) { |
||
83 | $this->registerModule($module); |
||
84 | } |
||
85 | } elseif (false !== ($subdomain = $this->uri->getSubdomain())) { |
||
86 | if (false !== ($app = modules()->getApp($subdomain))) { |
||
87 | $this->registerModule($app); |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | |||
92 | // App and Module routing |
||
93 | if ($numOfUriSegments = $this->uri->segments->count()) { |
||
94 | if (empty($app)) { |
||
95 | if (false !== ($module = modules()->getModule($this->uri->segments->first()))) { |
||
96 | $this->registerModule($module); |
||
97 | |||
98 | if ($module->getType() === 'APP') { |
||
99 | $this->uri->segments->shift(); |
||
100 | $this->handleAppRequest($module); |
||
101 | } else { |
||
102 | $this->handleSegmentsRequest(); |
||
103 | } |
||
104 | } |
||
105 | } elseif (false !== ($module = modules()->getModule($this->uri->segments->first()))) { |
||
106 | $this->registerModule($module); |
||
107 | |||
108 | if ($module->getType() === 'APP') { |
||
109 | $this->uri->segments->shift(); |
||
110 | $this->handleAppRequest($module); |
||
111 | } else { |
||
112 | $this->handleSegmentsRequest(); |
||
113 | } |
||
114 | } else { |
||
115 | $this->handleAppRequest($app); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | // Try to translate from uri string |
||
120 | if (false !== ($action = $this->addresses->getTranslation($this->uri->segments->__toString()))) { |
||
121 | if ( ! $action->isValidHttpMethod(input()->server('REQUEST_METHOD')) && ! $action->isAnyHttpMethod()) { |
||
122 | output()->sendError(405); |
||
123 | } else { |
||
124 | // Checks if action closure is an array |
||
125 | if (is_array($closureSegments = $action->getClosure())) { |
||
126 | $this->uri->segments->exchangeArray($closureSegments); |
||
127 | |||
128 | if (false !== ($module = modules()->getModule($this->uri->segments->first()))) { |
||
129 | $this->registerModule($module); |
||
130 | |||
131 | if ($module->getType() === 'APP') { |
||
132 | $this->uri->segments->shift(); |
||
133 | $this->handleAppRequest($module); |
||
134 | } else { |
||
135 | $this->handleSegmentsRequest(); |
||
136 | } |
||
137 | } else { |
||
138 | $this->handleSegmentsRequest(); |
||
139 | } |
||
140 | } else { |
||
141 | if (false !== ($parseSegments = $action->getParseUriString($this->uri->segments->__toString()))) { |
||
142 | $uriSegments = $parseSegments; |
||
143 | } else { |
||
144 | $uriSegments = []; |
||
145 | } |
||
146 | |||
147 | $this->uri = $this->uri->withSegments(new KernelMessageUriSegments($uriSegments)); |
||
148 | $uriString = $this->uri->segments->__toString(); |
||
149 | |||
150 | $this->parseAction($action, $uriSegments); |
||
151 | if ( ! empty(services()->has('controller'))) { |
||
152 | return true; |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | |||
158 | // Try to get route from controller & page |
||
159 | if ($numOfUriSegments = $this->uri->segments->count()) { |
||
160 | $uriSegments = $this->uri->segments->getArrayCopy(); |
||
161 | $uriString = $this->uri->segments->__toString(); |
||
162 | |||
163 | for ($i = 0; $i <= $numOfUriSegments; $i++) { |
||
164 | $uriRoutedSegments = array_slice($uriSegments, 0, ($numOfUriSegments - $i)); |
||
165 | $modules = modules()->getArrayCopy(); |
||
166 | |||
167 | foreach ($modules as $module) { |
||
168 | $controllerNamespace = $module->getNamespace() . 'Controllers\\'; |
||
169 | |||
170 | if ($module->getNamespace() === 'O2System\Framework\\') { |
||
171 | $controllerNamespace = 'O2System\Framework\Http\Controllers\\'; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Try to find requested controller |
||
176 | */ |
||
177 | if (class_exists($controllerClassName = $controllerNamespace . implode('\\', |
||
178 | array_map('studlycase', $uriRoutedSegments)))) { |
||
179 | |||
180 | if ($controllerClassName::$inherited) { |
||
181 | $uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
||
182 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
183 | $uriSegments); |
||
184 | |||
185 | break; |
||
186 | } else { |
||
187 | $uriSegments = array_diff($uriSegments, $uriRoutedSegments); |
||
188 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
189 | $uriSegments); |
||
190 | |||
191 | break; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Try to find requested page |
||
197 | */ |
||
198 | if (false !== ($pagesDir = $module->getResourcesDir('pages'))) { |
||
199 | if ($controllerClassName = $this->getPagesControllerClassName()) { |
||
200 | |||
201 | /** |
||
202 | * Try to find from database |
||
203 | */ |
||
204 | $modelClassName = str_replace('Controllers', 'Models', $controllerClassName); |
||
205 | |||
206 | if (class_exists($modelClassName)) { |
||
207 | models()->load($modelClassName, 'controller'); |
||
208 | |||
209 | if (false !== ($page = models('controller')->find($uriString, 'segments'))) { |
||
210 | if (isset($page->content)) { |
||
211 | presenter()->partials->offsetSet('content', $page->content); |
||
212 | |||
213 | $this->setController( |
||
214 | (new KernelControllerDataStructure($controllerClassName)) |
||
215 | ->setRequestMethod('index') |
||
216 | ); |
||
217 | |||
218 | return true; |
||
219 | break; |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Try to find from page file |
||
226 | */ |
||
227 | foreach (['.phtml', '.vue'] as $pageExtension) { |
||
228 | $pageFilePath = $pagesDir . implode(DIRECTORY_SEPARATOR, |
||
229 | array_map('dash', $uriRoutedSegments)) . $pageExtension; |
||
230 | |||
231 | if (is_file($pageFilePath)) { |
||
232 | presenter()->page->setFile($pageFilePath); |
||
233 | break; |
||
234 | } else { |
||
235 | $pageFilePath = str_replace($pageExtension, |
||
236 | DIRECTORY_SEPARATOR . 'index' . $pageExtension, $pageFilePath); |
||
237 | if (is_file($pageFilePath)) { |
||
238 | presenter()->page->setFile($pageFilePath); |
||
239 | break; |
||
240 | } |
||
241 | } |
||
242 | } |
||
243 | |||
244 | if (presenter()->page->file instanceof SplFileInfo) { |
||
245 | $this->setController( |
||
246 | (new KernelControllerDataStructure($controllerClassName)) |
||
247 | ->setRequestMethod('index') |
||
248 | ); |
||
249 | |||
250 | return true; |
||
251 | break; |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | } |
||
256 | |||
257 | // break the loop if the controller has been set |
||
258 | if (services()->has('controller')) { |
||
259 | return true; |
||
260 | break; |
||
261 | } |
||
262 | } |
||
263 | } |
||
264 | |||
265 | if (class_exists($controllerClassName = modules()->top()->getDefaultControllerClassName())) { |
||
266 | $this->setController(new KernelControllerDataStructure($controllerClassName), |
||
267 | $this->uri->segments->getArrayCopy()); |
||
268 | |||
269 | return true; |
||
270 | } |
||
558 | } |