Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 89f4429

Browse files
committed
feature #21625 Remove some container injections in favor of service locators (nicolas-grekas, chalasr)
This PR was merged into the 3.3-dev branch. Discussion ---------- Remove some container injections in favor of service locators | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony/symfony#21553 (comment) | License | MIT | Doc PR | n/a Commits ------- 8293b75 Replace some container injections by service locators 0be9ea8 [EventDispatcher] Fix abstract event subscribers registration
2 parents 6bf5331 + 2e12bde commit 89f4429

10 files changed

+224
-100
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
CHANGELOG
22
=========
33

4+
3.3.0
5+
-----
6+
7+
* Deprecated `LazyLoadingFragmentHandler::addRendererService()`
8+
* Added `SessionListener`
9+
* Added `TestSessionListener`
10+
411
3.2.0
512
-----
613

DependencyInjection/FragmentRendererPass.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
namespace Symfony\Component\HttpKernel\DependencyInjection;
1313

14+
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
1415
use Symfony\Component\DependencyInjection\ContainerBuilder;
1516
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1617
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
18+
use Symfony\Component\DependencyInjection\Reference;
1719
use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
1820

1921
/**
@@ -43,14 +45,11 @@ 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);
48-
if (!$def->isPublic()) {
49-
throw new InvalidArgumentException(sprintf('The service "%s" must be public as fragment renderer are lazy-loaded.', $id));
50-
}
51-
5251
if ($def->isAbstract()) {
53-
throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as fragment renderer are lazy-loaded.', $id));
52+
continue;
5453
}
5554

5655
$class = $container->getParameterBag()->resolveValue($def->getClass());
@@ -63,8 +62,10 @@ public function process(ContainerBuilder $container)
6362
}
6463

6564
foreach ($tags as $tag) {
66-
$definition->addMethodCall('addRendererService', array($tag['alias'], $id));
65+
$renderers[$tag['alias']] = new Reference($id);
6766
}
6867
}
68+
69+
$definition->replaceArgument(0, new ServiceLocatorArgument($renderers));
6970
}
7071
}

DependencyInjection/LazyLoadingFragmentHandler.php

Lines changed: 17 additions & 2 deletions
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,11 @@
2323
class LazyLoadingFragmentHandler extends FragmentHandler
2424
{
2525
private $container;
26+
/**
27+
* @deprecated since version 3.3, to be removed in 4.0
28+
*/
2629
private $rendererIds = array();
30+
private $initialized = array();
2731

2832
/**
2933
* Constructor.
@@ -44,9 +48,13 @@ public function __construct(ContainerInterface $container, RequestStack $request
4448
*
4549
* @param string $name The service name
4650
* @param string $renderer The render service id
51+
*
52+
* @deprecated since version 3.3, to be removed in 4.0
4753
*/
4854
public function addRendererService($name, $renderer)
4955
{
56+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
57+
5058
$this->rendererIds[$name] = $renderer;
5159
}
5260

