-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.php
More file actions
186 lines (169 loc) · 5.86 KB
/
App.php
File metadata and controls
186 lines (169 loc) · 5.86 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace TuxBoy;
use Cake\Database\Connection;
use Cake\Datasource\ConnectionManager;
use DI\ContainerBuilder;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Exception;
use Go\Core\AspectKernel;
use GuzzleHttp\Psr7\Response;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use TuxBoy\Builder\Builder;
use TuxBoy\Exception\NotMatchRouteException;
use TuxBoy\Handler\HandlerInterface;
use TuxBoy\Plugin\Plugin;
use TuxBoy\Router\Router;
/**
* Class App.
*/
class App
{
/**
* @var ContainerInterface
*/
public $container;
/**
* @var Router
*/
public $router;
/**
* @var Application[]
*/
private $applications;
/**
* @param array $custom_config
* @param array $applications
*/
public function __construct(array $custom_config = [], array $applications = [])
{
$this->initApplications($applications);
$this->container_builder = new ContainerBuilder();
$default_config = require __DIR__ . '/config.php';
$this->container_builder->addDefinitions($default_config[Priority::APP]);
$this->addPluginConfig($custom_config[Priority::PLUGIN], $default_config[Priority::PLUGIN]);
$this->addCoreConfig($custom_config[Priority::CORE], $default_config[Priority::CORE]);
$this->initApplicationConfig();
if (!empty($custom_config) && !empty($custom_config[Priority::APP])) {
$this->container_builder->addDefinitions($custom_config[Priority::APP]);
}
$this->container = $this->container_builder->build();
$kernel = $this->container->get(AspectKernel::class);
// Active le handle pour afficher les erreurs si on est en mode dev
if ($this->container->get('environement') === Environment::DEV) {
$this->container->get(HandlerInterface::class)->handle();
}
foreach ($this->getContainer()->get('annotations') as $annotation) {
AnnotationRegistry::loadAnnotationClass($annotation);
}
// Il faudra réfactorer la partie GoAOP dans PHP-DI
$aspectContainer = $kernel->getContainer();
$aspects = $this->getContainer()->get('goaop.aspect');
foreach ($aspects as $aspect) {
$aspectContainer->registerAspect($aspect);
}
ConnectionManager::setConfig('default', [
'className' => Connection::class,
'driver' => 'Cake\Database\Driver\Mysql',
'database' => $this->getContainer()->get('db.name'),
'username' => $this->getContainer()->get('db.user'),
'password' => $this->getContainer()->get('db.pass'),
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => false // If set to `true` you need to install the optional "cakephp/cache" package.
]);
$this->initApplicationsRoutes($this->getContainer()->get(Router::class));
}
/**
* Ajoute la définition des DI de l'application.
*/
private function initApplicationConfig(): void
{
foreach ($this->applications as $application) {
$this->container_builder->addDefinitions($application->addConfig());
}
}
/**
* Ajoute les routes définies dans l'application.
*
* @param $router Router
*/
private function initApplicationsRoutes(Router $router): void
{
foreach ($this->applications as $application) {
$application->getRoutes($router);
}
}
/**
* Initialisize les nouvelles applications.
*
* @param array $applications
*/
private function initApplications(array $applications = []): void
{
foreach ($applications as $application) {
/* @var $application Application */
$application = Builder::create($application);
$this->applications[] = $application;
}
}
/**
* @param array $custom_config
* @param array $default_config
*/
public function addPluginConfig(array $custom_config = [], array $default_config = []): void
{
if (!empty($custom_config)) {
$custom_config = array_merge($default_config, $custom_config);
Plugin::current()->addPlugin($custom_config);
} else {
Plugin::current()->addPlugin($default_config);
}
}
/**
* @param array $config
* @param array $default_config
*/
public function addCoreConfig(array $config, array $default_config): void
{
if (!empty($config)) {
$config = array_merge($config, $default_config);
Plugin::current()->addBuilder(Priority::CORE, $config);
} else {
Plugin::current()->addBuilder(Priority::CORE, $default_config);
}
}
/**
* @todo Cette méthode a besoin d'un bon réfactoring
*
* @param ServerRequestInterface $request
*
* @throws Exception
*
* @return mixed|ResponseInterface
*/
public function run(ServerRequestInterface $request): ResponseInterface
{
$router = $this->container->get(Router::class);
$route = $router->match($request);
if (null === $route) {
throw new NotMatchRouteException();
}
$parameters = array_merge($route->getParams(), ['request' => $request]);
$response = $this->container->call($route->getCallback(), $parameters);
if (is_string($response)) {
return new Response(200, [], $response);
} elseif ($response instanceof ResponseInterface) {
return $response;
}
throw new Exception('The response is not a string or an instance of ResponseInterface');
}
/**
* @return ContainerInterface
*/
public function getContainer(): ContainerInterface
{
return $this->container;
}
}