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 Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry; |
18
|
|
|
use Ynlo\GraphQLBundle\Schema\SchemaExporter; |
19
|
|
|
|
20
|
|
|
class SchemaExportCommand extends Command |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var SchemaExporter |
24
|
|
|
*/ |
25
|
|
|
protected $exporter; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var DefinitionRegistry |
29
|
|
|
*/ |
30
|
|
|
protected $definitionRegistry; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* GraphQLSchemaExportCommand constructor. |
34
|
|
|
* |
35
|
|
|
* @param SchemaExporter $exporter |
36
|
|
|
* @param DefinitionRegistry $definitionRegistry |
37
|
|
|
*/ |
38
|
|
|
public function __construct(SchemaExporter $exporter, DefinitionRegistry $definitionRegistry) |
39
|
|
|
{ |
40
|
|
|
$this->exporter = $exporter; |
41
|
|
|
$this->definitionRegistry = $definitionRegistry; |
42
|
|
|
|
43
|
|
|
parent::__construct(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
protected function configure() |
50
|
|
|
{ |
51
|
|
|
$this->setName('graphql:schema:export') |
52
|
|
|
->setDescription('Export your schema.') |
53
|
|
|
->addOption('endpoint', null, InputOption::VALUE_REQUIRED, 'Name of the endpoint to export', DefinitionRegistry::DEFAULT_ENDPOINT) |
54
|
|
|
->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Name of the file to save the schema, e.i. schema.graphql or schema.json') |
55
|
|
|
->addOption('json', null, InputOption::VALUE_NONE, 'Create json output, automatically used if the output contains json extension'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritDoc} |
60
|
|
|
*/ |
61
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
62
|
|
|
{ |
63
|
|
|
$endpoint = $input->getOption('endpoint'); |
64
|
|
|
$asJson = $input->getOption('json'); |
65
|
|
|
$outputName = $input->getOption('output'); |
66
|
|
|
if (preg_match('/\.json$/', $outputName)) { |
|
|
|
|
67
|
|
|
$asJson = true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$schema = $this->exporter->export($this->definitionRegistry->getEndpoint($endpoint), $asJson); |
|
|
|
|
71
|
|
|
if ($outputName) { |
72
|
|
|
file_put_contents($outputName, $schema); |
73
|
|
|
} else { |
74
|
|
|
$output->write($schema); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|