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\Command; |
13
|
|
|
|
14
|
|
|
use Overblog\GraphQLBundle\Resolver\ResolverInterface; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
20
|
|
|
|
21
|
|
|
class DebugCommand extends ContainerAwareCommand |
22
|
|
|
{ |
23
|
|
|
const CATEGORIES = ['type', 'mutation', 'resolver']; |
24
|
|
|
|
25
|
|
|
protected function configure() |
26
|
|
|
{ |
27
|
|
|
$this |
28
|
|
|
->setName('graphql:debug') |
29
|
|
|
->setAliases(['debug:graphql']) |
30
|
|
|
->addOption( |
31
|
|
|
'category', |
32
|
|
|
null, |
33
|
|
|
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, |
34
|
|
|
sprintf('filter by a category (%s).', implode(', ', self::CATEGORIES)) |
35
|
|
|
) |
36
|
|
|
->setDescription('Display current GraphQL services (types, resolvers and mutations)'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
40
|
|
|
{ |
41
|
|
|
$categoriesOption = $input->getOption('category'); |
42
|
|
|
$categoriesOption = is_array($categoriesOption) ? $categoriesOption : [$categoriesOption]; |
43
|
|
|
$notAllowed = array_diff($categoriesOption, self::CATEGORIES); |
44
|
|
|
if (!empty($notAllowed)) { |
45
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid category (%s)', implode(',', $notAllowed))); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$categories = empty($categoriesOption) ? self::CATEGORIES : $categoriesOption; |
49
|
|
|
|
50
|
|
|
$io = new SymfonyStyle($input, $output); |
51
|
|
|
$tableHeaders = ['id', 'aliases']; |
52
|
|
|
foreach ($categories as $category) { |
53
|
|
|
$io->title(sprintf('GraphQL %ss Services', ucfirst($category))); |
54
|
|
|
/** @var ResolverInterface $resolver */ |
55
|
|
|
$resolver = $this->getContainer()->get(sprintf('overblog_graphql.%s_resolver', $category)); |
56
|
|
|
$solutions = $this->retrieveSolutions($resolver); |
57
|
|
|
$this->renderTable($tableHeaders, $solutions, $io); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function renderTable(array $tableHeaders, array $solutions, SymfonyStyle $io) |
62
|
|
|
{ |
63
|
|
|
$tableRows = []; |
64
|
|
|
foreach ($solutions as $id => &$options) { |
65
|
|
|
$tableRows[] = [$id, implode("\n", $options['aliases'])]; |
66
|
|
|
} |
67
|
|
|
$io->table($tableHeaders, $tableRows); |
68
|
|
|
$io->write("\n\n"); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function retrieveSolutions(ResolverInterface $resolver) |
72
|
|
|
{ |
73
|
|
|
$data = []; |
74
|
|
|
foreach ($resolver->getSolutions() as $alias => $solution) { |
75
|
|
|
$options = $resolver->getSolutionOptions($alias); |
76
|
|
|
|
77
|
|
|
$id = $options['id']; |
78
|
|
|
if (!isset($data[$id]['aliases'])) { |
79
|
|
|
$data[$id]['aliases'] = []; |
80
|
|
|
} |
81
|
|
|
$data[$id]['aliases'][] = $options['alias'].(isset($options['method']) ? ' (method: '.$options['method'].')' : ''); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $data; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|