NgModelMetadataCommand.php 3.06 KB
Newer Older
1
2
<?php declare(strict_types=1);
/*
Guillaume Perréal's avatar
Guillaume Perréal committed
3
4
5
 * This file is part of "irstea/ng-model-generator-bundle".
 *
 * "irstea/ng-model-generator-bundle" generates Typescript interfaces for Angular using api-platform metadata.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * Copyright (C) 2018 IRSTEA
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License and the GNU
 * Lesser General Public License along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

namespace Irstea\NgModelGeneratorBundle\Command;

use ApiPlatform\Core\Documentation\Documentation;
use Irstea\NgModelGeneratorBundle\Metadata\MetadataFactoryInterface;
use Irstea\NgModelGeneratorBundle\ModelGenerator;
use Irstea\NgModelGeneratorBundle\Models\PHPClass;
use Symfony\Component\Console\Command\Command;
29
use Symfony\Component\Console\Input\InputArgument;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Class NgModelMetadataCommand.
 */
final class NgModelMetadataCommand extends Command
{
    /**
     * @var MetadataFactoryInterface
     */
    private $metadataFactory;

    /**
     * @var Documentation
     */
    private $documentation;

    /**
     * NgModelGenerateCommand constructor.
     *
     * @param ModelGenerator $generator
     * @param Documentation  $documentation
     */
    public function __construct(
        MetadataFactoryInterface $metadataFactory,
        Documentation $documentation
    ) {
        parent::__construct();

        $this->metadataFactory = $metadataFactory;
        $this->documentation = $documentation;
    }

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName('ng-model:metadata')
71
72
            ->setDescription('Dump model metadata')
            ->addArgument('classes', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Restrict dump to the listed classes.');
73
74
75
76
77
78
79
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
80
81
        $classes = $input->getArgument('classes');

82
83
84
        $metadata = [];
        foreach ($this->documentation->getResourceNameCollection() as $className) {
            $class = PHPClass::get($className);
85
86
87
            if ($classes && !\in_array($className, $classes, true) && !\in_array($class->getBaseName(), $classes, true)) {
                continue;
            }
88
89
90
91
92
93
            $metadata[$class->getBaseName()] = $this->metadataFactory->getResourceMetadata($class);
        }

        $output->writeln(\json_encode($metadata, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE));
    }
}