Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5e82520

Browse files
committedJan 5, 2022
wip
1 parent 70103a2 commit 5e82520

File tree

6 files changed

+61
-67
lines changed

6 files changed

+61
-67
lines changed
 

‎src/Config/TypeDefinition.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ protected function callbackNormalization(NodeDefinition $node, string $new, stri
161161
->then(function ($options) use ($old, $new) {
162162
if (is_callable($options[$old])) {
163163
if (is_array($options[$old])) {
164-
$options[$new]['method'] = implode('::', $options[$old]);
164+
$options[$new]['function'] = implode('::', $options[$old]);
165165
} else {
166-
$options[$new]['method'] = $options[$old];
166+
$options[$new]['function'] = $options[$old];
167167
}
168168
} elseif (is_string($options[$old])) {
169169
$options[$new]['expression'] = ExpressionLanguage::stringHasTrigger($options[$old]) ?
@@ -203,8 +203,8 @@ protected function callbackSection(string $name, string $info): ArrayNodeDefinit
203203
$node
204204
->info($info)
205205
->validate()
206-
->ifTrue(fn (array $v) => !empty($v['method']) && !empty($v['expression']))
207-
->thenInvalid('"method" and "expression" should not be use together.')
206+
->ifTrue(fn (array $v) => !empty($v['function']) && !empty($v['expression']))
207+
->thenInvalid('"function" and "expression" should not be use together.')
208208
->end()
209209
->beforeNormalization()
210210
// Allow short syntax
@@ -213,12 +213,11 @@ protected function callbackSection(string $name, string $info): ArrayNodeDefinit
213213
->end()
214214
->beforeNormalization()
215215
->ifTrue(fn ($options) => is_string($options) && !ExpressionLanguage::stringHasTrigger($options))
216-
->then(fn ($options) => ['method' => $options])
216+
->then(fn ($options) => ['function' => $options])
217217
->end()
218218
->children()
219-
->scalarNode('method')->end()
219+
->scalarNode('function')->end()
220220
->scalarNode('expression')->end()
221-
->scalarNode('id')->end()
222221
->end()
223222
;
224223

‎src/DependencyInjection/Compiler/IdentifyCallbackServiceIdsPass.php

+3-20
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,10 @@ public function process(ContainerBuilder $container): void
4848

4949
private function resolveServiceIdAndMethod(ContainerBuilder $container, ?array &$callback): void
5050
{
51-
if (!isset($callback['id']) && !isset($callback['method'])) {
51+
if (!isset($callback['function'])) {
5252
return;
5353
}
54-
$originalId = $callback['id'] ?? null;
55-
$originalMethod = $callback['method'] ?? null;
56-
57-
if (null === $originalId) {
58-
[$id, $method] = explode('::', $originalMethod, 2) + [null, null];
59-
$throw = false;
60-
} else {
61-
$id = $originalId;
62-
$method = $originalMethod;
63-
$throw = true;
64-
}
54+
[$id, $method] = explode('::', $callback['function'], 2) + [null, null];
6555

6656
try {
6757
$definition = $container->getDefinition($id);
@@ -72,12 +62,6 @@ private function resolveServiceIdAndMethod(ContainerBuilder $container, ?array &
7262
$id = (string) $alias;
7363
$definition = $container->getDefinition($id);
7464
} catch (ServiceNotFoundException|InvalidArgumentException $e) {
75-
if ($throw) {
76-
throw $e;
77-
}
78-
$callback['id'] = null;
79-
$callback['method'] = $originalMethod;
80-
8165
return;
8266
}
8367
}
@@ -88,7 +72,6 @@ private function resolveServiceIdAndMethod(ContainerBuilder $container, ?array &
8872
$definition->addTag('overblog_graphql.service');
8973
}
9074

91-
$callback['id'] = $id;
92-
$callback['method'] = $method;
75+
$callback['function'] = "$id::$method";
9376
}
9477
}

‎src/Generator/Config/Callback.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
final class Callback extends AbstractConfig
88
{
9-
public ?string $method = null;
9+
public ?string $function = null;
1010
public ?string $expression = null;
11-
public ?string $id = null;
1211
}

‎src/Generator/TypeBuilder.php

+29-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Murtukov\PHPCodeGenerator\Config;
1818
use Murtukov\PHPCodeGenerator\ConverterInterface;
1919
use Murtukov\PHPCodeGenerator\GeneratorInterface;
20+
use Murtukov\PHPCodeGenerator\IfElse;
2021
use Murtukov\PHPCodeGenerator\Instance;
2122
use Murtukov\PHPCodeGenerator\Literal;
2223
use Murtukov\PHPCodeGenerator\PhpFile;
@@ -885,17 +886,37 @@ private function buildCallback(Callback $callback, array $argNames, ?callable $e
885886
} else {
886887
return $expressionBuilder($callback->expression);
887888
}
888-
} elseif (null !== $callback->id) {
889-
$fnExpression = "$this->gqlServices->get('$callback->id')";
890-
if (null !== $callback->method) {
891-
$fnExpression = "[$fnExpression, '$callback->method']";
889+
} else {
890+
if (str_contains($callback->function, '::')) {
891+
$function = explode('::', $callback->function, 2);
892+
$isArray = true;
893+
} else {
894+
$function = $callback->function;
895+
$isArray = false;
892896
}
893897

894-
return ArrowFunction::new()
898+
$resolverExpression = IfElse::new("$this->gqlServices->has('".($isArray ? $function[0] : $function)."')");
899+
if ($isArray) {
900+
$resolverExpression
901+
->append('$resolver = ', "[$this->gqlServices->get('$function[0]'), '$function[1]']")
902+
->createElse()
903+
->append('$resolver = ', "'$callback->function'")
904+
->end()
905+
;
906+
} else {
907+
$resolverExpression
908+
->append('$resolver = ', "$this->gqlServices->get('$function')")
909+
->createElse()
910+
->append('$resolver = ', "'$function'")
911+
->end()
912+
;
913+
}
914+
915+
return Closure::new()
895916
->addArguments(...$argNames)
896-
->setExpression(Literal::new("($fnExpression)(...\\func_get_args())"));
897-
} else {
898-
return Literal::new("'$callback->method'");
917+
->bindVar(TypeGenerator::GRAPHQL_SERVICES)
918+
->append($resolverExpression)
919+
->append('return $resolver(...\\func_get_args())');
899920
}
900921
}
901922

‎tests/Functional/App/config/connection/mapping/connection.types.yaml

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ User:
1717
type: friendConnection
1818
argsBuilder: "Relay::Connection"
1919
resolver:
20-
method: 'overblog_graphql.test.resolver.node::friendsResolver'
20+
function: 'overblog_graphql.test.resolver.node::friendsResolver'
2121
friendsForward:
2222
type: userConnection
2323
argsBuilder: "Relay::ForwardConnection"
24-
resolver:
25-
id: 'overblog_graphql.test.resolver.node'
26-
method: 'friendsResolver'
24+
resolver: 'overblog_graphql.test.resolver.node::friendsResolver'
2725
friendsBackward:
2826
type: userConnection
2927
argsBuilder: "Relay::BackwardConnection"

‎tests/Functional/Controller/GraphControllerTest.php

+20-26
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ final class GraphControllerTest extends TestCase
6666
*/
6767
public function testEndpointAction(string $uri): void
6868
{
69-
$client = static::createClient(['test_case' => 'connectionWithCORS']);
69+
$client = self::createClient(['test_case' => 'connectionWithCORS']);
7070
$this->disableCatchExceptions($client);
7171

7272
$client->request('GET', $uri, ['query' => $this->friendsQuery], [], ['CONTENT_TYPE' => 'application/graphql;charset=utf8', 'HTTP_Origin' => 'http://example.com']);
@@ -87,7 +87,7 @@ public function testEndpointWithEmptyQuery(): void
8787
{
8888
$this->expectException(BadRequestHttpException::class);
8989
$this->expectExceptionMessage('Must provide query parameter');
90-
$client = static::createClient();
90+
$client = self::createClient();
9191
$this->disableCatchExceptions($client);
9292
$client->request('GET', '/', []);
9393
$client->getResponse()->getContent();
@@ -97,14 +97,14 @@ public function testEndpointWithEmptyPostJsonBodyQuery(): void
9797
{
9898
$this->expectException(BadRequestHttpException::class);
9999
$this->expectExceptionMessage('The request content body must not be empty when using json content type request.');
100-
$client = static::createClient();
100+
$client = self::createClient();
101101
$this->disableCatchExceptions($client);
102102
$client->request('POST', '/', [], [], ['CONTENT_TYPE' => 'application/json']);
103103
}
104104

105105
public function testEndpointWithJsonContentTypeAndGetQuery(): void
106106
{
107-
$client = static::createClient(['test_case' => 'connectionWithCORS']);
107+
$client = self::createClient(['test_case' => 'connectionWithCORS']);
108108
$this->disableCatchExceptions($client);
109109
$client->request('GET', '/', ['query' => $this->friendsQuery], [], ['CONTENT_TYPE' => 'application/json']);
110110
$result = $client->getResponse()->getContent();
@@ -115,15 +115,15 @@ public function testEndpointWithInvalidBodyQuery(): void
115115
{
116116
$this->expectException(BadRequestHttpException::class);
117117
$this->expectExceptionMessage('POST body sent invalid JSON');
118-
$client = static::createClient();
118+
$client = self::createClient();
119119
$this->disableCatchExceptions($client);
120120
$client->request('GET', '/', [], [], ['CONTENT_TYPE' => 'application/json'], '{');
121121
$client->getResponse()->getContent();
122122
}
123123

124124
public function testEndpointActionWithVariables(): void
125125
{
126-
$client = static::createClient(['test_case' => 'connection']);
126+
$client = self::createClient(['test_case' => 'connection']);
127127
$this->disableCatchExceptions($client);
128128

129129
$query = <<<'EOF'
@@ -151,7 +151,7 @@ public function testEndpointActionWithInvalidVariables(): void
151151
{
152152
$this->expectException(BadRequestHttpException::class);
153153
$this->expectExceptionMessage('Variables are invalid JSON');
154-
$client = static::createClient(['test_case' => 'connection']);
154+
$client = self::createClient(['test_case' => 'connection']);
155155
$this->disableCatchExceptions($client);
156156

157157
$query = <<<'EOF'
@@ -167,7 +167,7 @@ public function testMultipleEndpointActionWithUnknownSchemaName(): void
167167
{
168168
$this->expectException(NotFoundHttpException::class);
169169
$this->expectExceptionMessage('Could not find "fake" schema.');
170-
$client = static::createClient(['test_case' => 'connection']);
170+
$client = self::createClient(['test_case' => 'connection']);
171171
$this->disableCatchExceptions($client);
172172

173173
$query = <<<'EOF'
@@ -181,7 +181,7 @@ public function testMultipleEndpointActionWithUnknownSchemaName(): void
181181

182182
public function testEndpointActionWithOperationName(): void
183183
{
184-
$client = static::createClient(['test_case' => 'connection']);
184+
$client = self::createClient(['test_case' => 'connection']);
185185
$this->disableCatchExceptions($client);
186186

187187
$query = $this->friendsQuery."\n".$this->friendsTotalCountQuery;
@@ -196,7 +196,7 @@ public function testEndpointActionWithOperationName(): void
196196
*/
197197
public function testBatchEndpointAction(string $uri): void
198198
{
199-
$client = static::createClient(['test_case' => 'connection']);
199+
$client = self::createClient(['test_case' => 'connection']);
200200
$this->disableCatchExceptions($client);
201201

202202
$data = [
@@ -233,7 +233,7 @@ public function testBatchEndpointWithEmptyQuery(): void
233233
{
234234
$this->expectException(BadRequestHttpException::class);
235235
$this->expectExceptionMessage('Must provide at least one valid query.');
236-
$client = static::createClient();
236+
$client = self::createClient();
237237
$this->disableCatchExceptions($client);
238238
$client->request('GET', '/batch', [], [], ['CONTENT_TYPE' => 'application/json'], '{}');
239239
$client->getResponse()->getContent();
@@ -243,7 +243,7 @@ public function testBatchEndpointWrongContentType(): void
243243
{
244244
$this->expectException(BadRequestHttpException::class);
245245
$this->expectExceptionMessage('Batching parser only accepts "application/json" or "multipart/form-data" content-type but got "".');
246-
$client = static::createClient();
246+
$client = self::createClient();
247247
$this->disableCatchExceptions($client);
248248
$client->request('GET', '/batch');
249249
$client->getResponse()->getContent();
@@ -253,7 +253,7 @@ public function testBatchEndpointWithInvalidJson(): void
253253
{
254254
$this->expectException(BadRequestHttpException::class);
255255
$this->expectExceptionMessage('POST body sent invalid JSON');
256-
$client = static::createClient();
256+
$client = self::createClient();
257257
$this->disableCatchExceptions($client);
258258
$client->request('GET', '/batch', [], [], ['CONTENT_TYPE' => 'application/json'], '{');
259259
$client->getResponse()->getContent();
@@ -263,15 +263,15 @@ public function testBatchEndpointWithInvalidQuery(): void
263263
{
264264
$this->expectException(BadRequestHttpException::class);
265265
$this->expectExceptionMessage('1 is not a valid query');
266-
$client = static::createClient();
266+
$client = self::createClient();
267267
$this->disableCatchExceptions($client);
268268
$client->request('GET', '/batch', [], [], ['CONTENT_TYPE' => 'application/json'], '{"test" : {"query": 1}}');
269269
$client->getResponse()->getContent();
270270
}
271271

272272
public function testPreflightedRequestWhenDisabled(): void
273273
{
274-
$client = static::createClient(['test_case' => 'connection']);
274+
$client = self::createClient(['test_case' => 'connection']);
275275
$this->disableCatchExceptions($client);
276276
$client->request('OPTIONS', '/', [], [], ['HTTP_Origin' => 'http://example.com']);
277277
$response = $client->getResponse();
@@ -281,34 +281,31 @@ public function testPreflightedRequestWhenDisabled(): void
281281

282282
public function testUnAuthorizedMethod(): void
283283
{
284-
$client = static::createClient(['test_case' => 'connection']);
284+
$client = self::createClient(['test_case' => 'connection']);
285285
$this->disableCatchExceptions($client);
286286
$client->request('PUT', '/', [], [], ['HTTP_Origin' => 'http://example.com']);
287287
$this->assertSame(405, $client->getResponse()->getStatusCode());
288288
}
289289

290290
public function testPreflightedRequestWhenEnabled(): void
291291
{
292-
$client = static::createClient(['test_case' => 'connectionWithCORS']);
292+
$client = self::createClient(['test_case' => 'connectionWithCORS']);
293293
$this->disableCatchExceptions($client);
294294
$client->request('OPTIONS', '/batch', [], [], ['HTTP_Origin' => 'http://example.com']);
295295
$this->assertCORSHeadersExists($client);
296296
}
297297

298298
public function testNoCORSHeadersIfOriginHeaderNotExists(): void
299299
{
300-
$client = static::createClient(['test_case' => 'connectionWithCORS']);
300+
$client = self::createClient(['test_case' => 'connectionWithCORS']);
301301
$this->disableCatchExceptions($client);
302302
$client->request('GET', '/', ['query' => $this->friendsQuery], [], ['CONTENT_TYPE' => 'application/graphql']);
303303
$result = $client->getResponse()->getContent();
304304
$this->assertSame(['data' => $this->expectedData], json_decode($result, true), $result);
305305
$this->assertCORSHeadersNotExists($client);
306306
}
307307

308-
/**
309-
* @param KernelBrowser $client
310-
*/
311-
private function assertCORSHeadersNotExists($client): void
308+
private function assertCORSHeadersNotExists(KernelBrowser $client): void
312309
{
313310
$headers = $client->getResponse()->headers->all();
314311
$this->assertArrayNotHasKey('access-control-allow-origin', $headers);
@@ -318,10 +315,7 @@ private function assertCORSHeadersNotExists($client): void
318315
$this->assertArrayNotHasKey('access-control-max-age', $headers);
319316
}
320317

321-
/**
322-
* @param KernelBrowser $client
323-
*/
324-
private function assertCORSHeadersExists($client): void
318+
private function assertCORSHeadersExists(KernelBrowser $client): void
325319
{
326320
$response = $client->getResponse();
327321
$this->assertSame(200, $response->getStatusCode());

0 commit comments

Comments
 (0)
Please sign in to comment.