Skip to content

Commit 193b98a

Browse files
minor symfony#25460 [FrameworkBundle] Move AbstractController::getParameter() from the trait to the class & use PSR-11 (nicolas-grekas)
This PR was merged into the 4.1-dev branch. Discussion ---------- [FrameworkBundle] Move AbstractController::getParameter() from the trait to the class & use PSR-11 | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Feels more specific this way to me. Ping @chalasr Commits ------- 3051289 [FrameworkBundle] Move AbstractController::getParameter() from the trait to the class & use PSR-11
2 parents 72e189a + 3051289 commit 193b98a

File tree

4 files changed

+42
-39
lines changed

4 files changed

+42
-39
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Psr\Container\ContainerInterface;
1515
use Doctrine\Common\Persistence\ManagerRegistry;
16-
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
1716
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
1817
use Symfony\Component\Form\FormFactoryInterface;
1918
use Symfony\Component\HttpFoundation\RequestStack;
@@ -53,6 +52,22 @@ public function setContainer(ContainerInterface $container)
5352
return $previous;
5453
}
5554

55+
/**
56+
* Gets a container parameter by its name.
57+
*
58+
* @return mixed
59+
*
60+
* @final
61+
*/
62+
protected function getParameter(string $name)
63+
{
64+
if (!$this->container->has('parameter_bag')) {
65+
throw new \LogicException('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"');
66+
}
67+
68+
return $this->container->get('parameter_bag')->get($name);
69+
}
70+
5671
public static function getSubscribedServices()
5772
{
5873
return array(
@@ -68,7 +83,7 @@ public static function getSubscribedServices()
6883
'form.factory' => '?'.FormFactoryInterface::class,
6984
'security.token_storage' => '?'.TokenStorageInterface::class,
7085
'security.csrf.token_manager' => '?'.CsrfTokenManagerInterface::class,
71-
'parameter_bag' => '?'.ContainerBagInterface::class,
86+
'parameter_bag' => '?'.ContainerInterface::class,
7287
);
7388
}
7489
}

src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -377,20 +377,4 @@ protected function isCsrfTokenValid(string $id, string $token): bool
377377

378378
return $this->container->get('security.csrf.token_manager')->isTokenValid(new CsrfToken($id, $token));
379379
}
380-
381-
/**
382-
* Gets a container parameter by its name.
383-
*
384-
* @return mixed
385-
*
386-
* @final
387-
*/
388-
protected function getParameter(string $name)
389-
{
390-
if (!$this->container->has('parameter_bag')) {
391-
throw new \LogicException('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"');
392-
}
393-
394-
return $this->container->get('parameter_bag')->get($name);
395-
}
396380
}

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,33 @@
1313

1414
use Psr\Container\ContainerInterface;
1515
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
16+
use Symfony\Component\DependencyInjection\Container;
17+
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
18+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
1619

1720
class AbstractControllerTest extends ControllerTraitTest
1821
{
1922
protected function createController()
2023
{
2124
return new TestAbstractController();
2225
}
26+
27+
public function testGetParameter()
28+
{
29+
$container = new Container(new FrozenParameterBag(array('foo' => 'bar')));
30+
31+
$controller = $this->createController();
32+
$controller->setContainer($container);
33+
34+
if (!class_exists(ContainerBag::class)) {
35+
$this->expectException(\LogicException::class);
36+
$this->expectExceptionMessage('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"');
37+
} else {
38+
$container->set('parameter_bag', new ContainerBag($container));
39+
}
40+
41+
$this->assertSame('bar', $controller->getParameter('foo'));
42+
}
2343
}
2444

2545
class TestAbstractController extends AbstractController
@@ -57,6 +77,11 @@ public function setContainer(ContainerInterface $container)
5777
return parent::setContainer($container);
5878
}
5979

80+
public function getParameter(string $name)
81+
{
82+
return parent::getParameter($name);
83+
}
84+
6085
public function fooAction()
6186
{
6287
}

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1515
use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait;
1616
use Symfony\Component\DependencyInjection\Container;
17-
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
18-
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
19-
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
2017
use Symfony\Component\HttpFoundation\BinaryFileResponse;
2118
use Symfony\Component\HttpFoundation\File\File;
2219
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -531,23 +528,6 @@ public function testGetDoctrine()
531528

532529
$this->assertEquals($doctrine, $controller->getDoctrine());
533530
}
534-
535-
public function testGetParameter()
536-
{
537-
$container = new Container(new FrozenParameterBag(array('foo' => 'bar')));
538-
539-
$controller = $this->createController();
540-
$controller->setContainer($container);
541-
542-
if (!interface_exists(ContainerBagInterface::class)) {
543-
$this->expectException(\LogicException::class);
544-
$this->expectExceptionMessage('The "parameter_bag" service is not available. Try running "composer require dependency-injection:^4.1"');
545-
} else {
546-
$container->set('parameter_bag', new ContainerBag($container));
547-
}
548-
549-
$this->assertSame('bar', $controller->getParameter('foo'));
550-
}
551531
}
552532

553533
trait TestControllerTrait
@@ -572,6 +552,5 @@ trait TestControllerTrait
572552
createForm as public;
573553
createFormBuilder as public;
574554
getDoctrine as public;
575-
getParameter as public;
576555
}
577556
}

0 commit comments

Comments
 (0)