SyDependencyInjectionPlugin
use Symfony DependencyInjection Component SF2 on SF1., (*1)
Requirements
Installation
Add autoload Composer on your symfony project.
In the config/ProjectConfiguration.class.php
add:, (*2)
<?php
require_once __DIR__.'/../vendor/autoload.php';
?>
Add Symfony DependencyInjection Component in your composer.json
, (*3)
"require": {
...
"symflo/sy-dependency-injection-plugin": "dev-master"
...
},
Activate the plugin in the config/ProjectConfiguration.class.php
., (*4)
enablePlugins(array(
/* ... */
'syDependencyInjectionPlugin',
));
}
}
?>
Configuration
Two possibilities:, (*5)
Locator files
Simple and fast way (not recommanded for big project)
In app.yml
:, (*6)
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
., (*7)
load('services.xml');
}
public function getXsdValidationBasePath()
{
return false;
}
public function getNamespace()
{
return false;
}
public function getAlias()
{
return 'default';
}
}
?>
CompilerPass in lib\dependencyInjection\compilerPass
:, (*8)
More informations: Documentation on Component Symfony DependencyInjection, (*9)
Naturaly you can use 2 ways in same time., (*10)
Then in your Action
getContainer();
$service = $this->getService('your.service');
}
//...
?>
Or for example in a Task
configuration);
$container = $context->get('dependency_injection_container');
$service = $container->get('your.service');
}
//...
?>