1: <?php
2:
3: namespace Charcoal\App;
4:
5:
6: use Slim\Container;
7:
8:
9: use Pimple\ServiceProviderInterface;
10:
11:
12: use Charcoal\Factory\GenericFactory as Factory;
13:
14:
15: use Charcoal\Translator\ServiceProvider\TranslatorServiceProvider;
16:
17:
18: use Charcoal\View\ViewServiceProvider;
19:
20:
21: use Charcoal\App\AppConfig;
22: use Charcoal\App\ServiceProvider\AppServiceProvider;
23: use Charcoal\App\ServiceProvider\CacheServiceProvider;
24: use Charcoal\App\ServiceProvider\DatabaseServiceProvider;
25: use Charcoal\App\ServiceProvider\FilesystemServiceProvider;
26: use Charcoal\App\ServiceProvider\LoggerServiceProvider;
27:
28: 29: 30:
31: class AppContainer extends Container
32: {
33: 34: 35: 36: 37:
38: public function __construct(array $values = [])
39: {
40:
41: parent::__construct($values);
42:
43:
44: $this['config'] = (isset($values['config']) ? $values['config'] : new AppConfig());
45:
46: $this->register(new ViewServiceProvider());
47: $this->register(new AppServiceProvider());
48: $this->register(new CacheServiceProvider());
49: $this->register(new DatabaseServiceProvider());
50: $this->register(new FilesystemServiceProvider());
51: $this->register(new LoggerServiceProvider());
52: $this->register(new TranslatorServiceProvider());
53:
54: $this->registerProviderFactory();
55: $this->registerConfigProviders();
56: }
57:
58: 59: 60:
61: private function registerProviderFactory()
62: {
63: 64: 65:
66: if (!isset($this['provider/factory'])) {
67: $this['provider/factory'] = function () {
68: return new Factory([
69: 'base_class' => ServiceProviderInterface::class,
70: 'resolver_options' => [
71: 'suffix' => 'ServiceProvider'
72: ]
73: ]);
74: };
75: }
76: }
77:
78: 79: 80:
81: private function registerConfigProviders()
82: {
83: if (empty($this['config']['service_providers'])) {
84: return;
85: }
86:
87: $providers = $this['config']['service_providers'];
88:
89: foreach ($providers as $provider => $values) {
90: if (false === $values) {
91: continue;
92: }
93:
94: if (!is_array($values)) {
95: $values = [];
96: }
97:
98: $provider = $this['provider/factory']->create($provider);
99:
100: $this->register($provider, $values);
101: }
102: }
103: }
104: