1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bone; |
4
|
|
|
|
5
|
|
|
use Barnacle\Container; |
6
|
|
|
use Barnacle\RegistrationInterface; |
7
|
|
|
use Bone\Db\DbPackage; |
8
|
|
|
use Bone\Firewall\FirewallPackage; |
9
|
|
|
use Bone\Http\Middleware\Stack; |
10
|
|
|
use Bone\Http\MiddlewareAwareInterface; |
11
|
|
|
use Bone\I18n\I18nPackage; |
12
|
|
|
use Bone\I18n\I18nRegistrationInterface; |
13
|
|
|
use Bone\Log\LogPackage; |
14
|
|
|
use Bone\Controller\DownloadController; |
15
|
|
|
use Bone\Router\Router; |
16
|
|
|
use Bone\Router\RouterConfigInterface; |
17
|
|
|
use Bone\View\ViewEngine; |
18
|
|
|
use Bone\I18n\Service\TranslatorFactory; |
19
|
|
|
use Bone\View\ViewPackage; |
20
|
|
|
use League\Plates\Template\Folders; |
21
|
|
|
use League\Route\Strategy\JsonStrategy; |
22
|
|
|
use Laminas\Diactoros\ResponseFactory; |
23
|
|
|
use Laminas\I18n\Translator\Translator; |
24
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
25
|
|
|
|
26
|
|
|
class ApplicationPackage implements RegistrationInterface |
27
|
|
|
{ |
28
|
|
|
/** @var array $config */ |
29
|
|
|
private $config; |
30
|
|
|
|
31
|
|
|
/** @var Router $router */ |
32
|
|
|
private $router; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* ApplicationPackage constructor. |
36
|
|
|
* @param array $config |
37
|
|
|
* @param Router $router |
38
|
|
|
*/ |
39
|
9 |
|
public function __construct(array $config, Router $router) |
40
|
|
|
{ |
41
|
9 |
|
$this->config = $config; |
42
|
9 |
|
$this->router = $router; |
43
|
9 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Container $c |
47
|
|
|
* @throws \Bone\Exception |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
9 |
|
public function addToContainer(Container $c) |
51
|
|
|
{ |
52
|
9 |
|
$this->setConfigArray($c); |
53
|
9 |
|
$this->setupLogs($c); |
54
|
9 |
|
$this->setupPdoConnection($c); |
55
|
9 |
|
$this->setupViewEngine($c); |
56
|
9 |
|
$this->initMiddlewareStack($c); |
57
|
9 |
|
$this->setupTranslator($c); |
58
|
9 |
|
$this->setupPackages($c); |
59
|
9 |
|
$this->setupVendorViewOverrides($c); |
60
|
9 |
|
$this->setupDownloadController($c); |
61
|
9 |
|
$this->setupRouteFirewall($c); |
62
|
9 |
|
$this->setupMiddlewareStack($c); |
63
|
9 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Container $c |
67
|
|
|
*/ |
68
|
9 |
|
private function setConfigArray(Container $c) |
69
|
|
|
{ |
70
|
9 |
|
foreach ($this->config as $key => $value) { |
71
|
9 |
|
$c[$key] = $value; |
72
|
|
|
} |
73
|
9 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Container $c |
77
|
|
|
*/ |
78
|
9 |
|
private function setupViewEngine(Container $c) |
79
|
|
|
{ |
80
|
9 |
|
$package = new ViewPackage(); |
81
|
9 |
|
$package->addToContainer($c); |
82
|
9 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Container $c |
86
|
|
|
*/ |
87
|
9 |
|
private function setupPackages(Container $c) |
88
|
|
|
{ |
89
|
|
|
// set up the modules and vendor package modules |
90
|
9 |
|
$packages = $c->get('packages'); |
91
|
9 |
|
$i18n = $c->get('i18n'); |
92
|
|
|
/** @var Translator $translator */ |
93
|
9 |
|
$translator = $c->get(Translator::class); |
94
|
|
|
|
95
|
9 |
|
foreach ($packages as $packageName) { |
96
|
9 |
|
if (class_exists($packageName)) { |
97
|
|
|
/** @var RegistrationInterface $package */ |
98
|
9 |
|
$package = new $packageName(); |
99
|
|
|
|
100
|
9 |
|
if ($package->hasEntityPath()) { |
101
|
9 |
|
$paths = $c['entity_paths']; |
102
|
9 |
|
$paths[] = $package->getEntityPath(); |
103
|
9 |
|
$c['entity_paths'] = $paths; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
9 |
|
reset($packages); |
108
|
9 |
|
foreach ($packages as $packageName) { |
109
|
9 |
|
if (class_exists($packageName)) { |
110
|
|
|
/** @var RegistrationInterface $package */ |
111
|
9 |
|
$package = new $packageName(); |
112
|
9 |
|
$package->addToContainer($c); |
113
|
|
|
|
114
|
9 |
|
if ($package instanceof RouterConfigInterface) { |
115
|
9 |
|
$package->addRoutes($c, $this->router); |
116
|
|
|
} |
117
|
|
|
|
118
|
9 |
|
if ($package instanceof I18nRegistrationInterface) { |
119
|
9 |
|
foreach ($i18n['supported_locales'] as $locale) { |
120
|
9 |
|
$factory = new TranslatorFactory(); |
121
|
9 |
|
$factory->addPackageTranslations($translator, $package, $locale); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
9 |
|
if ($package instanceof MiddlewareAwareInterface) { |
126
|
|
|
$stack = $c->get(Stack::class); |
127
|
|
|
$package->addMiddleware($stack, $c); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
9 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param Container $c |
135
|
|
|
* @throws \Bone\Exception |
136
|
|
|
*/ |
137
|
9 |
|
private function setupTranslator(Container $c) |
138
|
|
|
{ |
139
|
9 |
|
$package = new I18nPackage(); |
140
|
9 |
|
$package->addToContainer($c); |
141
|
9 |
|
$package->addMiddleware($c->get(Stack::class), $c); |
142
|
9 |
|
} |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param Container $c |
147
|
|
|
* @throws \Bone\Exception |
148
|
|
|
*/ |
149
|
9 |
|
private function setupPdoConnection(Container $c) |
150
|
|
|
{ |
151
|
9 |
|
$package = new DbPackage(); |
152
|
9 |
|
$package->addToContainer($c); |
153
|
9 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param Container $c |
157
|
|
|
*/ |
158
|
9 |
|
private function setupDownloadController(Container $c): void |
159
|
|
|
{ |
160
|
9 |
|
$uploadDirectory = $c->get('uploads_dir'); |
161
|
9 |
|
$c[DownloadController::class] = new DownloadController($uploadDirectory); |
162
|
9 |
|
$strategy = new JsonStrategy(new ResponseFactory()); |
163
|
9 |
|
$strategy->setContainer($c); |
164
|
9 |
|
$this->router->map('GET', '/download', [DownloadController::class, 'downloadAction'])->setStrategy($strategy); |
165
|
9 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param Container $c |
169
|
|
|
*/ |
170
|
9 |
|
private function setupRouteFirewall(Container $c): void |
171
|
|
|
{ |
172
|
9 |
|
$pckage = new FirewallPackage(); |
173
|
9 |
|
$pckage->addToContainer($c); |
174
|
9 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param Container $c |
178
|
|
|
* @throws \Exception |
179
|
|
|
*/ |
180
|
9 |
|
private function setupLogs(Container $c) |
181
|
|
|
{ |
182
|
9 |
|
$package = new LogPackage(); |
183
|
9 |
|
$package->addToContainer($c); |
184
|
9 |
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param Container $c |
188
|
|
|
*/ |
189
|
9 |
|
private function setupVendorViewOverrides(Container $c): void |
190
|
|
|
{ |
191
|
|
|
/** @var ViewEngine $viewEngine */ |
192
|
9 |
|
$viewEngine = $c->get(ViewEngine::class); |
193
|
9 |
|
$views = $c->get('views'); |
194
|
9 |
|
$registeredViews = $viewEngine->getFolders(); |
195
|
|
|
|
196
|
9 |
|
foreach ($views as $view => $folder) { |
197
|
9 |
|
$this->overrideViewFolder($view, $folder, $registeredViews); |
198
|
|
|
} |
199
|
9 |
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $view |
203
|
|
|
* @param string $folder |
204
|
|
|
* @param array $registeredViews |
205
|
|
|
* @param ViewEngine $viewEngine |
|
|
|
|
206
|
|
|
*/ |
207
|
9 |
|
private function overrideViewFolder(string $view, string $folder, Folders $registeredViews): void |
208
|
|
|
{ |
209
|
9 |
|
if ($registeredViews->exists($view)) { |
210
|
|
|
/** @var \League\Plates\Template\Folder $currentFolder */ |
211
|
1 |
|
$currentFolder = $registeredViews->get($view); |
212
|
1 |
|
$currentFolder->setPath($folder); |
213
|
|
|
} |
214
|
9 |
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
1 |
|
public function getEntityPath(): string |
220
|
|
|
{ |
221
|
1 |
|
return ''; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return bool |
226
|
|
|
*/ |
227
|
1 |
|
public function hasEntityPath(): bool |
228
|
|
|
{ |
229
|
1 |
|
return false; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param Container $c |
234
|
|
|
*/ |
235
|
9 |
|
private function initMiddlewareStack(Container $c): void |
236
|
|
|
{ |
237
|
9 |
|
$router = $c->get(Router::class); |
238
|
9 |
|
$c[Stack::class] = new Stack($router); |
239
|
9 |
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param Container $c |
243
|
|
|
*/ |
244
|
9 |
|
private function setupMiddlewareStack(Container $c): void |
245
|
|
|
{ |
246
|
9 |
|
$stack = $c->get(Stack::class); |
247
|
9 |
|
$middlewareStack = $c->has('stack') ? $c->get('stack') : []; |
248
|
|
|
|
249
|
9 |
|
foreach ($middlewareStack as $middleware) { |
250
|
|
|
if ($middleware instanceof MiddlewareInterface) { |
251
|
|
|
$stack->addMiddleWare($middleware); |
252
|
|
|
} elseif ($c->has($middleware)) { |
253
|
|
|
$stack->addMiddleWare($c->get($middleware)); |
254
|
|
|
} else { |
255
|
|
|
$stack->addMiddleWare(new $middleware()); |
256
|
|
|
} |
257
|
|
|
} |
258
|
9 |
|
} |
259
|
|
|
} |
260
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.