1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebComplete\mvc; |
4
|
|
|
|
5
|
|
|
use DI\ContainerBuilder; |
6
|
|
|
use Monolog\ErrorHandler as MonologErrorHandler; |
7
|
|
|
use Monolog\Logger; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use WebComplete\core\cube\CubeManager; |
10
|
|
|
use WebComplete\core\utils\alias\AliasHelper; |
11
|
|
|
use WebComplete\core\utils\alias\AliasService; |
12
|
|
|
use WebComplete\core\utils\cache\Cache; |
13
|
|
|
use WebComplete\core\utils\cache\CacheService; |
14
|
|
|
use WebComplete\core\utils\hydrator\Hydrator; |
15
|
|
|
use WebComplete\core\utils\hydrator\HydratorInterface; |
16
|
|
|
use WebComplete\mvc\errorHandler\ErrorHandler; |
17
|
|
|
use WebComplete\core\utils\container\ContainerAdapter; |
18
|
|
|
use WebComplete\core\utils\container\ContainerInterface; |
19
|
|
|
use WebComplete\mvc\logger\LoggerService; |
20
|
|
|
use WebComplete\mvc\router\Router; |
21
|
|
|
|
22
|
|
|
class Application |
23
|
|
|
{ |
24
|
|
|
/** @var array */ |
25
|
|
|
protected $config; |
26
|
|
|
|
27
|
|
|
/** @var ContainerInterface */ |
28
|
|
|
protected $container; |
29
|
|
|
protected $errorHandler; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $config |
33
|
|
|
* @param bool $initErrorHandler |
34
|
|
|
* |
35
|
|
|
* @throws \Exception |
36
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
37
|
|
|
*/ |
38
|
|
|
public function __construct(array $config, $initErrorHandler = true) |
39
|
|
|
{ |
40
|
|
|
$this->config = $config; |
41
|
|
|
if ($initErrorHandler) { |
42
|
|
|
$this->initErrorHandler(); |
43
|
|
|
} |
44
|
|
|
$definitions = \array_merge( |
45
|
|
|
$this->getApplicationDefinitions(), |
46
|
|
|
$this->config['definitions'] ?? [] |
47
|
|
|
); |
48
|
|
|
$this->initContainer($definitions); |
49
|
|
|
$this->initCubes(); |
50
|
|
|
$this->bootstrapCubes(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws \Exception |
55
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
56
|
|
|
*/ |
57
|
|
|
protected function initCubes() |
58
|
|
|
{ |
59
|
|
|
$aliasService = $this->container->get(AliasService::class); |
60
|
|
|
AliasHelper::setInstance($aliasService); |
61
|
|
|
$cubeManager = $this->getContainer()->get(CubeManager::class); |
62
|
|
|
$cubesLocations = $this->config['cubesLocations'] ?? []; |
63
|
|
|
$cubesDefinitions = []; |
64
|
|
|
|
65
|
|
|
foreach ($cubesLocations as $location) { |
66
|
|
|
$cubeManager->registerAll($aliasService->get($location), $cubesDefinitions); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach ($cubesDefinitions as $def => $value) { |
70
|
|
|
$this->getContainer()->set($def, $value); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
*/ |
76
|
|
|
protected function bootstrapCubes() |
77
|
|
|
{ |
78
|
|
|
$cubeManager = $this->getContainer()->get(CubeManager::class); |
79
|
|
|
Cache::setCacheService($this->container->get(CacheService::class)); |
80
|
|
|
$commonLogger = $this->container->get(LoggerService::class)->get('*'); |
81
|
|
|
MonologErrorHandler::register($commonLogger, [], Logger::CRITICAL, Logger::EMERGENCY); |
82
|
|
|
$cubeManager->bootstrap($this->container); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
*/ |
87
|
|
|
protected function initErrorHandler() |
88
|
|
|
{ |
89
|
|
|
$this->errorHandler = new ErrorHandler(); |
90
|
|
|
$this->errorHandler->register(); |
91
|
|
|
$this->errorHandler->setErrorPagePath($this->config['errorPagePath'] ?? ''); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function getConfig(): array |
98
|
|
|
{ |
99
|
|
|
return $this->config; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return ContainerInterface |
104
|
|
|
*/ |
105
|
|
|
public function getContainer(): ContainerInterface |
106
|
|
|
{ |
107
|
|
|
return $this->container; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param ContainerInterface $container |
112
|
|
|
*/ |
113
|
|
|
public function setContainer(ContainerInterface $container) |
114
|
|
|
{ |
115
|
|
|
$this->container = $container; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $definitions |
120
|
|
|
* |
121
|
|
|
* @throws \DI\DependencyException |
122
|
|
|
* @throws \DI\NotFoundException |
123
|
|
|
* @throws \InvalidArgumentException |
124
|
|
|
*/ |
125
|
|
|
protected function initContainer($definitions) |
126
|
|
|
{ |
127
|
|
|
$definitions[ContainerInterface::class] = \DI\autowire(ContainerAdapter::class); |
128
|
|
|
$container = (new ContainerBuilder())->addDefinitions($definitions)->build(); |
129
|
|
|
$this->container = $container->get(ContainerInterface::class); |
130
|
|
|
$this->container->setContainer($container); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
* @throws \WebComplete\core\utils\alias\AliasException |
136
|
|
|
*/ |
137
|
|
|
protected function getApplicationDefinitions(): array |
138
|
|
|
{ |
139
|
|
|
$aliasService = new AliasService($this->config['aliases'] ?? []); |
140
|
|
|
$applicationConfig = new ApplicationConfig($this->config); |
141
|
|
|
$routes = $this->config['routes']; |
142
|
|
|
|
143
|
|
|
$definitions = [ |
144
|
|
|
ApplicationConfig::class => $applicationConfig, |
145
|
|
|
AliasService::class => $aliasService, |
146
|
|
|
Router::class => new Router($routes), |
147
|
|
|
Request::class => Request::createFromGlobals(), |
148
|
|
|
HydratorInterface::class => \DI\autowire(Hydrator::class), |
149
|
|
|
]; |
150
|
|
|
return $definitions; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|