Skip to content

Commit 1d3ef15

Browse files
committed
[HttpKernel] Replace container by service locator in LazyLoadingFragmentHandler
1 parent c723a66 commit 1d3ef15

File tree

5 files changed

+31
-14
lines changed

5 files changed

+31
-14
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/fragment_renderer.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<services>
1313
<service id="fragment.handler" class="Symfony\Component\HttpKernel\DependencyInjection\LazyLoadingFragmentHandler">
14-
<argument type="service" id="service_container" />
14+
<argument /> <!-- fragment renderer locator -->
1515
<argument type="service" id="request_stack" />
1616
<argument>%kernel.debug%</argument>
1717
</service>

src/Symfony/Component/HttpKernel/DependencyInjection/FragmentRendererPass.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1616
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1719
use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
1820

1921
/**
@@ -43,6 +45,7 @@ public function process(ContainerBuilder $container)
4345
}
4446

4547
$definition = $container->getDefinition($this->handlerService);
48+
$renderers = array();
4649
foreach ($container->findTaggedServiceIds($this->rendererTag) as $id => $tags) {
4750
$def = $container->getDefinition($id);
4851
if (!$def->isPublic()) {
@@ -63,8 +66,10 @@ public function process(ContainerBuilder $container)
6366
}
6467

6568
foreach ($tags as $tag) {
66-
$definition->addMethodCall('addRendererService', array($tag['alias'], $id));
69+
$renderers[$tag['alias']] = new Reference($id);
6770
}
6871
}
72+
73+
$definition->replaceArgument(0, new ServiceLocatorArgument($renderers));
6974
}
7075
}

src/Symfony/Component/HttpKernel/DependencyInjection/LazyLoadingFragmentHandler.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\HttpKernel\DependencyInjection;
1313

14-
use Symfony\Component\DependencyInjection\ContainerInterface;
14+
use Psr\Container\ContainerInterface;
1515
use Symfony\Component\HttpFoundation\RequestStack;
1616
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
1717

@@ -23,7 +23,7 @@
2323
class LazyLoadingFragmentHandler extends FragmentHandler
2424
{
2525
private $container;
26-
private $rendererIds = array();
26+
private $initialized = array();
2727

2828
/**
2929
* Constructor.
@@ -42,23 +42,24 @@ public function __construct(ContainerInterface $container, RequestStack $request
4242
/**
4343
* Adds a service as a fragment renderer.
4444
*
45+
* @deprecated since version 3.3, to be removed in 4.0
46+
*
4547
* @param string $name The service name
4648
* @param string $renderer The render service id
4749
*/
4850
public function addRendererService($name, $renderer)
4951
{
50-
$this->rendererIds[$name] = $renderer;
52+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
5153
}
5254

5355
/**
5456
* {@inheritdoc}
5557
*/
5658
public function render($uri, $renderer = 'inline', array $options = array())
5759
{
58-
if (isset($this->rendererIds[$renderer])) {
59-
$this->addRenderer($this->container->get($this->rendererIds[$renderer]));
60-
61-
unset($this->rendererIds[$renderer]);
60+
if (!isset($this->initialized[$renderer]) && $this->container->has($renderer)) {
61+
$this->addRenderer($this->container->get($renderer));
62+
$this->initialized[$renderer] = true;
6263
}
6364

6465
return parent::render($uri, $renderer, $options);

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/FragmentRendererPassTest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\HttpKernel\DependencyInjection\FragmentRendererPass;
1616
use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
17+
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
18+
use Symfony\Component\DependencyInjection\Reference;
1719

1820
class FragmentRendererPassTest extends \PHPUnit_Framework_TestCase
1921
{
@@ -59,9 +61,8 @@ public function testValidContentRenderer()
5961
$renderer = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
6062
$renderer
6163
->expects($this->once())
62-
->method('addMethodCall')
63-
->with('addRendererService', array('foo', 'my_content_renderer'))
64-
;
64+
->method('replaceArgument')
65+
->with(0, $this->equalTo(new ServiceLocatorArgument(array('foo' => new Reference('my_content_renderer')))));
6566

6667
$definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->getMock();
6768
$definition->expects($this->atLeastOnce())

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/LazyLoadingFragmentHandlerTest.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,25 @@ public function test()
2626
$requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock();
2727
$requestStack->expects($this->any())->method('getCurrentRequest')->will($this->returnValue(Request::create('/')));
2828

29-
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
29+
$container = $this->getMockBuilder('Psr\Container\ContainerInterface')->getMock();
30+
$container->expects($this->once())->method('has')->with('foo')->willReturn(true);
3031
$container->expects($this->once())->method('get')->will($this->returnValue($renderer));
3132

3233
$handler = new LazyLoadingFragmentHandler($container, $requestStack, false);
33-
$handler->addRendererService('foo', 'foo');
3434

3535
$handler->render('/foo', 'foo');
3636

3737
// second call should not lazy-load anymore (see once() above on the get() method)
3838
$handler->render('/foo', 'foo');
3939
}
40+
41+
/**
42+
* @group legacy
43+
* @expectedDeprecation The Symfony\Component\HttpKernel\DependencyInjection\LazyLoadingFragmentHandler::addRendererService() method is deprecated since version 3.3 and will be removed in 4.0.
44+
*/
45+
public function testAddRendererServiceIsDeprecated()
46+
{
47+
$handler = new LazyLoadingFragmentHandler($this->getMockBuilder('Psr\Container\ContainerInterface')->getMock(), $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock());
48+
$handler->addRendererService('foo', 'bar');
49+
}
4050
}

0 commit comments

Comments
 (0)