jeandormehl / jhd-base
Common functionality across the Jhd Modules.
This package has no released version yet, and little information is available.
README
Common functionality across the Jhd Modules.
Installation
Installation of this module uses composer. For composer documentation, please refer to getcomposer.org.
$ php composer.phar require jeandormehl/jhd-base:1.0.x-dev
This modules requires:
Provides:
- Abstract bootstrap listener
- Abstract mapping subscriber
- Abstract metadata options
- Various abstract models/traits and interfaces
- Standard libraries for various file path functions, file caching, decompression of zip/gzip and data transfer using Guzzle
Abstract Bootstrap Listener
This class provides abstraction for custom event listeners. It attaches your custom event listener to the Zend Frameworks onBootstrap event and loads metadata information for Doctrine 2 using the MetadataLoader class. A minimum configuration is required. See the AbstractMetadataOptions class below for details. To attach your custom event listener, add it to Module.php as follows:
<?php ... class Module { ... public function init() { $priority = 250; $aggregate = new MyCustomBootsrapListener(); $aggregate->attach($manager->getEventManager(), $priority); } ... }
Abstract Mapping Subscriber
This class is responsible for reading information passed by the object_mapping
key provided in the module configuration. It allows for dynamic overrides of doctrine field and association mappings of Doctrine 2 entities and models. See the AbstractMetadataOptions class below for details. To use custom mapping subscribers, create a new class and extend Jhd\Base\EventListener\AbstractMappingSubscriber
. Then simply add your new custom mapping subscriber into the module configuration under the subscribers
key.
<?php use MyCustomMappingSubscriber; return [ ... 'subscribers' => [ MyCustomMappingSubscriber::class ], ... ];
Abstract Metadata Options
This class provides configuration for use with the Abstract Bootstrap Listener and the Abstract Mapping Subscriber. It finds, loads and configures your Annotaion
, XML
and YAML
mapping files and loads them into Doctrine 2. It also guesses the type of object manager you are using and will load the metadata information accordingly. It provides the following keys:
object_manager
- The name/alias of the Doctrine 2 object manager the module should load models into.doctrine.entitymanager.orm_default
anddoctrine.entitymanager.odm_default
are common here.enable_default_models
- This key informs the module whether it should use default models shipped with the modules. If set to false, your custom models need to be created and namespace/path should be set.models_namespace
- If you are not using default models, a namespace for your models should be provided. Leave blank if using default models.models_path
- The path to your custom Doctrine 2 mapping files. Use theJhd\Base\Stdlib\Path
class to simplify path resolution. Leave blank if using default models.subscribers
- An array of custom doctrine subscribers.object_mapping
- Used to define overrides of default mapping configuration. Contains an array of models loaded into metadata and overrides default configuration. Please see the example. Here we dynamically map a ONE-TO-MANY association.
Example:
<?php use Jhd\Base\Stdlib\Path; use My\Custom\Models\Model; use My\Custom\Models\TargetEntity; use MyCustomMappingSubscriber; return [ 'module_name' => [ 'object_manager' => 'doctrine.entitymanager.orm_default', 'enable_default_models' => false, 'models_namespace' => 'My\Custom\Models', 'models_path' => Path::normalize('./dir/to/my/models'), 'subscribers' => [ MyCustomMappingSubscriber::class ], 'object_mapping' => [ /** ONE-TO-MANY, Unidirectional with join table. */ [ 'model' => Model::class, 'associationMappings' => [ 'field' => [ 'fieldName' => 'association', 'targetEntity' => TargetEntity::class, 'type' => MyCustomMappingSubscriber::MANY_TO_MANY, 'joinTable' => [ 'name' => 'table_name', 'joinColumns' => [ [ 'name' => 'modelEntity_id', 'referencedColumnName' => 'id', ], ], 'inverseJoinColumns' => [ [ 'name' => 'targetEntity_id', 'referencedColumnName' => 'id', 'unique' => true, ], ], ], 'cascade' => [ 'persist', 'remove', ], 'orphanRemoval' => true, ], ], ], ], ], ];
Various abstract models/traits and interfaces
Please see the Models
directory in the src
of this module for more details.
Contact me on jdormehl@gmail.com.