Completed
Pull Request — master (#61)
by
unknown
02:02
created

GenerateCommand   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 19
Bugs 2 Features 6
Metric Value
wmc 16
c 19
b 2
f 6
lcom 1
cbo 6
dl 0
loc 155
rs 10

3 Methods

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