Skip to content

Add ControllerTrait::getParameter() #25439

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 11, 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,6 +13,7 @@

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

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

/**
* Gets a container configuration 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 @@ -14,6 +14,9 @@
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 @@ -528,6 +531,23 @@ 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 @@ -552,5 +572,6 @@ trait TestControllerTrait
createForm as public;
createFormBuilder as public;
getDoctrine as public;
getParameter as public;
}
}