@@ -55,10 +63,17 @@ public function addRendererService($name, $renderer)
5563
*/
5664
public function render($uri, $renderer = 'inline', array $options = array())
5765
{
66+
// BC 3.x, to be removed in 4.0
5867
if (isset($this->rendererIds[$renderer])) {
5968
$this->addRenderer($this->container->get($this->rendererIds[$renderer]));
60-
6169
unset($this->rendererIds[$renderer]);
70+
71+
return parent::render($uri, $renderer, $options);
72+
}
73+
74+
if (!isset($this->initialized[$renderer]) && $this->container->has($renderer)) {
75+
$this->addRenderer($this->container->get($renderer));
76+
$this->initialized[$renderer] = true;
6277
}
6378

6479
return parent::render($uri, $renderer, $options);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\EventListener;
13+
14+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
15+
use Symfony\Component\HttpKernel\KernelEvents;
16+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17+
18+
/**
19+
* Sets the session in the request.
20+
*
21+
* @author Johannes M. Schmitt <[email protected]>
22+
*/
23+
abstract class AbstractSessionListener implements EventSubscriberInterface
24+
{
25+
public function onKernelRequest(GetResponseEvent $event)
26+
{
27+
if (!$event->isMasterRequest()) {
28+
return;
29+
}
30+
31+
$request = $event->getRequest();
32+
$session = $this->getSession();
33+
if (null === $session || $request->hasSession()) {
34+
return;
35+
}
36+
37+
$request->setSession($session);
38+
}
39+
40+
public static function getSubscribedEvents()
41+
{
42+
return array(
43+
KernelEvents::REQUEST => array('onKernelRequest', 128),
44+
);
45+
}
46+
47+
/**
48+
* Gets the session object.
49+
*
50+
* @return SessionInterface|null A SessionInterface instance or null if no session is available
51+
*/
52+
abstract protected function getSession();
53+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\EventListener;
13+
14+
use Symfony\Component\HttpFoundation\Cookie;
15+
use Symfony\Component\HttpKernel\KernelEvents;
16+
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
17+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
18+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19+
20+
/**
21+
* TestSessionListener.
22+
*
23+
* Saves session in test environment.
24+
*
25+
* @author Bulat Shakirzyanov <[email protected]>
26+
* @author Fabien Potencier <[email protected]>
27+
*/
28+
abstract class AbstractTestSessionListener implements EventSubscriberInterface
29+
{
30+
public function onKernelRequest(GetResponseEvent $event)
31+
{
32+
if (!$event->isMasterRequest()) {
33+
return;
34+
}
35+
36+
// bootstrap the session
37+
$session = $this->getSession();
38+
if (!$session) {
39+
return;
40+
}
41+
42+
$cookies = $event->getRequest()->cookies;
43+
44+
if ($cookies->has($session->getName())) {
45+
$session->setId($cookies->get($session->getName()));
46+
}
47+
}
48+
49+
/**
50+
* Checks if session was initialized and saves if current request is master
51+
* Runs on 'kernel.response' in test environment.
52+
*
53+
* @param FilterResponseEvent $event
54+
*/
55+
public function onKernelResponse(FilterResponseEvent $event)
56+
{
57+
if (!$event->isMasterRequest()) {
58+
return;
59+
}
60+
61+
$session = $event->getRequest()->getSession();
62+
if ($session && $session->isStarted()) {
63+
$session->save();
64+
$params = session_get_cookie_params();
65+
$event->getResponse()->headers->setCookie(new Cookie($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']));
66+
}
67+
}
68+
69+
public static function getSubscribedEvents()
70+
{
71+
return array(
72+
KernelEvents::REQUEST => array('onKernelRequest', 192),
73+
KernelEvents::RESPONSE => array('onKernelResponse', -128),
74+
);
75+
}
76+
77+
/**
78+
* Gets the session object.
79+
*
80+
* @return SessionInterface|null A SessionInterface instance or null if no session is available
81+
*/
82+
abstract protected function getSession();
83+
}

EventListener/SessionListener.php

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,30 @@
1111

1212
namespace Symfony\Component\HttpKernel\EventListener;
1313

14-
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
15-
use Symfony\Component\HttpKernel\KernelEvents;
16-
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14+
use Psr\Container\ContainerInterface;
1715

1816
/**
1917
* Sets the session in the request.
2018
*
21-
* @author Johannes M. Schmitt <[email protected]>
19+
* @author Fabien Potencier <[email protected]>
20+
*
21+
* @final since version 3.3
2222
*/
23-
abstract class SessionListener implements EventSubscriberInterface
23+
class SessionListener extends AbstractSessionListener
2424
{
25-
public function onKernelRequest(GetResponseEvent $event)
25+
private $container;
26+
27+
public function __construct(ContainerInterface $container)
2628
{
27-
if (!$event->isMasterRequest()) {
28-
return;
29-
}
29+
$this->container = $container;
30+
}
3031

31-
$request = $event->getRequest();
32-
$session = $this->getSession();
33-
if (null === $session || $request->hasSession()) {
32+
protected function getSession()
33+
{
34+
if (!$this->container->has('session')) {
3435
return;
3536
}
3637

37-
$request->setSession($session);
38+
return $this->container->get('session');
3839
}
39-
40-
public static function getSubscribedEvents()
41-
{
42-
return array(
43-
KernelEvents::REQUEST => array('onKernelRequest', 128),
44-
);
45-
}
46-
47-
/**
48-
* Gets the session object.
49-
*
50-
* @return SessionInterface|null A SessionInterface instance or null if no session is available
51-
*/
52-
abstract protected function getSession();
5340
}

EventListener/TestSessionListener.php

Lines changed: 12 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,73 +11,30 @@
1111

1212
namespace Symfony\Component\HttpKernel\EventListener;
1313

14-
use Symfony\Component\HttpFoundation\Cookie;
15-
use Symfony\Component\HttpKernel\KernelEvents;
16-
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
17-
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
18-
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14+
use Psr\Container\ContainerInterface;
1915

2016
/**
21-
* TestSessionListener.
17+
* Sets the session in the request.
2218
*
23-
* Saves session in test environment.
24-
*
25-
* @author Bulat Shakirzyanov <[email protected]>
2619
* @author Fabien Potencier <[email protected]>
20+
*
21+
* @final since version 3.3
2722
*/
28-
abstract class TestSessionListener implements EventSubscriberInterface
23+
class TestSessionListener extends AbstractTestSessionListener
2924
{
30-
public function onKernelRequest(GetResponseEvent $event)
31-
{
32-
if (!$event->isMasterRequest()) {
33-
return;
34-
}
35-
36-
// bootstrap the session
37-
$session = $this->getSession();
38-
if (!$session) {
39-
return;
40-
}
25+
private $container;
4126

42-
$cookies = $event->getRequest()->cookies;
43-
44-
if ($cookies->has($session->getName())) {
45-
$session->setId($cookies->get($session->getName()));
46-
}
27+
public function __construct(ContainerInterface $container)
28+
{
29+
$this->container = $container;
4730
}
4831

49-
/**
50-
* Checks if session was initialized and saves if current request is master
51-
* Runs on 'kernel.response' in test environment.
52-
*
53-
* @param FilterResponseEvent $event
54-
*/
55-
public function onKernelResponse(FilterResponseEvent $event)
32+
protected function getSession()
5633
{
57-
if (!$event->isMasterRequest()) {
34+
if (!$this->container->has('session')) {
5835
return;
5936
}
6037

61-
$session = $event->getRequest()->getSession();
62-
if ($session && $session->isStarted()) {
63-
$session->save();
64-
$params = session_get_cookie_params();
65-
$event->getResponse()->headers->setCookie(new Cookie($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']));
66-
}
67-
}
68-
69-
public static function getSubscribedEvents()
70-
{
71-
return array(
72-
KernelEvents::REQUEST => array('onKernelRequest', 192),
73-
KernelEvents::RESPONSE => array('onKernelResponse', -128),
74-
);
38+
return $this->container->get('session');
7539
}
76-
77-
/**
78-
* Gets the session object.
79-
*
80-
* @return SessionInterface|null A SessionInterface instance or null if no session is available
81-
*/
82-
abstract protected function getSession();
8340
}

0 commit comments

Comments
 (0)