1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bone\Mvc; |
4
|
|
|
|
5
|
|
|
use Barnacle\Container; |
6
|
|
|
use Barnacle\RegistrationInterface; |
7
|
|
|
use Bone\Mvc\Router\Decorator\ExceptionDecorator; |
8
|
|
|
use Bone\Mvc\Router\Decorator\NotAllowedDecorator; |
9
|
|
|
use Bone\Mvc\Router\Decorator\NotFoundDecorator; |
10
|
|
|
use Bone\Mvc\Router\PlatesStrategy; |
11
|
|
|
use Bone\Mvc\Router\RouterConfigInterface; |
12
|
|
|
use Bone\Mvc\View\Extension\Plates\Translate; |
13
|
|
|
use Bone\Mvc\View\Helper\Paginator; |
14
|
|
|
use Bone\Mvc\View\PlatesEngine; |
15
|
|
|
use Bone\Mvc\View\ViewRenderer; |
16
|
|
|
use Bone\Service\TranslatorFactory; |
17
|
|
|
use League\Route\Router; |
18
|
|
|
use Locale; |
19
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
20
|
|
|
use Zend\I18n\Translator\Translator; |
21
|
|
|
|
22
|
|
|
class ApplicationPackage implements RegistrationInterface |
23
|
|
|
{ |
24
|
|
|
/** @var array $config */ |
25
|
|
|
private $config; |
26
|
|
|
|
27
|
|
|
/** @var Router $router */ |
28
|
|
|
private $router; |
29
|
|
|
|
30
|
|
|
/** @var bool $i18nEnabledSite */ |
31
|
|
|
private $i18nEnabledSite = false; |
32
|
|
|
|
33
|
|
|
/** @var array $supportedLocales */ |
34
|
|
|
private $supportedLocales = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* ApplicationPackage constructor. |
38
|
|
|
* @param array $config |
39
|
|
|
* @param \League\Route\Router $router |
40
|
|
|
*/ |
41
|
|
|
public function __construct(array $config, Router $router) |
42
|
|
|
{ |
43
|
|
|
$this->config = $config; |
44
|
|
|
$this->router = $router; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param Container $c |
49
|
|
|
*/ |
50
|
|
|
public function addToContainer(Container $c) |
51
|
|
|
{ |
52
|
|
|
$this->setConfigArray($c); |
53
|
|
|
$this->setLocale($c); |
54
|
|
|
$this->setupPdoConnection($c); |
55
|
|
|
$this->setupViewEngine($c); |
56
|
|
|
$this->setupTranslator($c); |
57
|
|
|
$this->setupPaginator($c); |
58
|
|
|
$this->setupModules($c); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Container $c |
63
|
|
|
*/ |
64
|
|
|
private function setConfigArray(Container $c) |
65
|
|
|
{ |
66
|
|
|
foreach($this->config as $key => $value) |
67
|
|
|
{ |
68
|
|
|
$c[$key] = $value; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Container $c |
74
|
|
|
*/ |
75
|
|
|
private function setLocale(Container $c) |
76
|
|
|
{ |
77
|
|
|
$i18n = $c->get('i18n'); |
78
|
|
|
$this->i18nEnabledSite = $i18n['enabled']; |
79
|
|
|
$this->supportedLocales = $i18n['supported_locales']; |
80
|
|
|
$defaultLocale = $i18n['default_locale']; |
81
|
|
|
Locale::setDefault($defaultLocale); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Container $c |
86
|
|
|
*/ |
87
|
|
|
private function setSerializer(Container $c) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$c[SerializerInterface::class] = $serializer = SerializerBuilder::create()->build();; |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Container $c |
94
|
|
|
*/ |
95
|
|
|
private function setupViewEngine(Container $c) |
96
|
|
|
{ |
97
|
|
|
// set up the view engine dependencies |
98
|
|
|
$viewEngine = new PlatesEngine($c->get('viewFolder')); |
99
|
|
|
// $i18nExtension = new Translate(); |
100
|
|
|
// $viewEngine->loadExtension(); |
101
|
|
|
|
102
|
|
|
$c[PlatesEngine::class] = $viewEngine; |
103
|
|
|
|
104
|
|
View Code Duplication |
$c[NotFoundDecorator::class] = $c->factory(function (Container $c) { |
|
|
|
|
105
|
|
|
$layout = $c->get('default_layout'); |
106
|
|
|
$viewEngine = $c->get(PlatesEngine::class); |
107
|
|
|
$notFoundDecorator = new NotFoundDecorator($viewEngine); |
108
|
|
|
$notFoundDecorator->setLayout($layout); |
109
|
|
|
|
110
|
|
|
return $notFoundDecorator; |
111
|
|
|
}); |
112
|
|
|
|
113
|
|
View Code Duplication |
$c[NotAllowedDecorator::class] = $c->factory(function (Container $c) { |
|
|
|
|
114
|
|
|
$layout = $c->get('default_layout'); |
115
|
|
|
$viewEngine = $c->get(PlatesEngine::class); |
116
|
|
|
$notAllowedDecorator = new NotAllowedDecorator($viewEngine); |
117
|
|
|
$notAllowedDecorator->setLayout($layout); |
118
|
|
|
|
119
|
|
|
return $notAllowedDecorator; |
120
|
|
|
}); |
121
|
|
|
|
122
|
|
|
$c[ExceptionDecorator::class] = $c->factory(function (Container $c) { |
123
|
|
|
$viewEngine = $c->get(PlatesEngine::class); |
124
|
|
|
$notFoundDecorator = new ExceptionDecorator($viewEngine); |
125
|
|
|
|
126
|
|
|
return $notFoundDecorator; |
127
|
|
|
}); |
128
|
|
|
|
129
|
|
|
$c[PlatesStrategy::class] = $c->factory(function (Container $c) { |
130
|
|
|
$viewEngine = $c->get(PlatesEngine::class); |
131
|
|
|
$notFoundDecorator = $c->get(NotFoundDecorator::class); |
132
|
|
|
$notAllowedDecorator = $c->get(NotAllowedDecorator::class); |
133
|
|
|
$layout = $c->get('default_layout'); |
134
|
|
|
$strategy = new PlatesStrategy($viewEngine, $notFoundDecorator, $notAllowedDecorator, $layout); |
135
|
|
|
|
136
|
|
|
return $strategy; |
137
|
|
|
}); |
138
|
|
|
|
139
|
|
|
/** @var PlatesStrategy $strategy */ |
140
|
|
|
$strategy = $c->get(PlatesStrategy::class); |
141
|
|
|
$strategy->setContainer($c); |
142
|
|
|
|
143
|
|
|
if ($this->i18nEnabledSite === true) { |
144
|
|
|
$strategy->setI18nEnabled(true); |
145
|
|
|
$strategy->setSupportedLocales($this->supportedLocales); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$this->router->setStrategy($strategy); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param Container $c |
153
|
|
|
*/ |
154
|
|
|
private function setupModules(Container $c) |
155
|
|
|
{ |
156
|
|
|
// set up the modules and vendor package modules |
157
|
|
|
$packages = $c->get('packages'); |
158
|
|
|
|
159
|
|
|
foreach ($packages as $packageName) { |
160
|
|
|
if (class_exists($packageName)) { |
161
|
|
|
/** @var RegistrationInterface $package */ |
162
|
|
|
$package = new $packageName(); |
163
|
|
|
|
164
|
|
|
if ($package->hasEntityPath()) { |
165
|
|
|
$paths = $c['entity_paths']; |
166
|
|
|
$paths[] = $package->getEntityPath(); |
167
|
|
|
$c['entity_paths'] = $paths; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$package->addToContainer($c); |
171
|
|
|
|
172
|
|
|
if ($package instanceof RouterConfigInterface) { |
173
|
|
|
$package->addRoutes($c, $this->router); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param Container $c |
181
|
|
|
*/ |
182
|
|
|
private function setupTranslator(Container $c) |
183
|
|
|
{ |
184
|
|
|
$config = $c->get('i18n'); |
185
|
|
|
$engine = $c->get(PlatesEngine::class); |
186
|
|
|
if (is_array($config)) { |
187
|
|
|
$factory = new TranslatorFactory(); |
188
|
|
|
$translator = $factory->createTranslator($config); |
189
|
|
|
$c['translator'] = $translator; |
190
|
|
|
$engine->loadExtension(new Translate($translator)); |
191
|
|
|
$defaultLocale = $config['default_locale'] ?: 'en_GB'; |
192
|
|
|
// if (!in_array($locale, $config['supported_locales'])) { |
193
|
|
|
// $locale = $defaultLocale; |
194
|
|
|
// } |
195
|
|
|
$translator->setLocale($defaultLocale); |
196
|
|
|
$c[Translator::class] = $translator; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param Container $c |
202
|
|
|
*/ |
203
|
|
|
private function setupPaginator(Container $c) |
204
|
|
|
{ |
205
|
|
|
$c[Paginator::class] = $c->factory(function (Container $c, string $urlPart) { |
|
|
|
|
206
|
|
|
$config = $c->get('paginator'); |
207
|
|
|
$numPerPage = $config['num _per_page']; |
|
|
|
|
208
|
|
|
$pagerSize = $config['pager_size']; |
|
|
|
|
209
|
|
|
$urlPart = $config['url_part']; |
210
|
|
|
$pager = new Paginator(); |
211
|
|
|
$pager->setPagerSize(5); |
212
|
|
|
$pager->setUrlPart($urlPart); |
213
|
|
|
}); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param Container $c |
219
|
|
|
*/ |
220
|
|
|
private function setupPdoConnection(Container $c) |
221
|
|
|
{ |
222
|
|
|
// set up a db connection |
223
|
|
|
$c[PDO::class] = $c->factory(function(Container $c): PDO { |
224
|
|
|
$credentials = $c->get('db'); |
225
|
|
|
$host =$credentials['host']; |
226
|
|
|
$db = $credentials['database']; |
227
|
|
|
$user = $credentials['user']; |
228
|
|
|
$pass = $credentials['pass']; |
229
|
|
|
|
230
|
|
|
$dbConnection = new PDO('mysql:host='.$host.';dbname='.$db, $user, $pass, [ |
231
|
|
|
PDO::ATTR_EMULATE_PREPARES => false, |
232
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
233
|
|
|
]); |
234
|
|
|
|
235
|
|
|
return $dbConnection; |
236
|
|
|
}); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return string |
241
|
|
|
*/ |
242
|
|
|
function getEntityPath(): string |
243
|
|
|
{ |
244
|
|
|
return ''; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return bool |
249
|
|
|
*/ |
250
|
|
|
function hasEntityPath(): bool |
251
|
|
|
{ |
252
|
|
|
return false; |
253
|
|
|
} |
254
|
|
|
} |