-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbootstrap.php
103 lines (82 loc) · 3.01 KB
/
bootstrap.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
<?php
/*
* This file is part of the PHALCON-EXT package.
*
* (c) Jitendra Adhikari <[email protected]>
* <https://github.com/adhocore>
*
* Licensed under MIT license.
*/
use Ahc\Jwt\JWT;
use Phalcon\Cache\Frontend\None as CacheFront;
use Phalcon\Mvc\View;
use PhalconExt\Cache\Redis;
use PhalconExt\Db\Sqlite;
use PhalconExt\Di\FactoryDefault;
use PhalconExt\Mail\Mailer;
use PhalconExt\Validation\Validation;
use PhalconExt\View\Twig;
if (getenv('APP_ENV') !== 'test' && PHP_SAPI !== 'cli') {
// For debug
(new Phalcon\Debug)->listen(true, true);
}
$loader = (new Phalcon\Loader)
->registerNamespaces([
'PhalconExt\\Example' => __DIR__ . '/',
'PhalconExt\\Test' => __DIR__ . '/../tests/',
'PhalconExt' => __DIR__ . '/../src/',
])
->registerClasses(require __DIR__ . '/../vendor/composer/autoload_classmap.php')
->register();
if (getenv('APP_ENV') !== 'test') {
$loader->registerFiles(require __DIR__ . '/../vendor/composer/autoload_files.php')->loadFiles();
}
$di = new FactoryDefault;
// Since it is registered as instantiated object, it is auto aliased when calling `registerAliases()`.
$di->setShared('loader', $loader);
// Setting config in DI is REQUIRED by this package.
$di->setShared('config', new Phalcon\Config(require __DIR__ . '/config.php'));
$di->setShared('db', function () {
$config = $this->get('config')->toArray();
// PS: If you had already extended your db adapter then imoort trait `PhalconExt\Db\Extension`
return (new Sqlite($config['database']))->registerLogger($config['sqllogger']);
});
$di->setShared('view', function () {
return (new View)
->setViewsDir($this->get('config')->toArray()['view']['dir'])
->registerEngines([
'.twig' => 'twig',
]);
});
$di->setShared('twig', function () {
$twig = new Twig($this->get('view'), $this);
// Here you can:
// $twig->addFilter(...)
// $twig->addExtension(...)
return $twig;
});
$di->setShared('mailer', function () {
return new Mailer($this->get('config')->toArray()['mail']);
});
$di->setShared('redis', function () {
return new Redis(new CacheFront(['lifetime' => 0]));
});
// Since it is registered as FQCN, it is auto aliased when calling `registerAliases()`.
$di->setShared('validation', Validation::class);
$di->setShared('jwt', function () {
$config = $this->get('config')->toArray()['apiAuth']['jwt'];
return new JWT($config['keys'], $config['algo'], $config['maxAge'], $config['leeway'], $config['passphrase']);
});
// Aliasing is totally optional. This helps you to leverage power of di->resolve()
// Alternatively the service name in DI can be used as the name of constructor params in classes
// to be resolved and that works without aliasing.
$di->registerAliases([
// Alias => Known service
View::class => 'view',
Twig::class => 'twig',
Mailer::class => 'mailer',
JWT::class => 'jwt',
// Some like to call it validator!
'validator' => 'validation',
]);
return $di;