PHPackages                             fpeters/sf-dependency-injection-plugin - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. fpeters/sf-dependency-injection-plugin

AbandonedArchivedSymfony1-plugin

fpeters/sf-dependency-injection-plugin
======================================

Add Support for Symfony's DependencyInjection component to your symfony1.x project

0.1.0(11y ago)22841MITPHP

Since Jul 29Pushed 11y ago2 watchersCompare

[ Source](https://github.com/FlxPeters/sfDependencyInjectionPlugin)[ Packagist](https://packagist.org/packages/fpeters/sf-dependency-injection-plugin)[ RSS](/packages/fpeters-sf-dependency-injection-plugin/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (2)Used By (0)

sfDependencyInjectionPlugin
===========================

[](#sfdependencyinjectionplugin)

It provides supporting the Symfony's DependencyInjection component in your older symfony (1.4) project with Composer.

This Plugin is inspired by  but uses some other approach to load the configuration files.

Installation
------------

[](#installation)

Create the following `composer.json` in your symfony 1.4 project's root.

```
{
    "config": {
        "vendor-dir": "lib/vendor"
    },
    "require": {
        "fpeters/symfony-dependency-injection-plugin": "dev-master"
    },
    "autoload": {
        "psr-0": { "": "psr" }
    },
}
```

Here, Composer would install the plugin in your `plugins` directory and some Symfony2 components into `vendor/symfony/`. Also, You can locate your PSR supported libraries to be auto-loaded in `%SF_ROOT%/psr` (optional).

Install the Composer and install some libraries.

```
$ curl -sS https://getcomposer.org/installer | php
$ php composer.phar install

```

To register the autoloader for libraries installed with composer, you must add this at the top of your ProjectConfiguration class:

```
# config/ProjectConfiguration.class.php

// Composer autoload
require_once dirname(__DIR__).'/lib/vendor/autoload.php';

// symfony1 autoload
require_once dirname(__DIR__).'/lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
sfCoreAutoload::register();

class ProjectConfiguration extends sfProjectConfiguration
{
    // ...
}
```

Usage
-----

[](#usage)

The Plugin only creates an empty ContainerBuilder object.To add your service configuration, simply listen for the service\_container.load\_configuration event in your ProjectConfiguration. The following example will load the services.yml from the global config directory

```
// Load Services from services.yml
$this->dispatcher->connect(
    'service_container.load_configuration',
    function (sfEvent $event) {
        // load  global config dir
        $loader = new YamlFileLoader($event->getSubject(), new FileLocator(sfConfig::get('sf_config_dir')));
        $loader->load('services.yml');
    }
);

$this->dispatcher->connect(
    'service_container.loaded',
    function (sfEvent $event) {
        // here you can do stuff when container is loaded
        $service = $event->getSubject()->get('myCoolService');
    }
);
```

You can do nearly anything with this ContainerBuilder. So maybe have a look at documentation: [http://symfony.com/doc/current/components/dependency\_injection/compilation.html](http://symfony.com/doc/current/components/dependency_injection/compilation.html)

To use a Service in your Code, simply call the Service Container.

```
// in a action.class
$container = $this->getServiceContainer();
$container->get('myCoolService');

// there is a short version of that
$this->getService('myCoolService');
```

This will also work in your Templates anc Components.

```
