1 | <?php |
||
20 | class Container |
||
21 | { |
||
22 | // The Dependency Injection Container |
||
23 | private $di = null; |
||
24 | |||
25 | // The only instance of the Container (Singleton) |
||
26 | private static $xInstance = null; |
||
27 | |||
28 | public static function getInstance() |
||
36 | |||
37 | private function __construct() |
||
45 | |||
46 | /** |
||
47 | * Set the parameters and create the objects in the dependency injection container |
||
48 | * |
||
49 | * @param string $sTranslationDir The translation directory |
||
50 | * @param string $sTemplateDir The template directory |
||
51 | * |
||
52 | * @return void |
||
53 | */ |
||
54 | private function init($sTranslationDir, $sTemplateDir) |
||
55 | { |
||
56 | /* |
||
57 | * Parameters |
||
58 | */ |
||
59 | // Translation directory |
||
60 | $this->di['translation_dir'] = $sTranslationDir; |
||
61 | // Template directory |
||
62 | $this->di['template_dir'] = $sTemplateDir; |
||
63 | |||
64 | /* |
||
65 | * Managers |
||
66 | */ |
||
67 | // Plugin Manager |
||
68 | $this->di['plugin_manager'] = function ($c) { |
||
69 | return new \Jaxon\Plugin\Manager(); |
||
70 | }; |
||
71 | // Request Manager |
||
72 | $this->di['request_manager'] = function ($c) { |
||
73 | return new \Jaxon\Request\Manager(); |
||
74 | }; |
||
75 | // Request Factory |
||
76 | $this->di['request_factory'] = function ($c) { |
||
77 | return new \Jaxon\Request\Factory(); |
||
78 | }; |
||
79 | // Response Manager |
||
80 | $this->di['response_manager'] = function ($c) { |
||
81 | return new \Jaxon\Response\Manager(); |
||
82 | }; |
||
83 | |||
84 | /* |
||
85 | * Services |
||
86 | */ |
||
87 | // Config manager |
||
88 | $this->di['config'] = function ($c) { |
||
89 | return new Config\Config(); |
||
90 | }; |
||
91 | // Minifier |
||
92 | $this->di['minifier'] = function ($c) { |
||
93 | return new Template\Minifier(); |
||
94 | }; |
||
95 | // Translator |
||
96 | $this->di['translator'] = function ($c) { |
||
97 | return new Translation\Translator($c['translation_dir'], $c['config']); |
||
98 | }; |
||
99 | // Template engine |
||
100 | $this->di['template'] = function ($c) { |
||
101 | return new Template\Template($c['template_dir']); |
||
102 | }; |
||
103 | // Validator |
||
104 | $this->di['validator'] = function ($c) { |
||
105 | return new Validation\Validator($c['translator'], $c['config']); |
||
106 | }; |
||
107 | // Pagination Renderer |
||
108 | $this->di['pagination.renderer'] = function ($c) { |
||
109 | return new Pagination\Renderer(); |
||
110 | }; |
||
111 | // Pagination Paginator |
||
112 | $this->di['pagination.paginator'] = function ($c) { |
||
113 | return new Pagination\Paginator($c['pagination.renderer']); |
||
114 | }; |
||
115 | // Event Dispatcher |
||
116 | $this->di['events'] = function ($c) { |
||
117 | return new EventDispatcher(); |
||
118 | }; |
||
119 | |||
120 | /* |
||
121 | * Core library objects |
||
122 | */ |
||
123 | // Global Response |
||
124 | $this->di['response'] = function ($c) { |
||
125 | return new \Jaxon\Response\Response(); |
||
126 | }; |
||
127 | // Jaxon Core |
||
128 | $this->di['jaxon'] = function ($c) { |
||
129 | return new \Jaxon\Jaxon(); |
||
130 | }; |
||
131 | // View Renderer Facade |
||
132 | $this->di['sentry.view.renderer'] = function ($c) { |
||
133 | $aRenderers = $c['view.data.renderers']; |
||
134 | $sDefaultNamespace = $c['view.data.namespace.default']; |
||
135 | return new \Jaxon\Sentry\View\Facade($aRenderers, $sDefaultNamespace); |
||
136 | }; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Get the plugin manager |
||
141 | * |
||
142 | * @return object The plugin manager |
||
143 | */ |
||
144 | public function getPluginManager() |
||
145 | { |
||
146 | return $this->di['plugin_manager']; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Get the request manager |
||
151 | * |
||
152 | * @return object The request manager |
||
153 | */ |
||
154 | public function getRequestManager() |
||
155 | { |
||
156 | return $this->di['request_manager']; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get the request factory |
||
161 | * |
||
162 | * @return object The request factory |
||
163 | */ |
||
164 | public function getRequestFactory() |
||
165 | { |
||
166 | return $this->di['request_factory']; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Get the response manager |
||
171 | * |
||
172 | * @return object The response manager |
||
173 | */ |
||
174 | public function getResponseManager() |
||
175 | { |
||
176 | return $this->di['response_manager']; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Get the config manager |
||
181 | * |
||
182 | * @return Jaxon\Utils\Config\Config The config manager |
||
183 | */ |
||
184 | public function getConfig() |
||
185 | { |
||
186 | return $this->di['config']; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Create a new the config manager |
||
191 | * |
||
192 | * @return Jaxon\Utils\Config\Config The config manager |
||
193 | */ |
||
194 | public function newConfig() |
||
195 | { |
||
196 | return new Config\Config(); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Get the minifier |
||
201 | * |
||
202 | * @return object The minifier |
||
203 | */ |
||
204 | public function getMinifier() |
||
205 | { |
||
206 | return $this->di['minifier']; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Get the translator |
||
211 | * |
||
212 | * @return object The translator |
||
213 | */ |
||
214 | public function getTranslator() |
||
215 | { |
||
216 | return $this->di['translator']; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Get the template engine |
||
221 | * |
||
222 | * @return object The template engine |
||
223 | */ |
||
224 | public function getTemplate() |
||
225 | { |
||
226 | return $this->di['template']; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Get the validator |
||
231 | * |
||
232 | * @return object The validator |
||
233 | */ |
||
234 | public function getValidator() |
||
235 | { |
||
236 | return $this->di['validator']; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Get the paginator |
||
241 | * |
||
242 | * @return object The paginator |
||
243 | */ |
||
244 | public function getPaginator() |
||
245 | { |
||
246 | return $this->di['pagination.paginator']; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Set the pagination renderer |
||
251 | * |
||
252 | * @param object $xRenderer The pagination renderer |
||
253 | * |
||
254 | * @return void |
||
255 | */ |
||
256 | public function setPaginationRenderer($xRenderer) |
||
257 | { |
||
258 | $this->di['pagination.renderer'] = $xRenderer; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Get the event dispatcher |
||
263 | * |
||
264 | * @return object The event dispatcher |
||
265 | */ |
||
266 | public function getEventDispatcher() |
||
267 | { |
||
268 | return $this->di['events']; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Get the Global Response object |
||
273 | * |
||
274 | * @return object The Global Response object |
||
275 | */ |
||
276 | public function getResponse() |
||
277 | { |
||
278 | return $this->di['response']; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Create a new Jaxon response object |
||
283 | * |
||
284 | * @return \Jaxon\Response\Response The new Jaxon response object |
||
285 | */ |
||
286 | public function newResponse() |
||
287 | { |
||
288 | return new \Jaxon\Response\Response(); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Get the main Jaxon object |
||
293 | * |
||
294 | * @return object The Jaxon object |
||
295 | */ |
||
296 | public function getJaxon() |
||
297 | { |
||
298 | return $this->di['jaxon']; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Get the Jaxon library version number |
||
303 | * |
||
304 | * @return string The version number |
||
305 | */ |
||
306 | public function getVersion() |
||
307 | { |
||
308 | return $this->getJaxon()->getVersion(); |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Get the Sentry instance |
||
313 | * |
||
314 | * @return object The Sentry instance |
||
315 | */ |
||
316 | public function getSentry() |
||
317 | { |
||
318 | return $this->di['sentry']; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Set the Sentry instance |
||
323 | * |
||
324 | * @param object $xSentry The Sentry instance |
||
325 | * |
||
326 | * @return void |
||
327 | */ |
||
328 | public function setSentry($xSentry) |
||
329 | { |
||
330 | $this->di['sentry'] = $xSentry; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Get the Armada instance |
||
335 | * |
||
336 | * @return object The Armada instance |
||
337 | */ |
||
338 | public function getArmada() |
||
342 | |||
343 | /** |
||
344 | * Set the Armada instance |
||
345 | * |
||
346 | * @param object $xArmada The Armada instance |
||
347 | * |
||
348 | * @return void |
||
349 | */ |
||
350 | public function setArmada($xArmada) |
||
354 | |||
355 | /** |
||
356 | * Set the view renderers data |
||
357 | * |
||
358 | * @param array $aRenderers Array of renderer names with namespace as key |
||
359 | * |
||
360 | * @return void |
||
361 | */ |
||
362 | public function initViewRenderers($aRenderers) |
||
366 | |||
367 | /** |
||
368 | * Set the view namespaces data |
||
369 | * |
||
370 | * @param array $aNamespaces Array of namespaces with renderer name as key |
||
371 | * |
||
372 | * @return void |
||
373 | */ |
||
374 | public function initViewNamespaces($aNamespaces, $sDefaultNamespace) |
||
379 | |||
380 | /** |
||
381 | * Add a view renderer |
||
382 | * |
||
383 | * @param string $sId The unique identifier of the view renderer |
||
384 | * @param Closure $xClosure A closure to create the view instance |
||
385 | * |
||
386 | * @return void |
||
387 | */ |
||
388 | public function addViewRenderer($sId, $xClosure) |
||
409 | |||
410 | /** |
||
411 | * Get the view object |
||
412 | * |
||
413 | * @param string $sId The unique identifier of the view renderer |
||
414 | * |
||
415 | * @return object The view object |
||
416 | */ |
||
417 | public function getViewRenderer($sId = '') |
||
427 | |||
428 | /** |
||
429 | * Get the session object |
||
430 | * |
||
431 | * @return object The session object |
||
432 | */ |
||
433 | public function getSessionManager() |
||
437 | |||
438 | /** |
||
439 | * Set the session |
||
440 | * |
||
441 | * @param Closure $xClosure A closure to create the session instance |
||
442 | * |
||
443 | * @return void |
||
444 | */ |
||
445 | public function setSessionManager($xClosure) |
||
449 | } |
||
450 |