GithubHelp home page GithubHelp logo

nandokstronet / di-builder Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lcobucci/di-builder

0.0 2.0 0.0 108 KB

A symfony2 container builder to make easier the creation of containers from files

License: BSD 3-Clause "New" or "Revised" License

di-builder's Introduction

di-builder

master Build Status develop Build Status

Code Climate Test Coverage Total Downloads Latest Stable Version

This library tries to help the usage of the Symfony2 dependecy injection Component by offering an easy interface to build and load your dependency injection container.

Features

  • Multiple container files or paths: you can create the container from one or more files and paths, this is useful when dealing with modules;
  • Multiple file loader: you can select if you want to create your container from XML (default), YAML, PHP or mixed mode (delegates the loading by the extension of file);
  • Usage of your own container base class: sometimes you may use a different class to inherit your container (rather than Symfony\Component\DependencyInjection\Container;
  • Automatic dump creation: instead of building your container for all requests you will do that only when things change (development mode only);
  • Configuration handlers: you are able to inject handlers to be executed before the container compilation process (to change the services definitions);
  • Dynamic parameters: if you need to configure parameters that may change according with the environment automatically (like base project directory using __DIR__).

Installation using composer

Just add "lcobucci/di-builder": "~3.0" to your composer.json and do a composer update or you can run:

composer require lcobucci/di-builder:~3.0

Basic usage

The usage is really simple, just trust the Lcobucci\DependencyInjection\Builder interface and all should be good =)

Take a look:

<?php
/* Composer autoloader was required before this */ 

use Lcobucci\DependencyInjection\ContainerBuilder;
use Lcobucci\DependencyInjection\Config\Handlers\ContainerAware;
use Lcobucci\DependencyInjection\Generators\Php as PhpGenerator;

$container = (new ContainerBuilder())->setGenerator(new PhpGenerator()) // Changes the generator
                                     ->addFile(__DIR__ . '/config/services.php') // Appends a file to create the container
                                     ->addPath(__DIR__ . '/src/Users/config') // Appends a new path to locate files
                                     ->addFile('services.php') // Appends a file to create the container (to be used with the configured paths)
                                     ->useDevelopmentMode() // Enables the development mode (production is the default)
                                     ->setDumpDir(__DIR__ . '/tmp') // Changes the dump directory
                                     ->setParameter('app.basedir', __DIR__) // Configures a dynamic parameter
                                     ->addHandler(new ContainerAware()) // Appends a new configuration handler
                                     ->getContainer(); // Retrieves the container =)

Pretty easy, right?

Handlers

The handlers are very great to change your container before the compile process. And you can create your own handler by just implementing the Lcobucci\DependencyInjection\Config\Handler interface. Like this:

use Lcobucci\DependencyInjection\Config\Handler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class EventListenerInjector implements Handler
{
    /**
     * {@inheritdoc}
     */
    public function __invoke(ContainerBuilder $builder)
    {
        $dispatcher = $builder->getDefinition('event.dispatcher');   
    
        foreach ($builder->findTaggedServiceIds('event.listener') as $service => $listenerConfig) {
            $dispatcher->addMethodCall(
                'addListener',
                [$listenerConfig['event'], new Reference($service), $listenerConfig['priority']]
            );
        }
    }
}

Happy coding ;)

di-builder's People

Contributors

lcobucci avatar

Watchers

James Cloos avatar Nando Kstro Net avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.