Skip to content

Commit 2c976f7

Browse files
committed
feature #25439 Add ControllerTrait::getParameter() (chalasr)
This PR was merged into the 4.1-dev branch. Discussion ---------- Add ControllerTrait::getParameter() | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25288 (comment) | License | MIT | Doc PR | n/a Commits ------- 28397e5 Add ControllerTrait::getParameter()
2 parents cd4c849 + 28397e5 commit 2c976f7

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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
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

Lines changed: 21 additions & 0 deletions
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,23 @@ 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+
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+
}
531551
}
532552

533553
trait TestControllerTrait
@@ -552,5 +572,6 @@ trait TestControllerTrait
552572
createForm as public;
553573
createFormBuilder as public;
554574
getDoctrine as public;
575+
getParameter as public;
555576
}
556577
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy