-
Notifications
You must be signed in to change notification settings - Fork 0
/
Autoloader.php
executable file
·110 lines (93 loc) · 2.64 KB
/
Autoloader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
//echo "Hi! :D";
define('CORE_DIR', __DIR__);
use Phalcon\Logger;
use Phalcon\Events\Event;
use Phalcon\Events\Manager as EventsManager;
use Phalcon\Logger\Adapter\File as FileLogger;
use Phalcon\DI\FactoryDefault as DefaultDI,
Phalcon\Mvc\Micro\Collection,
Phalcon\Config\Adapter\Ini as IniConfig,
Phalcon\Loader;
if( !is_array($loaderList) ) $loaderList = array();
$loaderList['Utils'] = CORE_DIR . "/Utils/";
$loaderList['PhalconRest'] = CORE_DIR . "/";
$loaderList['Phalcon'] = CORE_DIR . '/Libs/';
$loader = new Loader();
$loader->registerNamespaces($loaderList)->register();
$di = new DefaultDI();
$di->set('collections', function(){
return include(CORE_DIR . '/routeLoader.php');
});
$di->setShared('config', function() {
return new IniConfig(ROOT_DIR . "/config.ini");
});
$di->setShared('session', function(){
$session = new \Phalcon\Session\Adapter\Files();
$session->start();
return $session;
});
$di->setShared('utils', function() {
return new \Utils\Utils();
});
$di->set('modelsCache', function() {
$frontCache = new \Phalcon\Cache\Frontend\Data(array(
'lifetime' => 3600
));
//File cache settings
$cache = new \Phalcon\Cache\Backend\File($frontCache, array(
'cacheDir' => __DIR__ . '/cache/'
));
return $cache;
});
$di->set('db', function(){
$conf = $this->get('config');
if(isset($conf->database)){
$dbConf = $conf->database->toArray();
unset($dbConf['adapter']);
$className = "\\Phalcon\\Db\\Adapter\\Pdo\\{$conf->database->adapter}";
$connection = new $className($dbConf);
// remember not to use in production since it can reveal very sensitive data
if ($dbConf['debug'] ) {
$eventsManager = new EventsManager();
$logger = new FileLogger(ROOT_DIR.'/logs/db.log');
$eventsManager->attach(
'db:beforeQuery',
function (Event $event, $connection) use ($logger) {
$sql = $connection->getSQLStatement();
$logger->log($sql, Logger::INFO);
}
);
$connection->setEventsManager($eventsManager);
}
return $connection;
}
});
$di->setShared('requestBody', function() {
$in = file_get_contents('php://input');
$in = json_decode($in, FALSE);
if($in === null){
throw new HTTPException(
'There was a problem understanding the data sent to the server by the application.',
409,
array(
'dev' => 'The JSON body sent to the server was unable to be parsed.',
'internalCode' => 'REQ1000',
'more' => ''
)
);
}
return $in;
});
/**
* Shared logger.
*/
$di->setShared('logger', function() {
$logger = new FileLogger('/var/log/nginx/site/debug.log');
$logger->setLogLevel(
Logger::DEBUG
);
return $logger;
});
$app = new Phalcon\Mvc\Micro();
$app->setDI($di);