Completed
Pull Request — 2.x (#80)
by Christian
02:11
created

GenerateCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 17
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[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
namespace Sonata\EasyExtendsBundle\Command;
13
14
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata;
15
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
21
22
/**
23
 * Generate Application entities from bundle entities.
24
 *
25
 * @author Thomas Rabaix <[email protected]>
26
 */
27
class GenerateCommand extends ContainerAwareCommand
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('sonata:easy-extends:generate')
36
            ->setHelp(<<<'EOT'
37
The <info>easy-extends:generate:entities</info> command generating a valid bundle structure from a Vendor Bundle.
38
39
  <info>ie: ./app/console sonata:easy-extends:generate SonataUserBundle</info>
40
EOT
41
            );
42
43
        $this->setDescription('Create entities used by Sonata\'s bundles');
44
45
        $this->addArgument('bundle', InputArgument::IS_ARRAY, 'The bundle name to "easy-extends"');
46
        $this->addOption('dest', 'd', InputOption::VALUE_OPTIONAL, 'The base folder where the Application will be created', false);
47
        $this->addOption('namespace', 'n', InputOption::VALUE_OPTIONAL, 'The namespace for the classes', false);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $destOption = $input->getOption('dest');
56
        if ($destOption) {
57
            $dest = realpath($destOption);
58
            if (false === $dest) {
59
                $output->writeln('');
60
                $output->writeln(sprintf('<error>The provided destination folder \'%s\' does not exist!</error>', $destOption));
61
62
                return 0;
63
            }
64
        } else {
65
            $dest = $this->getContainer()->get('kernel')->getRootDir();
66
        }
67
68
        $namespace = $input->getOption('namespace');
69
        if ($namespace) {
70
            if (!preg_match('/^(?:(?:[[:alnum:]]+|:vendor)\\\\?)+$/', $namespace)) {
71
                $output->writeln('');
72
                $output->writeln(sprintf('<error>The provided namespace \'%s\' is not a valid namespace!</error>', $namespace));
73
74
                return 0;
75
            }
76
        } else {
77
            $namespace = 'Application\:vendor';
78
        }
79
80
        $configuration = array(
81
            'application_dir' => sprintf('%s%s%s', $dest, DIRECTORY_SEPARATOR, str_replace('\\', DIRECTORY_SEPARATOR, $namespace)),
82
            'namespace' => $namespace,
83
        );
84
85
        $bundleNames = $input->getArgument('bundle');
86
87
        if (empty($bundleNames)) {
88
            $output->writeln('');
89
            $output->writeln('<error>You must provide a bundle name!</error>');
90
            $output->writeln('');
91
            $output->writeln('  Bundles availables :');
92
            /** @var BundleInterface $bundle */
93
            foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) {
94
                $bundleMetadata = new BundleMetadata($bundle, $configuration);
95
96
                if (!$bundleMetadata->isExtendable()) {
97
                    continue;
98
                }
99
100
                $output->writeln(sprintf('     - %s', $bundle->getName()));
101
            }
102
103
            $output->writeln('');
104
105
            return 0;
106
        }
107
108
        foreach ($bundleNames as $bundleName) {
109
            $processed = $this->generate($bundleName, $configuration, $output);
110
111
            if (!$processed) {
112
                $output->writeln(sprintf('<error>The bundle \'%s\' does not exist or not defined in the kernel file!</error>', $bundleName));
113
114
                return -1;
115
            }
116
        }
117
118
        $output->writeln('done!');
119
120
        return 0;
121
    }
122
123
    /**
124
     * Generates a bundle entities from a bundle name.
125
     *
126
     * @param string          $bundleName
127
     * @param array           $configuration
128
     * @param OutputInterface $output
129
     *
130
     * @return bool
131
     */
132
    protected function generate($bundleName, array $configuration, $output)
133
    {
134
        $processed = false;
135
136
        /** @var BundleInterface $bundle */
137
        foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) {
138
            if ($bundle->getName() != $bundleName) {
139
                continue;
140
            }
141
142
            $processed = true;
143
            $bundleMetadata = new BundleMetadata($bundle, $configuration);
144
145
            // generate the bundle file
146
            if (!$bundleMetadata->isExtendable()) {
147
                $output->writeln(sprintf('Ignoring bundle : "<comment>%s</comment>"', $bundleMetadata->getClass()));
148
                continue;
149
            }
150
151
            // generate the bundle file
152
            if (!$bundleMetadata->isValid()) {
153
                $output->writeln(sprintf('%s : <comment>wrong folder structure</comment>', $bundleMetadata->getClass()));
154
                continue;
155
            }
156
157
            $output->writeln(sprintf('Processing bundle : "<info>%s</info>"', $bundleMetadata->getName()));
158
159
            $this->getContainer()->get('sonata.easy_extends.generator.bundle')
160
                ->generate($output, $bundleMetadata);
161
162
            $output->writeln(sprintf('Processing Doctrine ORM : "<info>%s</info>"', $bundleMetadata->getName()));
163
            $this->getContainer()->get('sonata.easy_extends.generator.orm')
164
                ->generate($output, $bundleMetadata);
165
166
            $output->writeln(sprintf('Processing Doctrine ODM : "<info>%s</info>"', $bundleMetadata->getName()));
167
            $this->getContainer()->get('sonata.easy_extends.generator.odm')
168
                ->generate($output, $bundleMetadata);
169
170
            $output->writeln(sprintf('Processing Doctrine PHPCR : "<info>%s</info>"', $bundleMetadata->getName()));
171
            $this->getContainer()->get('sonata.easy_extends.generator.phpcr')
172
                ->generate($output, $bundleMetadata);
173
174
            $output->writeln(sprintf('Processing Serializer config : "<info>%s</info>"', $bundleMetadata->getName()));
175
            $this->getContainer()->get('sonata.easy_extends.generator.serializer')
176
                ->generate($output, $bundleMetadata);
177
178
            $output->writeln('');
179
        }
180
181
        return $processed;
182
    }
183
}
184