8000 Remove exceptions and the conditional behavior of json() · symfony/symfony@f2e54f6 · GitHub
[go: up one dir, main page]

Skip to content

Commit f2e54f6

Browse files
committed
Remove exceptions and the conditional behavior of json()
1 parent dcc7e09 commit f2e54f6

File tree

2 files changed

+7
-31
lines changed

2 files changed

+7
-31
lines changed

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,27 @@ trait ControllerTrait
5151
*/
5252
protected function getRouter(): RouterInterface
5353
{
54-
throw new \LogicException(sprintf('An instance of "%s" must be provided.', RouterInterface::class));
5554
}
5655

5756
/**
5857
* @required
5958
*/
6059
protected function getRequestStack(): RequestStack
6160
{
62-
throw new \LogicException(sprintf('An instance of "%s" must be provided.', RequestStack::class));
6361
}
6462

6563
/**
6664
* @required
6765
*/
6866
protected function getHttpKernel(): HttpKernelInterface
6967
{
70-
throw new \LogicException(sprintf('An instance of "%s" must be provided.', HttpKernelInterface::class));
7168
}
7269

7370
/**
7471
* @required
7572
*/
7673
protected function getSerializer(): SerializerInterface
7774
{
78-
throw new \LogicException(sprintf('An instance of "%s" must be provided.', SerializerInterface::class));
7975
}
8076

8177
/**
@@ -86,55 +82,48 @@ protected function getSerializer(): SerializerInterface
8682
*/
8783
protected function getSession(): Session
8884
{
89-
throw new \LogicException(sprintf('An instance of "%s" must be provided.', Session::class));
9085
}
9186

9287
/**
9388
* @required
9489
*/
9590
protected function getAuthorizationChecker(): AuthorizationCheckerInterface
9691
{
97-
throw new \LogicException(sprintf('An instance of "%s" must be provided.', AuthorizationCheckerInterface::class));
9892
}
9993

10094
/**
10195
* @required
10296
*/
10397
protected function getTwig(): \Twig_Environment
10498
{
105-
throw new \LogicException(sprintf('An instance of "%s" must be provided.', \Twig_Environment::class));
10699
}
107100

108101
/**
109102
* @required
110103
*/
111104
protected function getDoctrine(): ManagerRegistry
112105
{
113-
throw new \LogicException(sprintf('An instance of "%s" must be provided.', ManagerRegistry::class));
114106
}
115107

116108
/**
117109
* @required
118110
*/
119111
protected function getFormFactory(): FormFactoryInterface
120112
{
121-
throw new \LogicException(sprintf('An instance of "%s" must be provided.', FormFactoryInterface::class));
122113
}
123114

124115
/**
125116
* @required
126117
*/
127118
protected function getTokenStorage(): TokenStorageInterface
128119
{
129-
throw new \LogicException(sprintf('An instance of "%s" must be provided.', TokenStorageInterface::class));
130120
}
131121

132122
/**
133123
* @required
134124
*/
135125
protected function getCsrfTokenManager(): CsrfTokenManagerInterface
136126
{
137-
throw new \LogicException(sprintf('An instance of "%s" must be provided.', CsrfTokenManagerInterface::class));
138127
}
139128

140129
/**
@@ -200,26 +189,22 @@ protected function redirectToRoute(string $route, array $parameters = array(), i
200189
}
201190

202191
/**
203-
* Returns a JsonResponse that uses the serializer component if enabled, or json_encode.
192+
* Returns a JsonResponse that uses the serializer component.
204193
*
205194
* @param mixed $data The response data
206195
* @param int $status The status code to use for the Response
207196
* @param array $headers Array of extra headers to add
208-
* @param array $context Context to pass to serializer when using serializer component
197+
* @param array $context Context to pass to serializer
209198
*
210199
* @return JsonResponse
211200
*/
212201
protected function json($data, int $status = 200, array $headers = array(), array $context = array()): JsonResponse
213202
{
214-
try {
215-
$json = $this->getSerializer()->serialize($data, 'json', array_merge(array(
216-
'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS,
217-
), $context));
218-
219-
return new JsonResponse($json, $status, $headers, true);
220-
} catch (\LogicException $e) {
221-
return new JsonResponse($data, $status, $headers);
222-
}
203+
$json = $this->getSerializer()->serialize($data, 'json', array_merge(array(
204+
'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS,
205+
), $context));
206+
207+
return new JsonResponse($json, $status, $headers, true);
223208
}
224209

225210
/**

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTraitTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,6 @@ public function testGetUserWithEmptyTokenStorage()
147147
$this->assertNull($controller->getUser());
148148
}
149149

150-
public function testJson()
151-
{
152-
$controller = new UseControllerTraitController();
153-
154-
$response = $controller->json(array());
155-
$this->assertInstanceOf(JsonResponse::class, $response);
156-
$this->assertEquals('[]', $response->getContent());
157-
}
158-
159150
public function testJsonWithSerializer()
160151
{
161152
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();

0 commit comments

Comments
 (0)
0