1: <?php
2:
3: namespace Charcoal\Cms\ServiceProvider;
4:
5:
6: use Charcoal\Cms\Service\Loader\EventLoader;
7: use Charcoal\Cms\Service\Loader\NewsLoader;
8: use Charcoal\Cms\Service\Manager\EventManager;
9: use Charcoal\Cms\Service\Manager\NewsManager;
10: use Charcoal\Cms\Support\Helpers\DateHelper;
11: use Pimple\Container;
12: use Pimple\ServiceProviderInterface;
13:
14:
15: use Charcoal\Cms\Config;
16:
17:
18: use Charcoal\Cms\Service\Loader\SectionLoader;
19:
20:
21: use Charcoal\Model\AbstractModel;
22:
23:
24: use Charcoal\Cms\SectionInterface;
25: use Charcoal\Cms\Config\CmsConfig;
26:
27:
28: use Charcoal\Factory\GenericFactory;
29:
30: 31: 32: 33: 34: 35: 36:
37: class CmsServiceProvider implements ServiceProviderInterface
38: {
39: 40: 41: 42: 43: 44: 45: 46: 47:
48: public function register(Container $container)
49: {
50: 51: 52: 53:
54: $container['cms/config'] = function (Container $container) {
55: $appConfig = $container['config'];
56: $cms = $appConfig->get('cms');
57:
58: $cmsConfig = new CmsConfig();
59:
60: $cmsConfig->addFile(__DIR__.'/../../../../config/cms.json');
61: $cmsConfig->setData($cms);
62:
63: $config = $cmsConfig->get('config_obj');
64:
65: if ($config) {
66: $factory = $container['model/factory'];
67:
68: $configId = $cmsConfig->get('config_obj_id') ?: 1;
69:
70: $model = $factory
71: ->create($config)
72: ->load($configId);
73:
74: if (!!$model->id()) {
75: $cmsConfig->addModel($model);
76: }
77: }
78:
79: return $cmsConfig;
80: };
81:
82: 83: 84: 85:
86: $container['date/helper'] = function (Container $container) {
87:
88: trigger_error(sprintf(
89: '%s is deprecated, use %s instead',
90: '$container[\'date/helper\']',
91: '$container[\'date/helper\']'
92: ));
93:
94: return new DateHelper([
95: 'date_formats' => $container['cms/config']->get('date_formats'),
96: 'time_formats' => $container['cms/config']->get('time_formats'),
97: 'translator' => $container['translator']
98: ]);
99: };
100:
101: 102: 103: 104:
105: $container['cms/date/helper'] = function (Container $container) {
106:
107: return new DateHelper([
108: 'date_formats' => $container['cms/config']->get('date_formats'),
109: 'time_formats' => $container['cms/config']->get('time_formats'),
110: 'translator' => $container['translator']
111: ]);
112: };
113:
114: $this->registerSectionServices($container);
115: $this->registerNewsServices($container);
116: $this->registerEventServices($container);
117: }
118:
119: 120: 121: 122:
123: private function registerSectionServices(Container $container)
124: {
125: 126: 127: 128:
129: $container['cms/section/factory'] = function (Container $container) {
130: return new GenericFactory([
131: 'base_class' => SectionInterface::class,
132: 'arguments' => $container['model/factory']->arguments(),
133: 'resolver_options' => [
134: 'suffix' => 'Section'
135: ]
136: ]);
137: };
138:
139: 140: 141: 142:
143: $container['cms/section/loader'] = function (Container $container) {
144: $sectionLoader = new SectionLoader([
145: 'loader' => $container['model/collection/loader'],
146: 'factory' => $container['model/factory'],
147: 'cache' => $container['cache'],
148: 'translator' => $container['translator']
149: ]);
150:
151:
152: $sectionConfig = $container['cms/config']->sectionConfig();
153:
154: $sectionLoader->setObjType($sectionConfig->get('objType'));
155: $sectionLoader->setBaseSection($sectionConfig->get('baseSection'));
156: $sectionLoader->setSectionTypes($sectionConfig->get('sectionTypes'));
157:
158: return $sectionLoader;
159: };
160: }
161:
162: 163: 164: 165:
166: private function registerNewsServices(Container $container)
167: {
168: 169: 170: 171:
172: $container['cms/news/loader'] = function (Container $container) {
173: $newsLoader = new NewsLoader([
174: 'loader' => $container['model/collection/loader'],
175: 'factory' => $container['model/factory'],
176: 'cache' => $container['cache'],
177: 'translator' => $container['translator']
178: ]);
179:
180: $newsConfig = $container['cms/config']->newsConfig();
181:
182:
183: $objType = $newsConfig->get('obj_type');
184: $newsLoader->setObjType($objType);
185:
186: return $newsLoader;
187: };
188:
189: 190: 191: 192:
193: $container['cms/news/manager'] = function (Container $container) {
194:
195: $newsManager = new NewsManager([
196: 'loader' => $container['model/collection/loader'],
197: 'factory' => $container['model/factory'],
198: 'news/loader' => $container['cms/news/loader'],
199: 'cache' => $container['cache'],
200: 'cms/config' => $container['cms/config'],
201: 'translator' => $container['translator']
202: ]);
203:
204: return $newsManager;
205: };
206: }
207:
208: 209: 210: 211:
212: private function registerEventServices(Container $container)
213: {
214: 215: 216: 217:
218: $container['cms/event/loader'] = function (Container $container) {
219: $eventLoader = new EventLoader([
220: 'loader' => $container['model/collection/loader'],
221: 'factory' => $container['model/factory'],
222: 'cache' => $container['cache'],
223: 'translator' => $container['translator']
224: ]);
225:
226: $eventConfig = $container['cms/config']->eventConfig();
227:
228:
229: $objType = $eventConfig->get('obj_type');
230: $eventLoader->setObjType($objType);
231:
232: $lifespan = $eventConfig->get('lifespan');
233: $eventLoader->setLifespan($lifespan);
234:
235: return $eventLoader;
236: };
237:
238: 239: 240: 241:
242: $container['cms/event/manager'] = function (Container $container) {
243:
244: $eventManager = new EventManager([
245: 'loader' => $container['model/collection/loader'],
246: 'factory' => $container['model/factory'],
247: 'event/loader' => $container['cms/event/loader'],
248: 'cache' => $container['cache'],
249: 'cms/config' => $container['cms/config'],
250: 'translator' => $container['translator']
251: ]);
252:
253: return $eventManager;
254: };
255: }
256: }
257: