8000 [FrameworkBundle] Introduce autowirable ControllerTrait by dunglas · Pull Request #18193 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Introduce autowirable ControllerTrait #18193

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add annotation
  • Loading branch information
dunglas committed Mar 1, 2017
commit dcc7e09faecc512a794d2fdc8d7845a2def0d36c
8000
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,33 @@
*/
trait ControllerTrait
{
/**
* @required
*/
protected function getRouter(): RouterInterface
{
throw new \LogicException(sprintf('An instance of "%s" must be provided.', RouterInterface::class));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the exceptions here as PHP would throw an error anyway thanks to the type hint.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

I've also removed the conditional behavior of the json() method to make the Serializer mandatory:

  • it's a cleaner design (the behavior is predictable)
  • it allows to not catch a TypeError
  • having a proxy method doing return new JsonResponse($data, $status, $headers) is useless

}

/**
* @required
*/
protected function getRequestStack(): RequestStack
{
throw new \LogicException(sprintf('An instance of "%s" must be provided.', RequestStack::class));
}

/**
* @required
*/
protected function getHttpKernel(): HttpKernelInterface
{
throw new \LogicException(sprintf('An instance of "%s" must be provided.', HttpKernelInterface::class));
}

/**
* @required
*/
protected function getSerializer(): SerializerInterface
{
throw new \LogicException(sprintf('An instance of "%s" must be provided.', SerializerInterface::class));
Expand All @@ -70,38 +82,56 @@ protected function getSerializer(): SerializerInterface
* An instance of the Session implementation (and not the interface) is returned because getFlashBag is not part of
* the interface.
*
* @return Session
* @required
*/
protected function getSession(): Session
{
throw new \LogicException(sprintf('An instance of "%s" must be provided.', Session::class));
}

/**
* @required
*/
protected function getAuthorizationChecker(): AuthorizationCheckerInterface
{
throw new \LogicException(sprintf('An instance of "%s" must be provided.', AuthorizationCheckerInterface::class));
}

/**
* @required
*/
protected function getTwig(): \Twig_Environment
{
throw new \LogicException(sprintf('An instance of "%s" must be provided.', \Twig_Environment::class));
}

/**
* @required
*/
protected function getDoctrine(): ManagerRegistry
{
throw new \LogicException(sprintf('An instance of "%s" must be provided.', ManagerRegistry::class));
}

/**
* @required
*/
protected function getFormFactory(): FormFactoryInterface
{
throw new \LogicException(sprintf('An instance of "%s" must be provided.', FormFactoryInterface::class));
}

/**
* @required
*/
protected function getTokenStorage(): TokenStorageInterface
{
throw new \LogicException(sprintf('An instance of "%s" must be provided.', TokenStorageInterface::class));
}

/**
* @required
*/
protected function getCsrfTokenManager(): CsrfTokenManagerInterface
{
throw new \LogicException(sprintf('An instance of "%s" must be provided.', CsrfTokenManagerInterface::class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use PHPUnit\Framework\TestCase as PHPUnitTestCase;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*
* @requires PHP 7
*/
class ControllerTraitTest extends \PHPUnit_Framework_TestCase
class ControllerTraitTest extends PHPUnitTestCase
{
public function testGenerateUrl()
{
Expand Down
0