1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Phalcon\DI\FactoryDefault; |
4
|
|
|
use Phalcon\Mvc\View; |
5
|
|
|
use Phalcon\Mvc\Url as UrlResolver; |
6
|
|
|
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter; |
7
|
|
|
use Phalcon\Mvc\View\Engine\Volt as VoltEngine; |
8
|
|
|
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter; |
9
|
|
|
use Phalcon\Session\Adapter\Files as SessionAdapter; |
10
|
|
|
use Ajax\JsUtils; |
11
|
|
|
use Ajax\Bootstrap; |
12
|
|
|
use Phalcon\Mvc\Dispatcher as MvcDispatcher; |
13
|
|
|
use Phalcon\Events\Manager as EventsManager; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework |
17
|
|
|
*/ |
18
|
|
|
$di = new FactoryDefault(); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The URL component is used to generate all kind of urls in the application |
22
|
|
|
*/ |
23
|
|
|
$di->set('url', function () use ($config) { |
24
|
|
|
$url = new UrlResolver(); |
25
|
|
|
$url->setBaseUri($config->application->baseUri); |
26
|
|
|
|
27
|
|
|
return $url; |
28
|
|
|
}, true); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Setting up the view component |
32
|
|
|
*/ |
33
|
|
|
$di->set('view', function () use ($config) { |
34
|
|
|
|
35
|
|
|
$view = new View(); |
36
|
|
|
|
37
|
|
|
$view->setViewsDir($config->application->viewsDir); |
38
|
|
|
|
39
|
|
|
$view->registerEngines(array( |
40
|
|
|
'.volt' => function ($view, $di) use ($config) { |
41
|
|
|
|
42
|
|
|
$volt = new VoltEngine($view, $di); |
43
|
|
|
|
44
|
|
|
$volt->setOptions(array( |
45
|
|
|
'compiledPath' => $config->application->cacheDir, |
46
|
|
|
'compiledSeparator' => '_' |
47
|
|
|
)); |
48
|
|
|
|
49
|
|
|
return $volt; |
50
|
|
|
}, |
51
|
|
|
'.phtml' => 'Phalcon\Mvc\View\Engine\Php' |
52
|
|
|
)); |
53
|
|
|
|
54
|
|
|
return $view; |
55
|
|
|
}, true); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Database connection is created based in the parameters defined in the configuration file |
59
|
|
|
*/ |
60
|
|
|
$di->set('db', function () use ($config) { |
61
|
|
|
return new DbAdapter(array( |
62
|
|
|
'host' => $config->database->host, |
63
|
|
|
'username' => $config->database->username, |
64
|
|
|
'password' => $config->database->password, |
65
|
|
|
'dbname' => $config->database->dbname, |
66
|
|
|
'options' => array( |
67
|
|
|
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' |
68
|
|
|
) |
69
|
|
|
)); |
70
|
|
|
}); |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
$di->set('baseUrl', function () use ($config) { |
74
|
|
|
$baseUrl = $config->application->baseUri; |
75
|
|
|
return $baseUrl; |
76
|
|
|
}); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* If the configuration specify the use of metadata adapter use it or use memory otherwise |
80
|
|
|
*/ |
81
|
|
|
$di->set('modelsMetadata', function () { |
82
|
|
|
return new MetaDataAdapter(); |
83
|
|
|
}); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Start the session the first time some component request the session service |
87
|
|
|
*/ |
88
|
|
|
$di->set('session', function () { |
89
|
|
|
$session = new SessionAdapter(); |
90
|
|
|
$session->start(); |
91
|
|
|
|
92
|
|
|
return $session; |
93
|
|
|
}); |
94
|
|
|
|
95
|
|
|
$di->set("jquery", function () { |
96
|
|
|
$jquery = new JsUtils(array("driver" => "Jquery")); |
97
|
|
|
$jquery->bootstrap(new Bootstrap()); |
98
|
|
|
return $jquery; |
99
|
|
|
}); |
100
|
|
|
|
101
|
|
|
$di->set('dispatcher', function () { |
102
|
|
|
|
103
|
|
|
// Create an event manager |
104
|
|
|
$eventsManager = new EventsManager(); |
105
|
|
|
|
106
|
|
|
// Attach a listener for type "dispatch" |
107
|
|
|
$eventsManager->attach("dispatch", function ($event, $dispatcher) { |
|
|
|
|
108
|
|
|
// ... |
109
|
|
|
}); |
110
|
|
|
|
111
|
|
|
$dispatcher = new MvcDispatcher(); |
112
|
|
|
|
113
|
|
|
// Bind the eventsManager to the view component |
114
|
|
|
$dispatcher->setEventsManager($eventsManager); |
115
|
|
|
|
116
|
|
|
return $dispatcher; |
117
|
|
|
|
118
|
|
|
}, true); |
119
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.