[Bridge\Doctrine][FrameworkBundle] Deprecate some remaining uses of ContainerAwareTrait

This commit is contained in:
Nicolas Grekas 2017-10-03 11:46:06 +02:00
parent 90986f4502
commit df9c8748e3
9 changed files with 187 additions and 42 deletions

View File

@ -12,9 +12,10 @@
namespace Symfony\Bridge\Doctrine; namespace Symfony\Bridge\Doctrine;
use ProxyManager\Proxy\LazyLoadingInterface; use ProxyManager\Proxy\LazyLoadingInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
use Doctrine\Common\Persistence\AbstractManagerRegistry; use Doctrine\Common\Persistence\AbstractManagerRegistry;
/** /**
@ -24,7 +25,21 @@ use Doctrine\Common\Persistence\AbstractManagerRegistry;
*/ */
abstract class ManagerRegistry extends AbstractManagerRegistry implements ContainerAwareInterface 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} * {@inheritdoc}

View File

@ -31,7 +31,7 @@ class ManagerRegistryTest extends TestCase
$container = new \LazyServiceProjectServiceContainer(); $container = new \LazyServiceProjectServiceContainer();
$registry = new TestManagerRegistry('name', array(), array('defaultManager' => 'foo'), 'defaultConnection', 'defaultManager', 'proxyInterfaceName'); $registry = new TestManagerRegistry('name', array(), array('defaultManager' => 'foo'), 'defaultConnection', 'defaultManager', 'proxyInterfaceName');
$registry->setContainer($container); $registry->setTestContainer($container);
$foo = $container->get('foo'); $foo = $container->get('foo');
$foo->bar = 123; $foo->bar = 123;
@ -46,6 +46,11 @@ class ManagerRegistryTest extends TestCase
class TestManagerRegistry extends ManagerRegistry class TestManagerRegistry extends ManagerRegistry
{ {
public function setTestContainer($container)
{
$this->container = $container;
}
public function getAliasNamespace($alias) public function getAliasNamespace($alias)
{ {
return 'Foo'; return 'Foo';

View File

@ -48,13 +48,7 @@ class ControllerResolver extends ContainerControllerResolver
$resolvedController = parent::createController($controller); $resolvedController = parent::createController($controller);
if (1 === substr_count($controller, ':') && is_array($resolvedController)) { if (1 === substr_count($controller, ':') && is_array($resolvedController)) {
if ($resolvedController[0] instanceof ContainerAwareInterface) { $resolvedController[0] = $this->configureController($resolvedController[0]);
$resolvedController[0]->setContainer($this->container);
}
if ($resolvedController[0] instanceof AbstractController && null !== $previousContainer = $resolvedController[0]->setContainer($this->container)) {
$resolvedController[0]->setContainer($previousContainer);
}
} }
return $resolvedController; return $resolvedController;
@ -65,9 +59,19 @@ class ControllerResolver extends ContainerControllerResolver
*/ */
protected function instantiateController($class) protected function instantiateController($class)
{ {
$controller = parent::instantiateController($class); return $this->configureController(parent::instantiateController($class));
}
private function configureController($controller)
{
if ($controller instanceof ContainerAwareInterface) { 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); $controller->setContainer($this->container);
} }
if ($controller instanceof AbstractController && null !== $previousContainer = $controller->setContainer($this->container)) { if ($controller instanceof AbstractController && null !== $previousContainer = $controller->setContainer($this->container)) {

View File

@ -12,7 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Controller; namespace Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -23,10 +23,37 @@ use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
* Redirects a request to another URL. * Redirects a request to another URL.
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
*
* @final since version 3.4
*/ */
class RedirectController implements ContainerAwareInterface 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. * 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 (null === $httpPort) {
if ('http' === $request->getScheme()) { if ('http' === $request->getScheme()) {
$httpPort = $request->getPort(); $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'); $httpPort = $this->container->getParameter('request_listener.http_port');
} else {
$httpPort = $this->httpPort;
} }
} }
@ -127,8 +157,11 @@ class RedirectController implements ContainerAwareInterface
if (null === $httpsPort) { if (null === $httpsPort) {
if ('https' === $request->getScheme()) { if ('https' === $request->getScheme()) {
$httpsPort = $request->getPort(); $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'); $httpsPort = $this->container->getParameter('request_listener.https_port');
} else {
$httpsPort = $this->httpsPort;
} }
} }

View File

@ -12,17 +12,48 @@
namespace Symfony\Bundle\FrameworkBundle\Controller; namespace Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerAwareInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait; use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\EngineInterface;
use Twig\Environment;
/** /**
* TemplateController. * TemplateController.
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
*
* @final since version 3.4
*/ */
class TemplateController implements ContainerAwareInterface 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. * Renders a template.
@ -36,10 +67,10 @@ class TemplateController implements ContainerAwareInterface
*/ */
public function templateAction($template, $maxAge = null, $sharedAge = null, $private = null) public function templateAction($template, $maxAge = null, $sharedAge = null, $private = null)
{ {
if ($this->container->has('templating')) { if ($this->templating) {
$response = $this->container->get('templating')->renderResponse($template); $response = new Response($this->templating->render($template));
} elseif ($this->container->has('twig')) { } elseif ($this->twig) {
$response = new Response($this->container->get('twig')->render($template)); $response = new Response($this->twig->render($template));
} else { } else {
throw new \LogicException('You can not use the TemplateController if the Templating Component or the Twig Bundle are not available.'); throw new \LogicException('You can not use the TemplateController if the Templating Component or the Twig Bundle are not available.');
} }

View File

@ -110,5 +110,16 @@
<argument type="service" id="router.request_context" on-invalid="ignore" /> <argument type="service" id="router.request_context" on-invalid="ignore" />
<argument type="service" id="logger" on-invalid="ignore" /> <argument type="service" id="logger" on-invalid="ignore" />
</service> </service>
<service id="Symfony\Bundle\FrameworkBundle\Controller\RedirectController" public="true">
<argument type="service" id="router" />
<argument>%request_listener.http_port%</argument>
<argument>%request_listener.https_port%</argument>
</service>
<service id="Symfony\Bundle\FrameworkBundle\Controller\TemplateController" public="true">
<argument type="service" id="twig" on-invalid="ignore" />
<argument type="service" id="templating" on-invalid="ignore" />
</service>
</services> </services>
</container> </container>

View File

@ -15,6 +15,7 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\RedirectController; use Symfony\Bundle\FrameworkBundle\Controller\RedirectController;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase; use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
@ -66,23 +67,14 @@ class RedirectControllerTest extends TestCase
$request->attributes = new ParameterBag($attributes); $request->attributes = new ParameterBag($attributes);
$router = $this->getMockBuilder('Symfony\Component\Routing\RouterInterface')->getMock(); $router = $this->getMockBuilder(UrlGeneratorInterface::class)->getMock();
$router $router
->expects($this->once()) ->expects($this->once())
->method('generate') ->method('generate')
->with($this->equalTo($route), $this->equalTo($expectedAttributes)) ->with($this->equalTo($route), $this->equalTo($expectedAttributes))
->will($this->returnValue($url)); ->will($this->returnValue($url));
$container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock(); $controller = new RedirectController($router);
$container
->expects($this->once())
->method('get')
->with($this->equalTo('router'))
->will($this->returnValue($router));
$controller = new RedirectController();
$controller->setContainer($container);
$returnResponse = $controller->redirectAction($request, $route, $permanent, $ignoreAttributes); $returnResponse = $controller->redirectAction($request, $route, $permanent, $ignoreAttributes);
@ -130,7 +122,7 @@ class RedirectControllerTest extends TestCase
$this->assertEquals(302, $returnResponse->getStatusCode()); $this->assertEquals(302, $returnResponse->getStatusCode());
} }
public function testUrlRedirectDefaultPortParameters() public function testUrlRedirectDefaultPorts()
{ {
$host = 'www.example.com'; $host = 'www.example.com';
$baseUrl = '/base'; $baseUrl = '/base';
@ -151,6 +143,30 @@ class RedirectControllerTest extends TestCase
$this->assertRedirectUrl($returnValue, $expectedUrl); $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() public function urlRedirectProvider()
{ {
return array( return array(
@ -256,6 +272,14 @@ class RedirectControllerTest extends TestCase
} }
private function createRedirectController($httpPort = null, $httpsPort = null) 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(); $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();

View File

@ -12,8 +12,8 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Controller; namespace Symfony\Bundle\FrameworkBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\TemplateController; use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase; use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\HttpFoundation\Response;
/** /**
* @author Kévin Dunglas <dunglas@gmail.com> * @author Kévin Dunglas <dunglas@gmail.com>
@ -25,6 +25,29 @@ class TemplateControllerTest extends TestCase
$twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock(); $twig = $this->getMockBuilder('Twig\Environment')->disableOriginalConstructor()->getMock();
$twig->expects($this->once())->method('render')->willReturn('bar'); $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 = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
$container->expects($this->at(0))->method('has')->will($this->returnValue(false)); $container->expects($this->at(0))->method('has')->will($this->returnValue(false));
$container->expects($this->at(1))->method('has')->will($this->returnValue(true)); $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()); $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 = $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 = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock();
$container->expects($this->at(0))->method('has')->willReturn(true); $container->expects($this->at(0))->method('has')->willReturn(true);
@ -57,12 +83,7 @@ class TemplateControllerTest extends TestCase
*/ */
public function testNoTwigNorTemplating() 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 = new TemplateController();
$controller->setContainer($container);
$controller->templateAction('mytemplate')->getContent(); $controller->templateAction('mytemplate')->getContent();
} }

View File

@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Session\Storage\Handler\MongoDbSessionHandl
/** /**
* @author Markus Bachmann <markus.bachmann@bachi.biz> * @author Markus Bachmann <markus.bachmann@bachi.biz>
* @group time-sensitive * @group time-sensitive
* @group legacy
*/ */
class MongoDbSessionHandlerTest extends TestCase class MongoDbSessionHandlerTest extends TestCase
{ {