Conditions | 7 |
Paths | 4 |
Total Lines | 82 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | 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 |
||
12 | public function run(){ |
||
13 | // Маршрутизация статичных ресурсов. |
||
14 | $request = new Request; |
||
15 | if(strpos($request->path(), '.') !== false){ |
||
16 | return false; |
||
17 | } |
||
18 | |||
19 | $locator = new Locator; |
||
20 | |||
21 | // Конфигурация загрузчика. |
||
22 | $locator['loader'] = new Loader; |
||
23 | $locator['loader']->pref('Bricks\Frameworks\Base\Controller', 'backend/controller'); |
||
24 | $locator['loader']->pref('Bricks\Frameworks\Base\Template', 'backend/template'); |
||
25 | |||
26 | // Маршрутизация страниц. |
||
27 | $locator['router'] = new Router; |
||
28 | $locator['router']->all('~^/([A-Za-z0-9_]+)/([A-Za-z0-9_]+)~', |
||
29 | function(Request $request, Response $response, array $match) use($locator){ |
||
30 | $controllerName = ucfirst($match[0]); |
||
31 | $controller = 'Bricks\Frameworks\Base\Controller\\' . $controllerName; |
||
32 | $action = $match[1] . 'Action'; |
||
33 | |||
34 | if(!file_exists($locator['loader']->path($controller))){ |
||
35 | throw new RoutingException('Controller not found'); |
||
36 | } |
||
37 | |||
38 | $controller = new $controller($locator, $controllerName, $action); |
||
39 | if(!method_exists($controller, $action)){ |
||
40 | throw new RoutingException('Controller not found'); |
||
41 | } |
||
42 | |||
43 | return $controller->$action(); |
||
44 | } |
||
45 | ); |
||
46 | |||
47 | $locator['router']->all('~^/([A-Za-z0-9_]+)~', |
||
48 | function(Request $request, Response $response, array $match) use($locator){ |
||
49 | $controllerName = ucfirst($match[0]); |
||
50 | $controller = 'Bricks\Frameworks\Base\Controller\\' . $controllerName; |
||
51 | |||
52 | if(!file_exists($locator['loader']->path($controller))){ |
||
53 | throw new RoutingException('Controller not found'); |
||
54 | } |
||
55 | |||
56 | $controller = new $controller($locator, $controllerName, 'index'); |
||
57 | return $controller->indexAction(); |
||
58 | } |
||
59 | ); |
||
60 | |||
61 | $locator['router']->all('~^/~', |
||
62 | function() use($locator){ |
||
63 | $controller = new Controller\Index($locator, 'Index', 'index'); |
||
64 | return $controller->indexAction(); |
||
65 | } |
||
66 | ); |
||
67 | |||
68 | // Конфигурация шаблонизатора. |
||
69 | Template::helper('include', function($resource, array $env = []) use($locator){ |
||
70 | $template = new Template($locator['loader']->path('Bricks\Frameworks\Base\Template\\' . $resource, 'html')); |
||
71 | return $template->env($env); |
||
72 | }); |
||
73 | |||
74 | // Выполнение маршрутизации. |
||
75 | $locator['request'] = $request; |
||
76 | $locator['response'] = new Response; |
||
77 | try{ |
||
78 | $responseBody = $locator['router']->run($locator['request'], $locator['response']); |
||
79 | } |
||
80 | // Обработка исключений. |
||
81 | catch(RoutingException $exception){ |
||
82 | $responseBody = new Template($locator['loader']->path('Bricks\Frameworks\Base\Template\404', 'html')); |
||
83 | } |
||
84 | catch(\Exception $exception){ |
||
85 | $responseBody = new Template($locator['loader']->path('Bricks\Frameworks\Base\Template\500', 'html')); |
||
86 | } |
||
87 | |||
88 | // Возврат ответа. |
||
89 | $locator['response']->body($responseBody); |
||
90 | $locator['response']->send(); |
||
91 | |||
92 | return true; |
||
93 | } |
||
94 | } |
||
95 |