Code Monkey home page Code Monkey logo

sfdependencyinjectionplugin's Introduction

sfDependencyInjectionPlugin

Build Status

Provides integration Symfony2's Dependency Injection component with your older symfony (1.4+) project.

Installation

Using Composer would be best way:

$ composer require issei-m/sf-dependency-injection-plugin

Here, Composer would install this plugin in your plugins directory and some other libraries plugin depends on into vendor.

If you don't use Composer, you need to install this plugin and some others manually.

Usage

First, create your services.yml in %SF_ROOT%/config/services.yml. It can be defined your parameters/services to each different environments.

Something like:

# config/services.yml

dev:
  parameters:
    mailer.transport: gmail

all:
  parameters:
    # ...
    mailer.transport: sendmail

  services:
    mailer:
      class:     Mailer
      arguments: ["%mailer.transport%"]
    newsletter_manager:
      class:     NewsletterManager
      calls:
        - [setMailer, ["@mailer"]]

The services.yml is supporting the configuratoin cascade like the settings.yml, and it can be located in several different config directory for apps (e.g.apps/frontend/config). When the ServiceContainer is compiled, the values from these are merged.

Next, enable this plugin at your ProjectConfiguration:

class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    $this->enablePlugins('sfDependencyInjectionPlugin');
    ...
  }
}

Now, your sfContext has installed Symfony's service container, it is used as following in your code:

// Get the ServiceContainer.
$container = sfContext::getInstance()->getContainer();

// Retrieve the NewsletterManager class which was initialized with the Mailer.
$newsletterManager = $container->get('newsletter_manager');

If you use lexpress/symfony1, sfServiceContainer is replaced with plugin's service container. But it might work almost as well as framework's one:

$container = sfContext::getInstance()->getServiceContainer();
$newsletterManager = sfContext::getInstance()->getService('newsletter_manager');

At task

Even though you don't initialize the sfContext at task, you can initialize the service container manually like this:

$containerClass = require $this->configuration->getConfigCache()->checkConfig('config/services.yml', true);
$container = new $containerClass();

Event

When container is compiled, service_container.build event is fired. You can expand container definitions if you subscribe this event.

It means you can have control your service container as you wish with your own extension, compiler pass etc...:

class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    $this->dispatcher->connect('service_container.build', function (sfEvent $event) {
      /** @var \Symfony\Component\DependencyInjection\ContainerBuilder $container */
      $container = $event->getSubject();
      $container->addObjectResource($this);

      // additional parameter
      $container->setParameter('foo', 'bar');

      // add extension
      $container->registerExtension(new YourExtension());

      // add compiler pass
      $container->addCompilerPass(new YourPass());
    });
  }
}

sfdependencyinjectionplugin's People

Contributors

issei-m avatar adrianolek avatar gromnan avatar

Watchers

Yannick Gauthier avatar James Cloos 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.