1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Command; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\Console\Command\Command; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
use Symfony\Component\Console\Output\StreamOutput; |
18
|
|
|
use Ynlo\GraphQLBundle\Error\ControlledErrorManager; |
19
|
|
|
use Ynlo\GraphQLBundle\Error\Exporter\ErrorListExporterInterface; |
20
|
|
|
|
21
|
|
|
class ControlledErrorCommand extends Command |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ControlledErrorManager |
25
|
|
|
*/ |
26
|
|
|
protected $errorManager; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array|iterable|ErrorListExporterInterface[] |
30
|
|
|
*/ |
31
|
|
|
protected $exporters = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ControlledErrorCommand constructor. |
35
|
|
|
* |
36
|
|
|
* @param ControlledErrorManager $errorManager |
37
|
|
|
* @param array|iterable|ErrorListExporterInterface[] $exporters |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ControlledErrorManager $errorManager, iterable $exporters) |
40
|
|
|
{ |
41
|
|
|
$this->errorManager = $errorManager; |
42
|
|
|
$this->exporters = $exporters; |
43
|
|
|
parent::__construct(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
protected function configure() |
50
|
|
|
{ |
51
|
|
|
$this->setName('graphql:error:list') |
52
|
|
|
->setDescription('View, export and control your API errors.') |
53
|
|
|
->addOption('exporter', 'x', InputOption::VALUE_REQUIRED, 'Exporter to user', 'console') |
54
|
|
|
->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Name of the file to save the error list, e.i. error_codes.md'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
61
|
|
|
{ |
62
|
|
|
$this->errorManager->clear();//force reload |
63
|
|
|
$errors = $this->errorManager->all(); |
64
|
|
|
|
65
|
|
|
$result = $output; |
66
|
|
|
if ($output = $input->getOption('output')) { |
67
|
|
|
$info = new \SplFileInfo($output); |
68
|
|
|
if (!file_exists($info->getPath())) { |
69
|
|
|
if (!mkdir($info->getPath(), 0777, true) && !is_dir($info->getPath())) { |
70
|
|
|
throw new \RuntimeException(sprintf('Directory "%s" was not created', $info->getPath())); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$result = new StreamOutput(fopen($output, 'w+b', false)); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
foreach ($this->exporters as $exporter) { |
78
|
|
|
if ($exporter->getName() === $input->getOption('exporter')) { |
79
|
|
|
$exporter->export($errors, $result); |
80
|
|
|
|
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
throw new \Exception('There are not any registered exporter to process the list of errors'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|