Skip to content

[FrameworkBundle] Move AbstractController::getParameter() from the trait to the class & use PSR-11 #25460

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
Dec 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Psr\Container\ContainerInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
Expand Down Expand Up @@ -53,6 +52,22 @@ public function setContainer(ContainerInterface $container)
return $previous;
}

/**
* Gets a container parameter by its name.
*
* @return mixed
*
* @final
*/
protected function getParameter(string $name)
{
if (!$this->container->has('parameter_bag')) {
throw new \LogicException('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"');
}

return $this->container->get('parameter_bag')->get($name);
}

public static function getSubscribedServices()
{
return array(
Expand All @@ -68,7 +83,7 @@ public static function getSubscribedServices()
'form.factory' => '?'.FormFactoryInterface::class,
'security.token_storage' => '?'.TokenStorageInterface::class,
'security.csrf.token_manager' => '?'.CsrfTokenManagerInterface::class,
'parameter_bag' => '?'.ContainerBagInterface::class,
'parameter_bag' => '?'.ContainerInterface::class,
);
}
}
16 changes: 0 additions & 16 deletions src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,20 +377,4 @@ protected function isCsrfTokenValid(string $id, string $token): bool

return $this->container->get('security.csrf.token_manager')->isTokenValid(new CsrfToken($id, $token));
}

/**
* Gets a container parameter by its name.
*
* @return mixed
*
* @final
*/
protected function getParameter(string $name)
{
if (!$this->container->has('parameter_bag')) {
throw new \LogicException('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"');
}

return $this->container->get('parameter_bag')->get($name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,33 @@

use Psr\Container\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;

class AbstractControllerTest extends ControllerTraitTest
{
protected function createController()
{
return new TestAbstractController();
}

public function testGetParameter()
{
$container = new Container(new FrozenParameterBag(array('foo' => 'bar')));

$controller = $this->createController();
$controller->setContainer($container);

if (!class_exists(ContainerBag::class)) {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"');
} else {
$container->set('parameter_bag', new ContainerBag($container));
}

$this->assertSame('bar', $controller->getParameter('foo'));
}
}

class TestAbstractController extends AbstractController
Expand Down Expand Up @@ -57,6 +77,11 @@ public function setContainer(ContainerInterface $container)
return parent::setContainer($container);
}

public function getParameter(string $name)
{
return parent::getParameter($name);
}

public function fooAction()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -531,23 +528,6 @@ public function testGetDoctrine()

$this->assertEquals($doctrine, $controller->getDoctrine());
}

public function testGetParameter()
{
$container = new Container(new FrozenParameterBag(array('foo' => 'bar')));

$controller = $this->createController();
$controller->setContainer($container);

if (!interface_exists(ContainerBagInterface::class)) {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"');
} else {
$container->set('parameter_bag', new ContainerBag($container));
}

$this->assertSame('bar', $controller->getParameter('foo'));
}
}

trait TestControllerTrait
Expand All @@ -572,6 +552,5 @@ trait TestControllerTrait
createForm as public;
createFormBuilder as public;
getDoctrine as public;
getParameter as public;
}
}