|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebComplete\mvc; |
|
4
|
|
|
|
|
5
|
|
|
use DI\ContainerBuilder; |
|
6
|
|
|
use DI\Scope; |
|
7
|
|
|
use Monolog\ErrorHandler as MonologErrorHandler; |
|
8
|
|
|
use Monolog\Logger; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use WebComplete\core\cube\CubeManager; |
|
11
|
|
|
use WebComplete\core\utils\alias\AliasHelper; |
|
12
|
|
|
use WebComplete\core\utils\alias\AliasService; |
|
13
|
|
|
use WebComplete\core\utils\hydrator\Hydrator; |
|
14
|
|
|
use WebComplete\core\utils\hydrator\HydratorInterface; |
|
15
|
|
|
use WebComplete\mvc\errorHandler\ErrorHandler; |
|
16
|
|
|
use WebComplete\core\utils\container\ContainerAdapter; |
|
17
|
|
|
use WebComplete\core\utils\container\ContainerInterface; |
|
18
|
|
|
use WebComplete\mvc\logger\LoggerService; |
|
19
|
|
|
use WebComplete\mvc\router\Router; |
|
20
|
|
|
use WebComplete\mvc\view\View; |
|
21
|
|
|
use WebComplete\mvc\view\ViewInterface; |
|
22
|
|
|
|
|
23
|
|
|
class Application |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var array */ |
|
26
|
|
|
protected $config; |
|
27
|
|
|
|
|
28
|
|
|
/** @var ContainerInterface */ |
|
29
|
|
|
protected $container; |
|
30
|
|
|
protected $errorHandler; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array $config |
|
34
|
|
|
* @param bool $initErrorHandler |
|
35
|
|
|
* |
|
36
|
|
|
* @throws \Exception |
|
37
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(array $config, $initErrorHandler = true) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->config = $config; |
|
42
|
|
|
if ($initErrorHandler) { |
|
43
|
|
|
$this->initErrorHandler(); |
|
44
|
|
|
} |
|
45
|
|
|
$definitions = \array_merge( |
|
46
|
|
|
$this->getApplicationDefinitions(), |
|
47
|
|
|
$this->config['definitions'] ?? [] |
|
48
|
|
|
); |
|
49
|
|
|
$this->initContainer($definitions); |
|
50
|
|
|
$this->initCubes(); |
|
51
|
|
|
$this->bootstrapCubes(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @throws \Exception |
|
56
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function initCubes() |
|
59
|
|
|
{ |
|
60
|
|
|
$aliasService = $this->container->get(AliasService::class); |
|
61
|
|
|
AliasHelper::setInstance($aliasService); |
|
62
|
|
|
$cubeManager = $this->getContainer()->get(CubeManager::class); |
|
63
|
|
|
$cubesLocations = $this->config['cubesLocations'] ?? []; |
|
64
|
|
|
$cubesDefinitions = []; |
|
65
|
|
|
|
|
66
|
|
|
foreach ($cubesLocations as $location) { |
|
67
|
|
|
$cubeManager->registerAll($aliasService->get($location), $cubesDefinitions); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
foreach ($cubesDefinitions as $def => $value) { |
|
71
|
|
|
$this->getContainer()->set($def, $value); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function bootstrapCubes() |
|
78
|
|
|
{ |
|
79
|
|
|
$commonLogger = $this->container->get(LoggerService::class)->get('*'); |
|
80
|
|
|
MonologErrorHandler::register($commonLogger, [], Logger::CRITICAL, Logger::EMERGENCY); |
|
81
|
|
|
$cubeManager = $this->getContainer()->get(CubeManager::class); |
|
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\object(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
|
|
|
ViewInterface::class => \DI\object(View::class)->scope(Scope::PROTOTYPE), |
|
149
|
|
|
HydratorInterface::class => \DI\object(Hydrator::class), |
|
150
|
|
|
]; |
|
151
|
|
|
return $definitions; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|