1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\ExpressionLanguage; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\Generator\TypeGenerator; |
15
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunction; |
16
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface; |
17
|
|
|
|
18
|
|
|
class ConfigExpressionProvider implements ExpressionFunctionProviderInterface |
19
|
|
|
{ |
20
|
24 |
|
public function getFunctions() |
21
|
|
|
{ |
22
|
|
|
return [ |
23
|
24 |
|
new ExpressionFunction( |
24
|
24 |
|
'service', |
25
|
|
|
function ($value) { |
26
|
3 |
|
return sprintf('$container->get(%s)', $value); |
27
|
24 |
|
}, |
28
|
|
|
function () {} |
29
|
24 |
|
), |
30
|
|
|
|
31
|
24 |
|
new ExpressionFunction( |
32
|
24 |
|
'parameter', |
33
|
|
|
function ($value) { |
34
|
1 |
|
return sprintf('$container->getParameter(%s)', $value); |
35
|
24 |
|
}, |
36
|
|
|
function () {} |
37
|
24 |
|
), |
38
|
|
|
|
39
|
24 |
|
new ExpressionFunction( |
40
|
24 |
|
'isTypeOf', |
41
|
|
|
function ($className) { |
42
|
1 |
|
return sprintf('($className = %s) && $value instanceof $className', $className); |
43
|
24 |
|
}, |
44
|
|
|
function () {} |
45
|
24 |
|
), |
46
|
|
|
|
47
|
24 |
|
new ExpressionFunction( |
48
|
24 |
|
'resolver', |
49
|
|
|
function ($alias, $args = '[]') { |
50
|
7 |
|
return sprintf('$container->get(\'overblog_graphql.resolver_resolver\')->resolve([%s, %s])', $alias, $args); |
51
|
24 |
|
}, |
52
|
|
|
function () {} |
53
|
24 |
|
), |
54
|
|
|
|
55
|
24 |
|
new ExpressionFunction( |
56
|
24 |
|
'mutateAndGetPayloadCallback', |
57
|
|
View Code Duplication |
function ($mutateAndGetPayload) { |
|
|
|
|
58
|
|
|
$code = 'function ($value) use ('.TypeGenerator::USE_FOR_CLOSURES.', $args, $info) { '; |
59
|
|
|
$code .= 'return '.$mutateAndGetPayload.'; }'; |
60
|
|
|
|
61
|
|
|
return $code; |
62
|
24 |
|
}, |
63
|
|
|
function () {} |
64
|
24 |
|
), |
65
|
|
|
|
66
|
24 |
|
new ExpressionFunction( |
67
|
24 |
|
'mutateAndGetPayloadCallback', |
68
|
|
View Code Duplication |
function ($mutateAndGetPayload) { |
|
|
|
|
69
|
2 |
|
$code = 'function ($value) use ('.TypeGenerator::USE_FOR_CLOSURES.', $args, $info) { '; |
70
|
2 |
|
$code .= 'return '.$mutateAndGetPayload.'; }'; |
71
|
|
|
|
72
|
2 |
|
return $code; |
73
|
24 |
|
}, |
74
|
|
|
function () {} |
75
|
24 |
|
), |
76
|
|
|
|
77
|
24 |
|
new ExpressionFunction( |
78
|
24 |
|
'idFetcherCallback', |
79
|
|
|
function ($idFetcher) { |
80
|
2 |
|
$code = 'function ($value) use ('.TypeGenerator::USE_FOR_CLOSURES.', $args, $info) { '; |
81
|
2 |
|
$code .= 'return '.$idFetcher.'; }'; |
82
|
|
|
|
83
|
2 |
|
return $code; |
84
|
24 |
|
}, |
85
|
|
|
function () {} |
86
|
24 |
|
), |
87
|
|
|
|
88
|
24 |
|
new ExpressionFunction( |
89
|
24 |
|
'resolveSingleInputCallback', |
90
|
|
|
function ($resolveSingleInput) { |
91
|
1 |
|
$code = 'function ($value) use ('.TypeGenerator::USE_FOR_CLOSURES.', $args, $info) { '; |
92
|
1 |
|
$code .= 'return '.$resolveSingleInput.'; }'; |
93
|
|
|
|
94
|
1 |
|
return $code; |
95
|
24 |
|
}, |
96
|
|
|
function () {} |
97
|
24 |
|
), |
98
|
|
|
|
99
|
24 |
|
new ExpressionFunction( |
100
|
24 |
|
'mutation', |
101
|
|
|
function ($alias, $args = '[]') { |
102
|
2 |
|
return sprintf('$container->get(\'overblog_graphql.mutation_resolver\')->resolve([%s, %s])', $alias, $args); |
103
|
24 |
|
}, |
104
|
|
|
function () {} |
105
|
24 |
|
), |
106
|
|
|
|
107
|
24 |
|
new ExpressionFunction( |
108
|
24 |
|
'globalId', |
109
|
|
|
function ($id, $typeName = null) { |
110
|
1 |
|
$typeNameEmpty = null === $typeName || '""' === $typeName || 'null' === $typeName || 'false' === $typeName; |
111
|
|
|
|
112
|
1 |
|
return sprintf( |
113
|
1 |
|
'\\Overblog\\GraphQLBundle\\Relay\\Node\\GlobalId::toGlobalId(%s, %s)', |
114
|
1 |
|
sprintf($typeNameEmpty ? '$info->parentType->name' : '%s', $typeName), |
115
|
|
|
$id |
116
|
1 |
|
); |
117
|
24 |
|
}, |
118
|
|
|
function () {} |
119
|
24 |
|
), |
120
|
|
|
|
121
|
24 |
|
new ExpressionFunction( |
122
|
24 |
|
'fromGlobalId', |
123
|
|
|
function ($globalId) { |
124
|
1 |
|
return sprintf( |
125
|
1 |
|
'\\Overblog\\GraphQLBundle\\Relay\\Node\\GlobalId::fromGlobalId(%s)', |
126
|
|
|
$globalId |
127
|
1 |
|
); |
128
|
24 |
|
}, |
129
|
|
|
function () {} |
130
|
24 |
|
), |
131
|
|
|
|
132
|
24 |
|
new ExpressionFunction( |
133
|
24 |
|
'newObject', |
134
|
|
|
function ($className, $args = '[]') { |
135
|
1 |
|
return sprintf('(new \ReflectionClass(%s))->newInstanceArgs(%s)', $className, $args); |
136
|
24 |
|
}, |
137
|
|
|
function () {} |
138
|
24 |
|
), |
139
|
24 |
|
]; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.