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\DependencyInjection\Configurator; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\Resolver\AbstractResolver; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
17
|
|
|
|
18
|
|
|
class ResolverConfigurator implements ContainerAwareInterface |
19
|
|
|
{ |
20
|
|
|
use ContainerAwareTrait; |
21
|
|
|
|
22
|
|
|
private $mappings; |
23
|
|
|
|
24
|
25 |
|
public function addMapping($name, array $mapping = []) |
25
|
|
|
{ |
26
|
25 |
|
$this->mappings[$name] = $mapping; |
27
|
25 |
|
} |
28
|
|
|
|
29
|
25 |
|
private function configure(AbstractResolver $resolver, array $mapping) |
30
|
|
|
{ |
31
|
25 |
|
foreach ($mapping as $name => $options) { |
32
|
25 |
|
$cleanOptions = $options; |
33
|
25 |
|
unset($cleanOptions['alias'], $cleanOptions['id']); |
34
|
|
|
|
35
|
25 |
|
$solution = $this->container->get($options['id']); |
36
|
|
|
|
37
|
25 |
|
if ($solution instanceof ContainerAwareInterface) { |
38
|
|
|
$solution->setContainer($this->container); |
39
|
|
|
} |
40
|
|
|
|
41
|
25 |
|
$resolver->addSolution($name, $solution, $cleanOptions); |
42
|
25 |
|
} |
43
|
25 |
|
} |
44
|
|
|
|
45
|
25 |
|
public function __call($name, $arguments) |
46
|
|
|
{ |
47
|
25 |
|
if (!preg_match('/^configure(.*)/i', $name, $matches) || !isset($this->mappings[strtolower($matches[1])])) { |
48
|
|
|
throw new \BadMethodCallException(sprintf('Call to unknown method %s', $name)); |
49
|
|
|
} |
50
|
|
|
|
51
|
25 |
|
$mapping = $this->mappings[strtolower($matches[1])]; |
52
|
|
|
|
53
|
25 |
|
if (!isset($arguments[0]) || !$arguments[0] instanceof AbstractResolver) { |
54
|
|
|
throw new \InvalidArgumentException('Resolver must implement "%s"', AbstractResolver::class); |
55
|
|
|
} |
56
|
|
|
|
57
|
25 |
|
$this->configure($arguments[0], $mapping); |
58
|
25 |
|
} |
59
|
|
|
} |
60
|
|
|
|