1: <?php
2:
3: namespace Charcoal\Model\ServiceProvider;
4:
5:
6: use \Pimple\Container;
7: use \Pimple\ServiceProviderInterface;
8:
9:
10: use \Charcoal\Factory\GenericFactory as Factory;
11:
12:
13: use \Charcoal\Property\PropertyInterface;
14: use \Charcoal\Property\GenericProperty;
15:
16:
17: use \Charcoal\Loader\CollectionLoader;
18: use \Charcoal\Model\Collection;
19: use \Charcoal\Model\ModelInterface;
20: use \Charcoal\Source\SourceInterface;
21: use \Charcoal\Source\DatabaseSource;
22: use \Charcoal\Model\Service\MetadataLoader;
23: use \Charcoal\Model\Service\ModelBuilder;
24: use \Charcoal\Model\Service\ModelLoaderBuilder;
25:
26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46:
47: class ModelServiceProvider implements ServiceProviderInterface
48: {
49: 50: 51: 52:
53: public function register(Container $container)
54: {
55: $this->registerModelDependencies($container);
56:
57: 58: 59: 60:
61: $container['model/factory'] = function (Container $container) {
62: return new Factory([
63: 'base_class' => ModelInterface::class,
64: 'arguments' => [ $container['model/dependencies'] ]
65: ]);
66: };
67:
68: 69: 70: 71:
72: $container['model/builder'] = function (Container $container) {
73: return new ModelBuilder([
74: 'factory' => $container['model/factory'],
75: 'metadata_loader' => $container['metadata/loader'],
76: 'source_factory' => $container['source/factory']
77: ]);
78: };
79:
80: 81: 82: 83:
84: $container['model/loader/builder'] = function (Container $container) {
85: return new ModelLoaderBuilder([
86: 'factory' => $container['model/factory'],
87: 'cache' => $container['cache']
88: ]);
89: };
90:
91: 92: 93: 94:
95: $container['model/collection/loader'] = $container->factory(function (Container $container) {
96: $factory = $container['model/collection/loader/factory'];
97: return $factory->create($factory->defaultClass());
98: });
99:
100: 101: 102: 103:
104: $container['model/collection/loader/factory'] = function (Container $container) {
105: return new Factory([
106: 'default_class' => CollectionLoader::class,
107: 'arguments' => [[
108: 'logger' => $container['logger'],
109: 'factory' => $container['model/factory'],
110: 'collection' => $container['model/collection/class']
111: ]]
112: ]);
113: };
114:
115: 116: 117: 118:
119: $container['model/collection'] = $container->factory(function (Container $container) {
120: return new $container['model/collection/class'];
121: });
122:
123:
124: $container['model/collection/class'] = Collection::class;
125: }
126:
127: 128: 129: 130:
131: protected function registerModelDependencies(Container $container)
132: {
133:
134: if (!isset($container['model/dependencies'])) {
135: 136: 137: 138:
139: $container['model/dependencies'] = function (Container $container) {
140: return [
141: 'container' => $container,
142: 'logger' => $container['logger'],
143: 'view' => $container['view'],
144: 'property_factory' => $container['property/factory'],
145: 'metadata_loader' => $container['metadata/loader'],
146: 'source_factory' => $container['source/factory']
147: ];
148: };
149: }
150:
151:
152: if (!isset($container['property/factory'])) {
153: 154: 155: 156:
157: $container['property/factory'] = function (Container $container) {
158: return new Factory([
159: 'base_class' => PropertyInterface::class,
160: 'default_class' => GenericProperty::class,
161: 'resolver_options' => [
162: 'prefix' => '\\Charcoal\\Property\\',
163: 'suffix' => 'Property'
164: ],
165: 'arguments' => [[
166: 'container' => $container,
167: 'database' => $container['database'],
168: 'logger' => $container['logger'],
169: 'translator' => $container['translator']
170: ]]
171: ]);
172: };
173: }
174:
175: if (!isset($container['metadata/loader'])) {
176: 177: 178: 179:
180: $container['metadata/loader'] = function (Container $container) {
181: return new MetadataLoader([
182: 'logger' => $container['logger'],
183: 'cache' => $container['cache'],
184: 'base_path' => $container['config']['base_path'],
185: 'paths' => $container['config']['metadata.paths']
186: ]);
187: };
188: }
189:
190: if (!isset($container['source/factory'])) {
191: 192: 193: 194:
195: $container['source/factory'] = function (Container $container) {
196: return new Factory([
197: 'map' => [
198: 'database' => DatabaseSource::class
199: ],
200: 'base_class' => SourceInterface::class,
201: 'arguments' => [[
202: 'logger' => $container['logger'],
203: 'cache' => $container['cache'],
204: 'pdo' => $container['database']
205: ]]
206: ]);
207: };
208: }
209: }
210: }
211: