|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+<?php
|
|
|
2
|
+declare(strict_types=1);
|
|
|
3
|
+
|
|
|
4
|
+use Phalcon\Escaper;
|
|
|
5
|
+use Phalcon\Flash\Direct as Flash;
|
|
|
6
|
+use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
|
|
|
7
|
+use Phalcon\Mvc\View;
|
|
|
8
|
+use Phalcon\Mvc\View\Engine\Php as PhpEngine;
|
|
|
9
|
+use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
|
|
|
10
|
+use Phalcon\Session\Adapter\Stream as SessionAdapter;
|
|
|
11
|
+use Phalcon\Session\Manager as SessionManager;
|
|
|
12
|
+use Phalcon\Url as UrlResolver;
|
|
|
13
|
+
|
|
|
14
|
+/**
|
|
|
15
|
+ * Shared configuration service
|
|
|
16
|
+ */
|
|
|
17
|
+$di->setShared('config', function () {
|
|
|
18
|
+ return include APP_PATH . "/config/config.php";
|
|
|
19
|
+});
|
|
|
20
|
+
|
|
|
21
|
+/**
|
|
|
22
|
+ * The URL component is used to generate all kind of urls in the application
|
|
|
23
|
+ */
|
|
|
24
|
+$di->setShared('url', function () {
|
|
|
25
|
+ $config = $this->getConfig();
|
|
|
26
|
+
|
|
|
27
|
+ $url = new UrlResolver();
|
|
|
28
|
+ $url->setBaseUri($config->application->baseUri);
|
|
|
29
|
+
|
|
|
30
|
+ return $url;
|
|
|
31
|
+});
|
|
|
32
|
+
|
|
|
33
|
+/**
|
|
|
34
|
+ * Setting up the view component
|
|
|
35
|
+ */
|
|
|
36
|
+$di->setShared('view', function () {
|
|
|
37
|
+ $config = $this->getConfig();
|
|
|
38
|
+
|
|
|
39
|
+ $view = new View();
|
|
|
40
|
+ $view->setDI($this);
|
|
|
41
|
+ $view->setViewsDir($config->application->viewsDir);
|
|
|
42
|
+
|
|
|
43
|
+ $view->registerEngines([
|
|
|
44
|
+ '.volt' => function ($view) {
|
|
|
45
|
+ $config = $this->getConfig();
|
|
|
46
|
+
|
|
|
47
|
+ $volt = new VoltEngine($view, $this);
|
|
|
48
|
+
|
|
|
49
|
+ $volt->setOptions([
|
|
|
50
|
+ 'path' => $config->application->cacheDir,
|
|
|
51
|
+ 'separator' => '_'
|
|
|
52
|
+ ]);
|
|
|
53
|
+
|
|
|
54
|
+ return $volt;
|
|
|
55
|
+ },
|
|
|
56
|
+ '.phtml' => PhpEngine::class
|
|
|
57
|
+
|
|
|
58
|
+ ]);
|
|
|
59
|
+
|
|
|
60
|
+ return $view;
|
|
|
61
|
+});
|
|
|
62
|
+
|
|
|
63
|
+/**
|
|
|
64
|
+ * Database connection is created based in the parameters defined in the configuration file
|
|
|
65
|
+ */
|
|
|
66
|
+$di->setShared('db', function () {
|
|
|
67
|
+ $config = $this->getConfig();
|
|
|
68
|
+
|
|
|
69
|
+ $class = 'Phalcon\Db\Adapter\Pdo\\' . $config->database->adapter;
|
|
|
70
|
+ $params = [
|
|
|
71
|
+ 'host' => $config->database->host,
|
|
|
72
|
+ 'username' => $config->database->username,
|
|
|
73
|
+ 'password' => $config->database->password,
|
|
|
74
|
+ 'dbname' => $config->database->dbname,
|
|
|
75
|
+ 'charset' => $config->database->charset
|
|
|
76
|
+ ];
|
|
|
77
|
+
|
|
|
78
|
+ if ($config->database->adapter == 'Postgresql') {
|
|
|
79
|
+ unset($params['charset']);
|
|
|
80
|
+ }
|
|
|
81
|
+
|
|
|
82
|
+ return new $class($params);
|
|
|
83
|
+});
|
|
|
84
|
+
|
|
|
85
|
+
|
|
|
86
|
+/**
|
|
|
87
|
+ * If the configuration specify the use of metadata adapter use it or use memory otherwise
|
|
|
88
|
+ */
|
|
|
89
|
+$di->setShared('modelsMetadata', function () {
|
|
|
90
|
+ return new MetaDataAdapter();
|
|
|
91
|
+});
|
|
|
92
|
+
|
|
|
93
|
+/**
|
|
|
94
|
+ * Register the session flash service with the Twitter Bootstrap classes
|
|
|
95
|
+ */
|
|
|
96
|
+$di->set('flash', function () {
|
|
|
97
|
+ $escaper = new Escaper();
|
|
|
98
|
+ $flash = new Flash($escaper);
|
|
|
99
|
+ $flash->setImplicitFlush(false);
|
|
|
100
|
+ $flash->setCssClasses([
|
|
|
101
|
+ 'error' => 'alert alert-danger',
|
|
|
102
|
+ 'success' => 'alert alert-success',
|
|
|
103
|
+ 'notice' => 'alert alert-info',
|
|
|
104
|
+ 'warning' => 'alert alert-warning'
|
|
|
105
|
+ ]);
|
|
|
106
|
+
|
|
|
107
|
+ return $flash;
|
|
|
108
|
+});
|
|
|
109
|
+
|
|
|
110
|
+/**
|
|
|
111
|
+ * Start the session the first time some component request the session service
|
|
|
112
|
+ */
|
|
|
113
|
+$di->setShared('session', function () {
|
|
|
114
|
+ $session = new SessionManager();
|
|
|
115
|
+ $files = new SessionAdapter([
|
|
|
116
|
+ 'savePath' => sys_get_temp_dir(),
|
|
|
117
|
+ ]);
|
|
|
118
|
+ $session->setAdapter($files);
|
|
|
119
|
+ $session->start();
|
|
|
120
|
+
|
|
|
121
|
+ return $session;
|
|
|
122
|
+});
|