Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2c976f7

Browse filesBrowse 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
Copy full SHA for 2c976f7

File tree

3 files changed

+39
-0
lines changed
Filter options

3 files changed

+39
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php
+2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
+16Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php
+21Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.