Completed
Push — master ( 1826e3...724c6f )
by Rafael
07:29
created

SchemaExportCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 2
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
     * GraphQLSchemaExportCommand constructor.
29
     *
30
     * @param SchemaExporter $exporter
31
     */
32
    public function __construct(SchemaExporter $exporter)
33
    {
34
        $this->exporter = $exporter;
35
36
        parent::__construct();
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    protected function configure()
43
    {
44
        $this->setName('graphql:schema:export')
45
             ->setDescription('Export your schema.')
46
             ->addOption('endpoint', null, InputOption::VALUE_REQUIRED, 'Name of the endpoint to export', DefinitionRegistry::DEFAULT_ENDPOINT)
47
             ->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Name of the file to save the schema, e.i. schema.graphql or schema.json')
48
             ->addOption('json', null, InputOption::VALUE_NONE, 'Create json output, automatically used if the output contains json extension');
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    protected function execute(InputInterface $input, OutputInterface $output)
55
    {
56
        $endpoint = $input->getOption('endpoint');
57
        $asJson = $input->getOption('json');
58
        $outputName = $input->getOption('output');
59
        if (preg_match('/\.json$/', $outputName)) {
60
            $asJson = true;
61
        }
62
63
        $schema = $this->exporter->export($endpoint, $asJson);
64
        if ($outputName) {
65
            file_put_contents($outputName, $schema);
66
        } else {
67
            $output->write($schema);
68
        }
69
    }
70
}
71