Overview

Namespaces

  • Charcoal
    • Loader
    • Model
      • Service
      • ServiceProvider
    • Source
      • Database
    • Validator

Classes

  • Charcoal\Loader\CollectionLoader
  • Charcoal\Loader\FileLoader
  • Charcoal\Model\AbstractMetadata
  • Charcoal\Model\AbstractModel
  • Charcoal\Model\Collection
  • Charcoal\Model\Model
  • Charcoal\Model\ModelMetadata
  • Charcoal\Model\ModelValidator
  • Charcoal\Model\Service\MetadataLoader
  • Charcoal\Model\Service\ModelBuilder
  • Charcoal\Model\Service\ModelLoader
  • Charcoal\Model\Service\ModelLoaderBuilder
  • Charcoal\Model\ServiceProvider\ModelServiceProvider
  • Charcoal\Source\AbstractSource
  • Charcoal\Source\Database\DatabaseFilter
  • Charcoal\Source\Database\DatabaseOrder
  • Charcoal\Source\Database\DatabasePagination
  • Charcoal\Source\DatabaseSource
  • Charcoal\Source\DatabaseSourceConfig
  • Charcoal\Source\Filter
  • Charcoal\Source\Order
  • Charcoal\Source\Pagination
  • Charcoal\Source\SourceConfig
  • Charcoal\Validator\AbstractValidator
  • Charcoal\Validator\ValidatorResult

Interfaces

  • Charcoal\Model\CollectionInterface
  • Charcoal\Model\DescribableInterface
  • Charcoal\Model\MetadataInterface
  • Charcoal\Model\ModelInterface
  • Charcoal\Source\DatabaseSourceInterface
  • Charcoal\Source\FilterInterface
  • Charcoal\Source\OrderInterface
  • Charcoal\Source\PaginationInterface
  • Charcoal\Source\SourceInterface
  • Charcoal\Source\StorableInterface
  • Charcoal\Validator\ValidatableInterface
  • Charcoal\Validator\ValidatorInterface

Traits

  • Charcoal\Model\DescribableTrait
  • Charcoal\Source\StorableTrait
  • Charcoal\Validator\ValidatableTrait
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: namespace Charcoal\Model\ServiceProvider;
  4: 
  5: // Dependencies from `Pimple`
  6: use \Pimple\Container;
  7: use \Pimple\ServiceProviderInterface;
  8: 
  9: // Module `charcoal-factory` dependencies
 10: use \Charcoal\Factory\GenericFactory as Factory;
 11: 
 12: // Module `charcoal-property` dependencies
 13: use \Charcoal\Property\PropertyInterface;
 14: use \Charcoal\Property\GenericProperty;
 15: 
 16: // Intra-module (`charcoal-core`) dependencies
 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:  * Model Service Providers.
 28:  *
 29:  * ##Container dependencies
 30:  *
 31:  * The following keys are expected to be set on the container
 32:  * (from external sources / providers):
 33:  *
 34:  * - `cache` A PSR-6 compliant cache pool.
 35:  * - `config` A charcoal app config (\Charcoal\Config\ConfigInterface)q
 36:  * - `database` A PDO database instance
 37:  * - `logger` A PSR-3 compliant logger.
 38:  * - `view` A \Charcoal\View\ViewInterface instance
 39:  *
 40:  * ## Services
 41:  *
 42:  * The following services are registered on the container:
 43:  *
 44:  * - `model/factory` A \Charcoal\Factory\FactoryInterface factory to create models.
 45:  * - `model/collection/loader` A collection loader (should not be used).
 46:  */
 47: class ModelServiceProvider implements ServiceProviderInterface
 48: {
 49:     /**
 50:      * @param Container $container A Pimple DI container instance.
 51:      * @return void
 52:      */
 53:     public function register(Container $container)
 54:     {
 55:         $this->registerModelDependencies($container);
 56: 
 57:         /**
 58:          * @param Container $container A Pimple DI container instance.
 59:          * @return ModelFactory
 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:          * @param Container $container A Pimple DI container instance.
 70:          * @return ModelBuilder
 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:          * @param Container $container A Pimple DI container instance.
 82:          * @return ModelLoaderBuilder
 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:          * @param Container $container A Pimple DI container instance.
 93:          * @return CollectionLoader
 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:          * @param Container $container A Pimple DI container instance.
102:          * @return FactoryInterface
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:          * @param Container $container A Pimple DI container instance.
117:          * @return ArrayAccess|\Traversable
118:          */
119:         $container['model/collection'] = $container->factory(function (Container $container) {
120:             return new $container['model/collection/class'];
121:         });
122: 
123:         /** The default collection class name. */
124:         $container['model/collection/class'] = Collection::class;
125:     }
126: 
127:     /**
128:      * @param Container $container A Pimple DI container instance.
129:      * @return void
130:      */
131:     protected function registerModelDependencies(Container $container)
132:     {
133:         // The model dependencies might be already set from elsewhere; defines it if not.
134:         if (!isset($container['model/dependencies'])) {
135:             /**
136:              * @param Container $container A Pimple DI container instance.
137:              * @return array The model dependencies array.
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:         // The property factory might be already set from elsewhere; defines it if not.
152:         if (!isset($container['property/factory'])) {
153:             /**
154:              * @param Container $container A Pimple DI container instance.
155:              * @return \Charcoal\Factory\FactoryInterface
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:              * @param Container $container A Pimple DI container instance.
178:              * @return MetadataLoader
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:              * @param Container $container A Pimple DI container instance.
193:              * @return \Charcoal\Factory\FactoryInterface
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: 
API documentation generated by ApiGen