Completed
Branch v2.0.0 (e47d62)
by Alexander
03:40
created

Application::initView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * @author Alexander Torosh <[email protected]>
4
 */
5
6
namespace Web;
7
8
use Core\Annotations\AnnotationsManager;
9
use Core\Assets\AssetsHelper;
10
use Core\Cache\ApcuCache;
11
use Core\Config\EnvironmentLoader;
12
use Dashboard\Module as DashboardModule;
13
use Front\Module as FrontModule;
14
use Phalcon\Debug;
15
use Phalcon\Di\DiInterface;
16
use Phalcon\Di\FactoryDefault;
17
use Phalcon\Mvc\Application as PhalconApplication;
18
use Phalcon\Mvc\Dispatcher;
19
use Phalcon\Tag;
20
use Phalcon\Url;
21
use Web\Exceptions\AccessDeniedException;
22
23
class Application
24
{
25
    public function run()
26
    {
27
        // DI Container
28
        $container = new FactoryDefault();
29
30
        // Initialize app
31
        $app = new PhalconApplication($container);
32
33
        // Set serverCache service
34
        $container->setShared('serverCache', (new ApcuCache())->init());
35
36
        $this->initConfiguration($container);
37
38
        // Phalcon Debug
39
        if ('development' === getenv('APP_ENV')) {
40
            $debug = new Debug();
41
            $debug->listen();
42
        }
43
44
        $this->initEventsManager($app, $container);
45
        $this->initDispatchingProcess($app, $container);
46
        $this->initAnnotations($container);
47
        $this->initAcl($container);
48
        $this->initView($container);
49
        $this->initApplicationServices($container);
50
51
        try {
52
            // Handle request
53
            $response = $app->handle($_SERVER['REQUEST_URI']);
54
            $response->send();
55
        } catch (AccessDeniedException $e) {
56
            // Access Denied
57
            $this->handleAccessDenied($app);
58
        } catch (Dispatcher\Exception $e) {
59
            // 404 Not Found
60
            $this->notFoundError($app);
61
        } catch (\Exception $e) {
62
            // 503 Server Error
63
            $this->serviceUnavailableError($app, $e);
64
        }
65
    }
66
67
    private function initConfiguration(DiInterface $container)
68
    {
69
        $configLoader = new EnvironmentLoader();
70
        $configLoader->setDI($container);
71
        $configLoader->load();
72
    }
73
74
    private function initEventsManager(PhalconApplication $app, DiInterface $container)
75
    {
76
        // Create EventsManager Manager
77
        $webEventsManager = new EventsManager($container);
78
        $eventsManager = $webEventsManager->getEventsManager();
79
80
        // Save Events Manager to DI Container
81
        $container->setShared('eventsManager', $eventsManager);
82
83
        // Bind Events Manager to Application
84
        $app->setEventsManager($eventsManager);
85
    }
86
87
    private function initDispatchingProcess(PhalconApplication $app, DiInterface $container)
88
    {
89
        // Router
90
        $webRouter = new Router($container);
91
        $container->setShared('router', $webRouter->getRouter());
92
93
        // Register Application Modules
94
        $app->registerModules([
95
            'front' => [
96
                'className' => FrontModule::class,
97
                'path' => __DIR__.'/../modules/front/src/Module.php',
98
            ],
99
            'dashboard' => [
100
                'className' => DashboardModule::class,
101
                'path' => __DIR__.'/../modules/dashboard/src/Module.php',
102
            ],
103
        ]);
104
    }
105
106
    private function initAnnotations(DiInterface $container)
107
    {
108
        $annotationsManager = new AnnotationsManager($container);
109
        $container->setShared('annotations', $annotationsManager->getAnnotations());
110
    }
111
112
    private function initAcl(DiInterface $container)
113
    {
114
        $aclManager = new AclManager($container, $container->get('eventsManager'));
115
        $container->setShared('acl', $aclManager->getAcl());
116
    }
117
118
    private function initView(DiInterface $container)
119
    {
120
        $webView = new View();
121
        $webView->setEventsManager($container->get('eventsManager'));
122
        $container->setShared('view', $webView);
123
    }
124
125
    private function initApplicationServices(DiInterface $container)
126
    {
127
        // Url
128
        $url = new Url();
129
        $url->setBaseUri('/');
130
        $container->setShared('url', $url);
131
132
        // Assets Build Resolver
133
        $assetsHelper = new AssetsHelper($container);
134
        $container->setShared('assetsHelper', $assetsHelper);
135
136
        // @TODO Move it to another place
137
        // Tag
138
        Tag::setTitleSeparator(' - ');
139
        Tag::setTitle('Yona CMS');
140
    }
141
142
    private function handleAccessDenied(PhalconApplication $app)
143
    {
144
        $app->response
145
            ->redirect($app->url->get(
146
                ['for' => 'dashboardLogin'],
147
                ['redirect' => $app->request->getURI()]
148
            ))
149
            ->send()
150
        ;
151
    }
152
153
    private function notFoundError(PhalconApplication $app)
154
    {
155
        Tag::prependTitle('Page Not Found');
156
157
        $app->view->render('errors', '404-not-found');
158
        $app->response
159
            ->setStatusCode(404)
160
            ->send()
161
        ;
162
    }
163
164
    private function serviceUnavailableError(PhalconApplication $app, \Exception $e)
165
    {
166
        Tag::prependTitle('Service Unavailable');
167
168
        $app->view->render('errors', '503-service-unavailable', [
169
            'message' => $e->getMessage(),
170
        ]);
171
        $app->response
172
            ->setStatusCode(503)
173
            ->send()
174
        ;
175
    }
176
}
177