xsolve-pl/model-factory-bundle

This bundle provides a versatile skeleton for organizing model factories.

Installs: 51 717

Dependents: 0

Suggesters: 1

Security: 0

Stars: 13

Watchers: 59

Forks: 7

Type:symfony-bundle

v2.0.4 2023-01-25 15:47 UTC

README

Build Status Scrutinizer Code Quality Latest Stable Version Total Downloads Monthly Downloads License

Table of contents

Introduction

This bundle wraps xsolve-pl/model-factory library and allows to compose collections of model factories declared as services with the use of tags.

See the library documentation for more details on specific use cases.

License

This bundle is under the MIT license. See the complete license in LICENSE file.

Getting started

Include this bundle in your Symfony project using Composer as follows (assuming it is installed globally):

$ composer require xsolve-pl/model-factory-bundle

For more information on Composer see its Introduction.

Afterwards you need to enable this bundle by adding a line to app/AppKernel.php file of your project:

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Xsolve\ModelFactoryBundle\XsolveModelFactoryBundle(),
        );

        // ...
    }
}

That's all - now you're ready to go!

Usage example

Grouping model factories into collections

To make it easy to produce models for multiple objects it is possible to group model factories into collections. If your application provides multiple APIs (or multiple API versions that are so different that they utilize completely different models) you are able to group factories in separate collections and avoid the risk of producing incorrect models.

Grouping model factories into collections is made easier by providing a dedicated compiler pass that uses tags on model factory service definitions to inject them into appropriate collections. Consider following example of services.xml file:

<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>

        <service id="example.model_factory_collection.first"
                 class="Xsolve\ModelFactoryBundle\ModelFactoryCollection\ModelFactoryCollection"
        />

        <service id="example.model_factory_collection.second"
                 class="Xsolve\ModelFactoryBundle\ModelFactoryCollection\ModelFactoryCollection"
        />

        <service id="example.model_factory.foo"
                 class="Example\FooModelFactory"
        >
            <tag name="xsolve.model_factory_bundle.model_factory"
                 model-factory-collection-id="example.model_factory_collection.first"
            />
            <tag name="xsolve.model_factory_bundle.model_factory"
                 model-factory-collection-id="example.model_factory_collection.second"
            />
        </service>

    </services>

</container>

This snippet defines two model factory collections (with ids example.model_factory_collection.first and example.model_factory_collection.second respectively). It also defines a single model factory (with id example.model_factory.foo). This service has a tag assigned with name attribute equal xsolve.model_factory_bundle.model_factory (which will result in it being processed by Xsolve\ModelFactoryBundle\DependencyInjection\CompilerPass\ModelFactoryCollectionCompilerPass) and model-factory-collection-id attribute containing service ids of respective collections.