Skip to content

Commit 96d41b7

Browse files
committed
Add ControllerTrait::getParameter()
1 parent efc19fc commit 96d41b7

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Container\ContainerInterface;
1515
use Doctrine\Common\Persistence\ManagerRegistry;
16+
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
1617
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
1718
use Symfony\Component\Form\FormFactoryInterface;
1819
use Symfony\Component\HttpFoundation\RequestStack;
@@ -67,6 +68,7 @@ public static function getSubscribedServices()
6768
'form.factory' => '?'.FormFactoryInterface::class,
6869
'security.token_storage' => '?'.TokenStorageInterface::class,
6970
'security.csrf.token_manager' => '?'.CsrfTokenManagerInterface::class,
71+
'parameter_bag' => '?'.ContainerBagInterface::class,
7072
);
7173
}
7274
}

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

+16
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,20 @@ 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 configuration parameter by its name.
383+
*
384+
* @return mixed
385+
*
386+
* @final since version 3.4
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+
}
380396
}

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

+26
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
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;
1720
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1821
use Symfony\Component\HttpFoundation\File\File;
1922
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -528,6 +531,28 @@ public function testGetDoctrine()
528531

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

533558
trait TestControllerTrait
@@ -552,5 +577,6 @@ trait TestControllerTrait
552577
createForm as public;
553578
createFormBuilder as public;
554579
getDoctrine as public;
580+
getParameter as public;
555581
}
556582
}

0 commit comments

Comments
 (0)