|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebComplete\mvc; |
|
4
|
|
|
|
|
5
|
|
|
use DI\ContainerBuilder; |
|
6
|
|
|
use DI\Scope; |
|
7
|
|
|
use Symfony\Component\Cache\Simple\FilesystemCache; |
|
8
|
|
|
use Symfony\Component\Cache\Simple\NullCache; |
|
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\container\ContainerAdapter; |
|
14
|
|
|
use WebComplete\core\utils\container\ContainerInterface; |
|
15
|
|
|
use WebComplete\core\utils\helpers\ClassHelper; |
|
16
|
|
|
use WebComplete\mvc\errorHandler\ErrorHandler; |
|
17
|
|
|
use WebComplete\mvc\router\Router; |
|
18
|
|
|
use WebComplete\mvc\view\View; |
|
19
|
|
|
use WebComplete\mvc\view\ViewInterface; |
|
20
|
|
|
|
|
21
|
|
|
class Application |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var array */ |
|
24
|
|
|
protected $config; |
|
25
|
|
|
|
|
26
|
|
|
/** @var ContainerInterface */ |
|
27
|
|
|
protected $container; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param array $config |
|
31
|
|
|
* @param bool $initErrorHandler |
|
32
|
|
|
* |
|
33
|
|
|
* @throws \Exception |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(array $config, $initErrorHandler = true) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->config = $config; |
|
38
|
|
|
if ($initErrorHandler) { |
|
39
|
|
|
$this->initErrorHandler(); |
|
40
|
|
|
} |
|
41
|
|
|
$definitions = \array_merge( |
|
42
|
|
|
$this->init(), |
|
43
|
|
|
$this->config['definitions'] ?? [] |
|
44
|
|
|
); |
|
45
|
|
|
$this->initContainer($definitions); |
|
46
|
|
|
$this->afterInit(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function initErrorHandler() |
|
52
|
|
|
{ |
|
53
|
|
|
$errorHandler = new ErrorHandler(); |
|
54
|
|
|
$errorHandler->register(); |
|
55
|
|
|
$errorHandler->setErrorPagePath($this->config['errorPagePath'] ?? ''); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return array |
|
60
|
|
|
* @throws \Exception |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function init(): array |
|
63
|
|
|
{ |
|
64
|
|
|
$aliasService = new AliasService($this->config['aliases'] ?? []); |
|
65
|
|
|
$definitions = [ |
|
66
|
|
|
AliasService::class => $aliasService, |
|
67
|
|
|
Router::class => new Router($this->config['routes'] ?? []), |
|
68
|
|
|
Request::class => Request::createFromGlobals(), |
|
69
|
|
|
ViewInterface::class => \DI\object(View::class)->scope(Scope::PROTOTYPE) |
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
|
|
$pmCache = \ENV === 'dev' ? new NullCache() : new FilesystemCache(); |
|
|
|
|
|
|
73
|
|
|
$cubeManager = new CubeManager(new ClassHelper(), $pmCache); |
|
74
|
|
|
$cubesLocations = $this->config['cubesLocations'] ?? []; |
|
75
|
|
|
foreach ($cubesLocations as $location) { |
|
76
|
|
|
$cubeManager->registerAll($aliasService->get($location), $definitions); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $definitions; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function afterInit() |
|
83
|
|
|
{ |
|
84
|
|
|
AliasHelper::setInstance($this->container->get(AliasService::class)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getConfig(): array |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->config; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return ContainerInterface |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getContainer(): ContainerInterface |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->container; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param ContainerInterface $container |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setContainer(ContainerInterface $container) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->container = $container; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param $definitions |
|
113
|
|
|
* |
|
114
|
|
|
* @throws \DI\DependencyException |
|
115
|
|
|
* @throws \DI\NotFoundException |
|
116
|
|
|
* @throws \InvalidArgumentException |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function initContainer($definitions) |
|
119
|
|
|
{ |
|
120
|
|
|
$definitions[ContainerInterface::class] = \DI\object(ContainerAdapter::class); |
|
121
|
|
|
$container = (new ContainerBuilder())->addDefinitions($definitions)->build(); |
|
122
|
|
|
$this->container = $container->get(ContainerInterface::class); |
|
123
|
|
|
$this->container->setContainer($container); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|