symflo/sy-dependency-injection-plugin

Symfony 1.x 扩展,用于 Symfony 依赖注入组件 SF2。

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

This package is not auto-updated.

Last update: 2024-09-24 02:16:44 UTC


README

在 SF1 中使用 Symfony DependencyInjection 组件 SF2

需求

  • PHP 5.3.3+

安装

在您的 symfony 项目中添加 Composer 自动加载。在 config/ProjectConfiguration.class.php 中添加:

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

在您的 composer.json 中添加 Symfony DependencyInjection 组件。

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

config/ProjectConfiguration.class.php 中激活插件。

<?php

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

配置

两种可能的方式

定位文件

简单快捷的方法(不推荐用于大型项目)在 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
          ...

扩展

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

示例扩展和编译器:在 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';
    }
}
?>

编译器在 lib\dependencyInjection\compilerPass

<?php

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

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

?>

更多信息:[Component Symfony DependencyInjection 文档](https://symfony.ac.cn/doc/current/components/dependency_injection/compilation.html)

当然,您可以在同一时间使用两种方式。

然后在您的动作中

<?php
//...

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

//...
?>

或者例如在一个任务中

<?php
//...

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

//...
?>