symflo/sy-dependency-injection-plugin

symfony 1.x plugin for Symfony Dependency Injection component SF2.

dev-master 2013-11-21 11:00 UTC

This package is not auto-updated.

Last update: 2024-12-17 03:16:06 UTC


README

use Symfony DependencyInjection Component SF2 on SF1.

Requirements

  • PHP 5.3.3+

Installation

Add autoload Composer on your symfony project. In the config/ProjectConfiguration.class.php add:

<?php
require_once __DIR__.'/../vendor/autoload.php';
?>

Add Symfony DependencyInjection Component in your composer.json

    "require": {
        ...
        "symflo/sy-dependency-injection-plugin": "dev-master"
        ...
    },

Activate the plugin in the config/ProjectConfiguration.class.php.

<?php

class ProjectConfiguration extends sfProjectConfiguration
{
    public function setup()
    {
        $this->enablePlugins(array(
            /* ... */
            'syDependencyInjectionPlugin',
        ));
    }
}
?>

Configuration

Two possibilities:

Locator files

Simple and fast way (not recommanded for big project) In app.yml:

all:
  syDependencyInjectionPlugin:
    enabledDebug: false #true by default - if false, it allows to avoid regenerate container in sf_debug environment
    locators:
      defaultFileLocators:
        dir: %SF_ROOT_DIR%/config/services
        files:
          - services.php
          - services.xml
          - services.yml
          ...
      otherFileLocators:
        dir: %SF_PLUGIN_DIR%/YourPlugin/config/otherservices
        files:
          - services.php
          ...

Extensions

all:
  syDependencyInjectionPlugin:
    extensions:
      - MyDefaultExtension
      - MyOtherExtension
    compilerPass:
      custom:
        class: CustomCompilerPass
        #passConfig: not required #TYPE_BEFORE_OPTIMIZATION, TYPE_OPTIMIZE, TYPE_BEFORE_REMOVING, TYPE_REMOVE, TYPE_AFTER_REMOVING

Example extension and compilerPass: Extension in lib\dependencyInjection.

<?php

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\Config\FileLocator;

class DefaultExtension implements ExtensionInterface
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new XmlFileLoader(
            $container,
            new FileLocator(__DIR__.'/../../config/services')
        );
        $loader->load('services.xml');
    }

    public function getXsdValidationBasePath()
    {
        return false;
    }

    public function getNamespace()
    {
        return false;
    }

    public function getAlias()
    {
        return 'default';
    }
}
?>

CompilerPass in lib\dependencyInjection\compilerPass:

<?php

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class CustomCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
    }
}

?>

More informations: Documentation on Component Symfony DependencyInjection

Naturaly you can use 2 ways in same time.

Then in your Action

<?php
//...

public function executeYourAction(sfWebRequest $request)
{
    $container = $this->getContainer();
    $service = $this->getService('your.service');
}

//...
?>

Or for example in a Task

<?php
//...

protected function execute($arguments = array(), $options = array())
{
    $context = sfContext::createInstance($this->configuration);
    $container = $context->get('dependency_injection_container');
    $service = $container->get('your.service');
}

//...
?>