App   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 0
cbo 8
dl 0
loc 84
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C run() 0 82 7
1
<?php
2
namespace Bricks\Frameworks\Base;
3
use Bricks\Autoload\Loader;
4
use Bricks\ServiceLocator\Manager as Locator;
5
use Bricks\Http\Routing\Router;
6
use Bricks\Http\Routing\Request;
7
use Bricks\Http\Routing\Response;
8
use Bricks\Http\Routing\RoutingException;
9
use Bricks\TemplateEngine\Php\Template;
10
11
class App{
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