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\Resolver; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
15
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
16
|
|
|
|
17
|
|
|
class ResolverResolver extends AbstractResolver |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param $input |
21
|
|
|
* |
22
|
|
|
* @return mixed |
23
|
|
|
*/ |
24
|
4 |
|
public function resolve($input) |
25
|
|
|
{ |
26
|
4 |
|
if (!is_array($input)) { |
27
|
2 |
|
$input = [$input]; |
28
|
2 |
|
} |
29
|
|
|
|
30
|
4 |
|
if (!isset($input[0]) || !isset($input[1])) { |
31
|
2 |
|
$optionResolver = new OptionsResolver(); |
32
|
2 |
|
$optionResolver->setDefaults([null, []]); |
33
|
2 |
|
$input = $optionResolver->resolve($input); |
34
|
2 |
|
} |
35
|
|
|
|
36
|
4 |
|
$alias = $input[0]; |
37
|
4 |
|
$funcArgs = $input[1]; |
38
|
|
|
|
39
|
4 |
|
if (null === $func = $this->cache->fetch($alias)) { |
40
|
4 |
|
$options = $this->getResolverServiceOptionsFromAlias($alias); |
41
|
|
|
|
42
|
2 |
|
$resolver = $this->container->get($options['id']); |
43
|
2 |
|
if ($resolver instanceof ContainerAwareInterface) { |
44
|
|
|
$resolver->setContainer($this->container); |
45
|
|
|
} |
46
|
2 |
|
$func = [$resolver, $options['method']]; |
47
|
|
|
|
48
|
2 |
|
$this->cache->save($alias, $func); |
49
|
2 |
|
} |
50
|
|
|
|
51
|
2 |
|
return call_user_func_array($func, $funcArgs); |
52
|
|
|
} |
53
|
|
|
|
54
|
4 |
|
private function getResolverServiceOptionsFromAlias($alias) |
55
|
|
|
{ |
56
|
4 |
|
$resolversMapping = $this->getMapping(); |
57
|
|
|
|
58
|
4 |
|
if (!isset($resolversMapping[$alias])) { |
59
|
2 |
|
throw new UnresolvableException( |
60
|
2 |
|
$this->unresolvableMessage($alias) |
61
|
2 |
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
return $resolversMapping[$alias]; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
protected function getMapping() |
68
|
|
|
{ |
69
|
2 |
|
return $this->container->getParameter('overblog_graphql.resolvers_mapping'); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
protected function unresolvableMessage($alias) |
73
|
|
|
{ |
74
|
1 |
|
return sprintf('Unknown resolver with alias "%s" (verified service tag)', $alias); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|