Skip to content

Instantly share code, notes, and snippets.

@avalanche123
Created October 1, 2010 11:34
Show Gist options
  • Save avalanche123/606086 to your computer and use it in GitHub Desktop.
Save avalanche123/606086 to your computer and use it in GitHub Desktop.
<?php
namespace Symfony\Component\DependencyInjection;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* ContainerAwareInterface should be implemented by classes that depends on a Container.
*
* @author Fabien Potencier <[email protected]>
*/
interface ContainerAwareInterface
{
/**
* Sets the Container.
*
* @param ContainerInterface $container A ContainerInterface instance
*/
function setContainer(ContainerInterface $container = null);
}
<?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">
<services>
<service id="index_controller" class="Company\ApplicationBundle\Controller\SimpleController" />
</services>
</container>
<?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">
<interfaces>
<interface class="Symfony\Component\DependencyInjection\ContainerAwareInterface">
<call method="setContainer">
<argument type="service" id="container" />
</call>
</interface>
</interfaces>
</container>
<?php
namespace Company\ApplicationBundle\Controller;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
class SimpleController implements ContainerAwareInterface
{
/**
* @var Symfony\Component\DependencyInjection\Container
*/
private $container;
/**
* @param Symfony\Component\DependencyInjection\Container $container
*/
public function setContainer(Container $container)
{
$this->container = $container;
}
//...
}
@henrikbjorn
Copy link

This is kinda awesome if i understand it correctly. So if a class implementes a specific interface (in this case ContainerAwareInterface) the service will call setContainer for that class?

@avalanche123
Copy link
Author

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