Skip to content

[Bridge\Doctrine][FrameworkBundle] Remove legacy uses of ContainerAwareInterface #24441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions src/Symfony/Bridge/Doctrine/ManagerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,20 @@
use ProxyManager\Proxy\LazyLoadingInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
use Doctrine\Common\Persistence\AbstractManagerRegistry;

/**
* References Doctrine connections and entity/document managers.
*
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*/
abstract class ManagerRegistry extends AbstractManagerRegistry implements ContainerAwareInterface
abstract class ManagerRegistry extends AbstractManagerRegistry
{
/**
* @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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ protected function 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Symfony\Bundle\FrameworkBundle\Controller;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -26,13 +24,8 @@
*
* @final since version 3.4
*/
class RedirectController implements ContainerAwareInterface
class RedirectController
{
/**
* @deprecated since version 3.4, to be removed in 4.0
*/
protected $container;

private $router;
private $httpPort;
private $httpsPort;
Expand All @@ -44,17 +37,6 @@ public function __construct(UrlGeneratorInterface $router = null, $httpPort = nu
$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.
*
Expand Down Expand Up @@ -104,8 +86,8 @@ public function redirectAction(Request $request, $route, $permanent = false, $ig
* @param string $path The absolute path or URL to redirect to
* @param bool $permanent Whether the redirect is permanent or not
* @param string|null $scheme The URL scheme (null to keep the current one)
* @param int|null $httpPort The HTTP port (null to keep the current one for the same scheme or the configured port in the container)
* @param int|null $httpsPort The HTTPS port (null to keep the current one for the same scheme or the configured port in the container)
* @param int|null $httpPort The HTTP port (null to keep the current one for the same scheme or the default configured port)
* @param int|null $httpsPort The HTTPS port (null to keep the current one for the same scheme or the default configured port)
*
* @return Response A Response instance
*
Expand Down Expand Up @@ -142,9 +124,6 @@ public function urlRedirectAction(Request $request, $path, $permanent = false, $
if (null === $httpPort) {
if ('http' === $request->getScheme()) {
$httpPort = $request->getPort();
} 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;
}
Expand All @@ -157,9 +136,6 @@ public function urlRedirectAction(Request $request, $path, $permanent = false, $
if (null === $httpsPort) {
if ('https' === $request->getScheme()) {
$httpsPort = $request->getPort();
} 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Symfony\Bundle\FrameworkBundle\Controller;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Templating\EngineInterface;
use Twig\Environment;
Expand All @@ -24,13 +22,8 @@
*
* @final since version 3.4
*/
class TemplateController implements ContainerAwareInterface
class TemplateController
{
/**
* @deprecated since version 3.4, to be removed in 4.0
*/
protected $container;

private $twig;
private $templating;

Expand All @@ -40,21 +33,6 @@ public function __construct(Environment $twig = null, EngineInterface $templatin
$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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,30 +143,6 @@ public function testUrlRedirectDefaultPorts()
$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(
Expand Down Expand Up @@ -276,44 +252,6 @@ 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();

if (null !== $httpPort) {
$container
->expects($this->once())
->method('hasParameter')
->with($this->equalTo('request_listener.http_port'))
->will($this->returnValue(true));
$container
->expects($this->once())
->method('getParameter')
->with($this->equalTo('request_listener.http_port'))
->will($this->returnValue($httpPort));
}
if (null !== $httpsPort) {
$container
->expects($this->once())
->method('hasParameter')
->with($this->equalTo('request_listener.https_port'))
->will($this->returnValue(true));
$container
->expects($this->once())
->method('getParameter')
->with($this->equalTo('request_listener.https_port'))
->will($this->returnValue($httpsPort));
}

$controller = new RedirectController();
$controller->setContainer($container);

return $controller;
}

public function assertRedirectUrl(Response $returnResponse, $expectedUrl)
{
$this->assertTrue($returnResponse->isRedirect($expectedUrl), "Expected: $expectedUrl\nGot: ".$returnResponse->headers->get('Location'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,6 @@ public function testTemplating()
$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));
$container->expects($this->at(2))->method('get')->will($this->returnValue($twig));

$controller = new TemplateController();
$controller->setContainer($container);

$this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent());
}

/**
* @group legacy
*/
public function testLegacyTemplating()
{
$templating = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface')->getMock();
$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);
$container->expects($this->at(1))->method('get')->will($this->returnValue($templating));

$controller = new TemplateController();
$controller->setContainer($container);

$this->assertEquals('bar', $controller->templateAction('mytemplate')->getContent());
}

/**
* @expectedException \LogicException
* @expectedExceptionMessage You can not use the TemplateController if the Templating Component or the Twig Bundle are not available.
Expand Down