From df9c8748e3d6d37b7c62da896fffd84577e923a1 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 3 Oct 2017 11:46:06 +0200 Subject: [PATCH] [Bridge\Doctrine][FrameworkBundle] Deprecate some remaining uses of ContainerAwareTrait --- .../Bridge/Doctrine/ManagerRegistry.php | 19 +++++++- .../Doctrine/Tests/ManagerRegistryTest.php | 7 ++- .../Controller/ControllerResolver.php | 20 ++++---- .../Controller/RedirectController.php | 43 +++++++++++++++-- .../Controller/TemplateController.php | 43 ++++++++++++++--- .../Resources/config/routing.xml | 11 +++++ .../Controller/RedirectControllerTest.php | 48 ++++++++++++++----- .../Controller/TemplateControllerTest.php | 37 ++++++++++---- .../Handler/MongoDbSessionHandlerTest.php | 1 + 9 files changed, 187 insertions(+), 42 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/ManagerRegistry.php b/src/Symfony/Bridge/Doctrine/ManagerRegistry.php index c353e36e66..af5ccaa1ab 100644 --- a/src/Symfony/Bridge/Doctrine/ManagerRegistry.php +++ b/src/Symfony/Bridge/Doctrine/ManagerRegistry.php @@ -12,9 +12,10 @@ namespace Symfony\Bridge\Doctrine; use ProxyManager\Proxy\LazyLoadingInterface; +use Psr\Container\ContainerInterface; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerAwareTrait; +use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface; use Doctrine\Common\Persistence\AbstractManagerRegistry; /** @@ -24,7 +25,21 @@ use Doctrine\Common\Persistence\AbstractManagerRegistry; */ abstract class ManagerRegistry extends AbstractManagerRegistry implements ContainerAwareInterface { - use ContainerAwareTrait; + /** + * @var ContainerInterface + */ + protected $container; + + /** + * @deprecated since version 3.4, to be removed in 4.0 alongside with the ContainerAwareInterface type. + * @final since version 3.4 + */ + public function setContainer(SymfonyContainerInterface $container = null) + { + @trigger_error(sprintf('The "%s()" method is deprecated since version 3.4 and will be removed in 4.0. Inject a PSR-11 container using the constructor instead.', __METHOD__), E_USER_DEPRECATED); + + $this->container = $container; + } /** * {@inheritdoc} diff --git a/src/Symfony/Bridge/Doctrine/Tests/ManagerRegistryTest.php b/src/Symfony/Bridge/Doctrine/Tests/ManagerRegistryTest.php index fa3037a609..d1596a1968 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/ManagerRegistryTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/ManagerRegistryTest.php @@ -31,7 +31,7 @@ class ManagerRegistryTest extends TestCase $container = new \LazyServiceProjectServiceContainer(); $registry = new TestManagerRegistry('name', array(), array('defaultManager' => 'foo'), 'defaultConnection', 'defaultManager', 'proxyInterfaceName'); - $registry->setContainer($container); + $registry->setTestContainer($container); $foo = $container->get('foo'); $foo->bar = 123; @@ -46,6 +46,11 @@ class ManagerRegistryTest extends TestCase class TestManagerRegistry extends ManagerRegistry { + public function setTestContainer($container) + { + $this->container = $container; + } + public function getAliasNamespace($alias) { return 'Foo'; diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php index b88ad0cc7d..a688abbaba 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php @@ -48,13 +48,7 @@ class ControllerResolver extends ContainerControllerResolver $resolvedController = parent::createController($controller); if (1 === substr_count($controller, ':') && is_array($resolvedController)) { - if ($resolvedController[0] instanceof ContainerAwareInterface) { - $resolvedController[0]->setContainer($this->container); - } - - if ($resolvedController[0] instanceof AbstractController && null !== $previousContainer = $resolvedController[0]->setContainer($this->container)) { - $resolvedController[0]->setContainer($previousContainer); - } + $resolvedController[0] = $this->configureController($resolvedController[0]); } return $resolvedController; @@ -65,9 +59,19 @@ class ControllerResolver extends ContainerControllerResolver */ protected function instantiateController($class) { - $controller = parent::instantiateController($class); + return $this->configureController(parent::instantiateController($class)); + } + private function configureController($controller) + { if ($controller instanceof ContainerAwareInterface) { + // @deprecated switch, to be removed in 4.0 where these classes + // won't implement ContainerAwareInterface anymore + switch (\get_class($controller)) { + case RedirectController::class: + case TemplateController::class: + return $controller; + } $controller->setContainer($this->container); } if ($controller instanceof AbstractController && null !== $previousContainer = $controller->setContainer($this->container)) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php index b2cb1d1acc..6edee88ce5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php @@ -12,7 +12,7 @@ namespace Symfony\Bundle\FrameworkBundle\Controller; use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerAwareTrait; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -23,10 +23,37 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface; * Redirects a request to another URL. * * @author Fabien Potencier + * + * @final since version 3.4 */ class RedirectController implements ContainerAwareInterface { - use ContainerAwareTrait; + /** + * @deprecated since version 3.4, to be removed in 4.0 + */ + protected $container; + + private $router; + private $httpPort; + private $httpsPort; + + public function __construct(UrlGeneratorInterface $router = null, $httpPort = null, $httpsPort = null) + { + $this->router = $router; + $this->httpPort = $httpPort; + $this->httpsPort = $httpsPort; + } + + /** + * @deprecated since version 3.4, to be removed in 4.0 alongside with the ContainerAwareInterface type. + */ + public function setContainer(ContainerInterface $container = null) + { + @trigger_error(sprintf('The "%s()" method is deprecated since version 3.4 and will be removed in 4.0. Inject an UrlGeneratorInterface using the constructor instead.', __METHOD__), E_USER_DEPRECATED); + + $this->container = $container; + $this->router = $container->get('router'); + } /** * Redirects to another route with the given name. @@ -61,7 +88,7 @@ class RedirectController implements ContainerAwareInterface } } - return new RedirectResponse($this->container->get('router')->generate($route, $attributes, UrlGeneratorInterface::ABSOLUTE_URL), $permanent ? 301 : 302); + return new RedirectResponse($this->router->generate($route, $attributes, UrlGeneratorInterface::ABSOLUTE_URL), $permanent ? 301 : 302); } /** @@ -115,8 +142,11 @@ class RedirectController implements ContainerAwareInterface if (null === $httpPort) { if ('http' === $request->getScheme()) { $httpPort = $request->getPort(); - } elseif ($this->container->hasParameter('request_listener.http_port')) { + } elseif ($this->container && $this->container->hasParameter('request_listener.http_port')) { + @trigger_error(sprintf('Passing the http port as a container parameter is deprecated since Symfony 3.4 and won\'t be possible in 4.0. Pass it to the constructor of the "%s" class instead.', __CLASS__), E_USER_DEPRECATED); $httpPort = $this->container->getParameter('request_listener.http_port'); + } else { + $httpPort = $this->httpPort; } } @@ -127,8 +157,11 @@ class RedirectController implements ContainerAwareInterface if (null === $httpsPort) { if ('https' === $request->getScheme()) { $httpsPort = $request->getPort(); - } elseif ($this->container->hasParameter('request_listener.https_port')) { + } elseif ($this->container && $this->container->hasParameter('request_listener.https_port')) { + @trigger_error(sprintf('Passing the https port as a container parameter is deprecated since Symfony 3.4 and won\'t be possible in 4.0. Pass it to the constructor of the "%s" class instead.', __CLASS__), E_USER_DEPRECATED); $httpsPort = $this->container->getParameter('request_listener.https_port'); + } else { + $httpsPort = $this->httpsPort; } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php b/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php index 42ebd33bad..5d13355208 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/TemplateController.php @@ -12,17 +12,48 @@ namespace Symfony\Bundle\FrameworkBundle\Controller; use Symfony\Component\DependencyInjection\ContainerAwareInterface; -use Symfony\Component\DependencyInjection\ContainerAwareTrait; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Templating\EngineInterface; +use Twig\Environment; /** * TemplateController. * * @author Fabien Potencier + * + * @final since version 3.4 */ class TemplateController implements ContainerAwareInterface { - use ContainerAwareTrait; + /** + * @deprecated since version 3.4, to be removed in 4.0 + */ + protected $container; + + private $twig; + private $templating; + + public function __construct(Environment $twig = null, EngineInterface $templating = null) + { + $this->twig = $twig; + $this->templating = $templating; + } + + /** + * @deprecated since version 3.4, to be removed in 4.0 alongside with the ContainerAwareInterface type. + */ + public function setContainer(ContainerInterface $container = null) + { + @trigger_error(sprintf('The "%s()" method is deprecated since version 3.4 and will be removed in 4.0. Inject a Twig Environment or an EngineInterface using the constructor instead.', __METHOD__), E_USER_DEPRECATED); + + if ($container->has('templating')) { + $this->templating = $container->get('templating'); + } elseif ($container->has('twig')) { + $this->twig = $container->get('twig'); + } + $this->container = $container; + } /** * Renders a template. @@ -36,10 +67,10 @@ class TemplateController implements ContainerAwareInterface */ public function templateAction($template, $maxAge = null, $sharedAge = null, $private = null) { - if ($this->container->has('templating')) { - $response = $this->container->get('templating')->renderResponse($template); - } elseif ($this->container->has('twig')) { - $response = new Response($this->container->get('twig')->render($template)); + if ($this->templating) { + $response = new Response($this->templating->render($template)); + } elseif ($this->twig) { + $response = new Response($this->twig->render($template)); } else { throw new \LogicException('You can not use the TemplateController if the Templating Component or the Twig Bundle are not available.'); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml index 6856512e20..ed8dbfe8d7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml @@ -110,5 +110,16 @@ + + + + %request_listener.http_port% + %request_listener.https_port% + + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/RedirectControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/RedirectControllerTest.php index 14b6e4428e..a95083a382 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/RedirectControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/RedirectControllerTest.php @@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Exception\HttpException; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Bundle\FrameworkBundle\Controller\RedirectController; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; @@ -66,23 +67,14 @@ class RedirectControllerTest extends TestCase $request->attributes = new ParameterBag($attributes); - $router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->getMock(); + $router = $this->getMockBuilder(UrlGeneratorInterface::class)->getMock(); $router ->expects($this->once()) ->method('generate') ->with($this->equalTo($route), $this->equalTo($expectedAttributes)) ->will($this->returnValue($url)); - $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); - - $container - ->expects($this->once()) - ->method('get') - ->with($this->equalTo('router')) - ->will($this->returnValue($router)); - - $controller = new RedirectController(); - $controller->setContainer($container); + $controller = new RedirectController($router); $returnResponse = $controller->redirectAction($request, $route, $permanent, $ignoreAttributes); @@ -130,7 +122,7 @@ class RedirectControllerTest extends TestCase $this->assertEquals(302, $returnResponse->getStatusCode()); } - public function testUrlRedirectDefaultPortParameters() + public function testUrlRedirectDefaultPorts() { $host = 'www.example.com'; $baseUrl = '/base'; @@ -151,6 +143,30 @@ class RedirectControllerTest extends TestCase $this->assertRedirectUrl($returnValue, $expectedUrl); } + /** + * @group legacy + */ + public function testUrlRedirectDefaultPortParameters() + { + $host = 'www.example.com'; + $baseUrl = '/base'; + $path = '/redirect-path'; + $httpPort = 1080; + $httpsPort = 1443; + + $expectedUrl = "https://$host:$httpsPort$baseUrl$path"; + $request = $this->createRequestObject('http', $host, $httpPort, $baseUrl); + $controller = $this->createLegacyRedirectController(null, $httpsPort); + $returnValue = $controller->urlRedirectAction($request, $path, false, 'https'); + $this->assertRedirectUrl($returnValue, $expectedUrl); + + $expectedUrl = "http://$host:$httpPort$baseUrl$path"; + $request = $this->createRequestObject('https', $host, $httpPort, $baseUrl); + $controller = $this->createLegacyRedirectController($httpPort); + $returnValue = $controller->urlRedirectAction($request, $path, false, 'http'); + $this->assertRedirectUrl($returnValue, $expectedUrl); + } + public function urlRedirectProvider() { return array( @@ -256,6 +272,14 @@ class RedirectControllerTest extends TestCase } private function createRedirectController($httpPort = null, $httpsPort = null) + { + return new RedirectController(null, $httpPort, $httpsPort); + } + + /** + * @deprecated + */ + private function createLegacyRedirectController($httpPort = null, $httpsPort = null) { $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php index 6afb5e3203..497c112eed 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/TemplateControllerTest.php @@ -12,8 +12,8 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\Controller; use Symfony\Bundle\FrameworkBundle\Controller\TemplateController; +use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; -use Symfony\Component\HttpFoundation\Response; /** * @author Kévin Dunglas @@ -25,6 +25,29 @@ class TemplateControllerTest extends TestCase $twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock(); $twig->expects($this->once())->method('render')->willReturn('bar'); + $controller = new TemplateController($twig); + + $this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent()); + } + + public function testTemplating() + { + $templating = $this->getMockBuilder(EngineInterface::class)->getMock(); + $templating->expects($this->once())->method('render')->willReturn('bar'); + + $controller = new TemplateController(null, $templating); + + $this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent()); + } + + /** + * @group legacy + */ + public function testLegacyTwig() + { + $twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock(); + $twig->expects($this->once())->method('render')->willReturn('bar'); + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); $container->expects($this->at(0))->method('has')->will($this->returnValue(false)); $container->expects($this->at(1))->method('has')->will($this->returnValue(true)); @@ -36,10 +59,13 @@ class TemplateControllerTest extends TestCase $this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent()); } - public function testTemplating() + /** + * @group legacy + */ + public function testLegacyTemplating() { $templating = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface')->getMock(); - $templating->expects($this->once())->method('renderResponse')->willReturn(new Response('bar')); + $templating->expects($this->once())->method('render')->willReturn('bar'); $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); $container->expects($this->at(0))->method('has')->willReturn(true); @@ -57,12 +83,7 @@ class TemplateControllerTest extends TestCase */ public function testNoTwigNorTemplating() { - $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); - $container->expects($this->at(0))->method('has')->willReturn(false); - $container->expects($this->at(1))->method('has')->willReturn(false); - $controller = new TemplateController(); - $controller->setContainer($container); $controller->templateAction('mytemplate')->getContent(); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php index 74366863f7..9b28f066d6 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php @@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandl /** * @author Markus Bachmann * @group time-sensitive + * @group legacy */ class MongoDbSessionHandlerTest extends TestCase {