Passed
Push — master ( aeecbb...ebc98d )
by Alan
03:59
created

GraphQlExportCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Command;
15
16
use ApiPlatform\Core\GraphQl\Type\SchemaBuilder;
17
use GraphQL\Utils\SchemaPrinter;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Console\Style\SymfonyStyle;
23
24
/**
25
 * Export the GraphQL schema in Schema Definition Language (SDL).
26
 *
27
 * @experimental
28
 *
29
 * @author Alan Poulain <[email protected]>
30
 */
31
class GraphQlExportCommand extends Command
32
{
33
    protected static $defaultName = 'api:graphql:export';
34
35
    private $schemaBuilder;
36
37
    public function __construct(SchemaBuilder $schemaBuilder)
38
    {
39
        $this->schemaBuilder = $schemaBuilder;
40
41
        parent::__construct();
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function configure(): void
48
    {
49
        $this
50
            ->setDescription('Export the GraphQL schema in Schema Definition Language (SDL)')
51
            ->addOption('comment-descriptions', null, InputOption::VALUE_NONE, 'Use preceding comments as the description')
52
            ->addOption('output', 'o', InputOption::VALUE_REQUIRED, 'Write output to file')
53
        ;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        $io = new SymfonyStyle($input, $output);
62
63
        $options = [];
64
65
        if ($input->getOption('comment-descriptions')) {
66
            $options['commentDescriptions'] = true;
67
        }
68
69
        $schemaExport = SchemaPrinter::doPrint($this->schemaBuilder->getSchema(), $options);
70
71
        $filename = $input->getOption('output');
72
        if (\is_string($filename)) {
73
            file_put_contents($filename, $schemaExport);
74
            $io->success(sprintf('Data written to %s.', $filename));
75
        } else {
76
            $output->writeln($schemaExport);
77
        }
78
79
        return 0;
80
    }
81
}
82