Skip to content

Navigation Menu

Sign in
Appearance settings

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

[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
Remove exceptions and the conditional behavior of json()
  • Loading branch information
dunglas committed Mar 2, 2017
commit f2e54f660e90126824b0706653c222adb41fe9e7
29 changes: 7 additions & 22 deletions 29 src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,27 @@ trait ControllerTrait
*/
protected function getRouter(): RouterInterface
{
throw new \LogicException(sprintf('An instance of "%s" must be provided.', RouterInterface::class));
}

/**
* @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 @@ -86,55 +82,48 @@ protected function getSerializer(): SerializerInterface
*/
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 Expand Up @@ -200,26 +189,22 @@ protected function redirectToRoute(string $route, array $parameters = array(), i
}

/**
* Returns a JsonResponse that uses the serializer component if enabled, or json_encode.
* Returns a JsonResponse that uses the serializer component.
*
* @param mixed $data The response data
* @param int $status The status code to use for the Response
* @param array $headers Array of extra headers to add
* @param array $context Context to pass to serializer when using serializer component
* @param array $context Context to pass to serializer
*
* @return JsonResponse
*/
protected function json($data, int $status = 200, array $headers = array(), array $context = array()): JsonResponse
{
try {
$json = $this->getSerializer()->serialize($data, 'json', array_merge(array(
'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS,
), $context));

return new JsonResponse($json, $status, $headers, true);
} catch (\LogicException $e) {
return new JsonResponse($data, $status, $headers);
}
$json = $this->getSerializer()->serialize($data, 'json', array_merge(array(
'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS,
), $context));

return new JsonResponse($json, $status, $headers, true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,6 @@ public function testGetUserWithEmptyTokenStorage()
$this->assertNull($controller->getUser());
}

public function testJson()
{
$controller = new UseControllerTraitController();

$response = $controller->json(array());
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals('[]', $response->getContent());
}

public function testJsonWithSerializer()
{
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.