Skip to content

Instantly share code, notes, and snippets.

@hidenorigoto
Created February 17, 2011 07:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hidenorigoto/831230 to your computer and use it in GitHub Desktop.
Save hidenorigoto/831230 to your computer and use it in GitHub Desktop.
enabling Symfony2 DI container in custom class
<?xml version="1.0" ?>
<container xmlns="http://www.symfony-project.org/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="hello.my_class">Sensio\HelloBundle\Lib\MyClass</parameter>
</parameters>
<services>
<service id="my_class" class="%hello.my_class%">
<argument type="service" id="service_container" />
</service>
</services>
</container>
<?php
// Sensio/HelloBundle/DependencyInjection
namespace Sensio\HelloBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
class HelloExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('hello.xml');
}
public function getAlias()
{
return 'hello';
}
}
<?php
// Sensio/HelloBundle/Lib
namespace Sensio\HelloBundle\Lib;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MyClass
{
private $container = null;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function test()
{
if ($this->container)
{
echo $this->container->getParameter('kernel.root_dir');
}
return null;
}
}
<?php
// Sensio/HelloBundle/Command
namespace Sensio\HelloBundle\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Symfony\Bundle\FrameworkBundle\Command\Command;
class TestCommand extends Command
{
/**
* @see Command
*/
protected function configure()
{
$this
->setName('hello:test')
;
}
/**
* @see Command
*
* @throws \InvalidArgumentException When namespace doesn't end with Bundle
* @throws \RuntimeException When bundle can't be executed
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$myclass = $this->container->get('my_class');
$myclass->test();
}
}
@hidenorigoto
Copy link
Author

Symfony2で自作クラス内でcontainerを呼び出せるようにするためのサンプルです。自作クラスのあるバンドルにDependencyInjectionディレクトリを作成してエクステンションクラスを作成し、loadメソッドでサービス定義のXMLファイルを読み込むようにします。
XMLファイル内に、自作クラスをサービスとして定義して、コンストラクタのパラメータとしてservice_containerを渡すようにしています。
コンストラクタで渡せないクラスの場合は、callを使えばDICでインスタンス化した後に指定メソッドを指定パラメータで呼び出すようにもできます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment