1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Command; |
4
|
|
|
|
5
|
|
|
use Overblog\GraphQLBundle\Resolver\FluentResolverInterface; |
6
|
|
|
use Overblog\GraphQLBundle\Resolver\MutationResolver; |
7
|
|
|
use Overblog\GraphQLBundle\Resolver\ResolverResolver; |
8
|
|
|
use Overblog\GraphQLBundle\Resolver\TypeResolver; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
14
|
|
|
|
15
|
|
|
class DebugCommand extends Command |
16
|
|
|
{ |
17
|
|
|
private static $categories = ['type', 'mutation', 'resolver']; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var TypeResolver |
21
|
|
|
*/ |
22
|
|
|
private $typeResolver; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var MutationResolver |
26
|
|
|
*/ |
27
|
|
|
private $mutationResolver; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ResolverResolver |
31
|
|
|
*/ |
32
|
|
|
private $resolverResolver; |
33
|
|
|
|
34
|
6 |
|
public function __construct( |
35
|
|
|
TypeResolver $typeResolver, |
36
|
|
|
MutationResolver $mutationResolver, |
37
|
|
|
ResolverResolver $resolverResolver |
38
|
|
|
) { |
39
|
6 |
|
parent::__construct(); |
40
|
6 |
|
$this->typeResolver = $typeResolver; |
41
|
6 |
|
$this->mutationResolver = $mutationResolver; |
42
|
6 |
|
$this->resolverResolver = $resolverResolver; |
43
|
6 |
|
} |
44
|
|
|
|
45
|
6 |
|
protected function configure() |
46
|
|
|
{ |
47
|
|
|
$this |
48
|
6 |
|
->setName('graphql:debug') |
49
|
6 |
|
->setAliases(['debug:graphql']) |
50
|
6 |
|
->addOption( |
51
|
6 |
|
'category', |
52
|
6 |
|
null, |
53
|
6 |
|
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, |
54
|
6 |
|
sprintf('filter by a category (%s).', implode(', ', self::$categories)) |
55
|
|
|
) |
56
|
6 |
|
->setDescription('Display current GraphQL services (types, resolvers and mutations)'); |
57
|
6 |
|
} |
58
|
|
|
|
59
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
60
|
|
|
{ |
61
|
6 |
|
$categoriesOption = $input->getOption('category'); |
62
|
6 |
|
$categoriesOption = is_array($categoriesOption) ? $categoriesOption : [$categoriesOption]; |
63
|
6 |
|
$notAllowed = array_diff($categoriesOption, self::$categories); |
64
|
6 |
|
if (!empty($notAllowed)) { |
65
|
1 |
|
throw new \InvalidArgumentException(sprintf('Invalid category (%s)', implode(',', $notAllowed))); |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
$categories = empty($categoriesOption) ? self::$categories : $categoriesOption; |
69
|
|
|
|
70
|
5 |
|
$io = new SymfonyStyle($input, $output); |
71
|
5 |
|
$tableHeaders = ['id', 'aliases']; |
72
|
5 |
|
foreach ($categories as $category) { |
73
|
5 |
|
$io->title(sprintf('GraphQL %ss Services', ucfirst($category))); |
74
|
|
|
|
75
|
|
|
/** @var FluentResolverInterface $resolver */ |
76
|
5 |
|
$resolver = $this->{$category.'Resolver'}; |
77
|
5 |
|
$this->renderTable($resolver, $tableHeaders, $io); |
78
|
|
|
} |
79
|
5 |
|
} |
80
|
|
|
|
81
|
5 |
|
private function renderTable(FluentResolverInterface $resolver, array $tableHeaders, SymfonyStyle $io) |
82
|
|
|
{ |
83
|
5 |
|
$tableRows = []; |
84
|
5 |
|
$solutions = $resolver->getSolutions(); |
85
|
5 |
|
ksort($solutions); |
86
|
5 |
|
foreach ($solutions as $solutionID => $solution) { |
87
|
5 |
|
$aliases = $resolver->getSolutionAliases($solutionID); |
88
|
5 |
|
$aliases[] = $solutionID; |
89
|
5 |
|
ksort($aliases); |
90
|
|
|
|
91
|
5 |
|
$options = $resolver->getSolutionOptions($solutionID); |
92
|
5 |
|
$id = $options['id']; |
93
|
5 |
|
$aliases = array_map(function ($alias) use ($options) { |
94
|
5 |
|
return $alias.(isset($options['method']) ? ' (method: '.$options['method'].')' : ''); |
95
|
5 |
|
}, $aliases); |
96
|
|
|
|
97
|
5 |
|
$tableRows[$id] = [ |
98
|
5 |
|
$id, |
99
|
5 |
|
implode("\n", $aliases), |
100
|
|
|
]; |
101
|
|
|
} |
102
|
5 |
|
ksort($tableRows); |
103
|
|
|
|
104
|
5 |
|
$io->table($tableHeaders, $tableRows); |
105
|
5 |
|
$io->write("\n\n"); |
106
|
5 |
|
} |
107
|
|
|
|
108
|
6 |
|
public static function getCategories() |
109
|
|
|
{ |
110
|
6 |
|
return self::$categories; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|