Completed
Push — master ( ba3223...b47a39 )
by Luís
17s
created

GenerateEntitiesCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 32
nc 1
nop 0
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 14
cp 0
crap 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Tools\Console\Command;
21
22
use Doctrine\ORM\Tools\Console\MetadataFilter;
23
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
24
use Doctrine\ORM\Tools\EntityGenerator;
25
use Symfony\Component\Console\Command\Command;
26
use Symfony\Component\Console\Input\InputArgument;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
use Symfony\Component\Console\Style\SymfonyStyle;
31
32
/**
33
 * Command to generate entity classes and method stubs from your mapping information.
34
 *
35
 * @link    www.doctrine-project.org
36
 * @since   2.0
37
 * @author  Benjamin Eberlei <[email protected]>
38
 * @author  Guilherme Blanco <[email protected]>
39
 * @author  Jonathan Wage <[email protected]>
40
 * @author  Roman Borschel <[email protected]>
41
 */
42
class GenerateEntitiesCommand extends Command
43
{
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function configure()
48
    {
49
        $this->setName('orm:generate-entities')
50
             ->setAliases(['orm:generate:entities'])
51
             ->setDescription('Generate entity classes and method stubs from your mapping information')
52
             ->addArgument('dest-path', InputArgument::REQUIRED, 'The path to generate your entity classes.')
53
             ->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be processed.')
54
             ->addOption('generate-annotations', null, InputOption::VALUE_OPTIONAL, 'Flag to define if generator should generate annotation metadata on entities.', false)
55
             ->addOption('generate-methods', null, InputOption::VALUE_OPTIONAL, 'Flag to define if generator should generate stub methods on entities.', true)
56
             ->addOption('regenerate-entities', null, InputOption::VALUE_OPTIONAL, 'Flag to define if generator should regenerate entity if it exists.', false)
57
             ->addOption('update-entities', null, InputOption::VALUE_OPTIONAL, 'Flag to define if generator should only update entity if it exists.', true)
58
             ->addOption('extend', null, InputOption::VALUE_REQUIRED, 'Defines a base class to be extended by generated entity classes.')
59
             ->addOption('num-spaces', null, InputOption::VALUE_REQUIRED, 'Defines the number of indentation spaces', 4)
60
             ->addOption('no-backup', null, InputOption::VALUE_NONE, 'Flag to define if generator should avoid backuping existing entity file if it exists.')
61
             ->setHelp(<<<EOT
62
Generate entity classes and method stubs from your mapping information.
63
64
If you use the <comment>--update-entities</comment> or <comment>--regenerate-entities</comment> flags your existing
65
code gets overwritten. The EntityGenerator will only append new code to your
66
file and will not delete the old code. However this approach may still be prone
67
to error and we suggest you use code repositories such as GIT or SVN to make
68
backups of your code.
69
70
It makes sense to generate the entity code if you are using entities as Data
71
Access Objects only and don't put much additional logic on them. If you are
72
however putting much more logic on the entities you should refrain from using
73
the entity-generator and code your entities manually.
74
75
<error>Important:</error> Even if you specified Inheritance options in your
76
XML or YAML Mapping files the generator cannot generate the base and
77
child classes for you correctly, because it doesn't know which
78
class is supposed to extend which. You have to adjust the entity
79
code manually for inheritance to work!
80
EOT
81
             );
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    protected function execute(InputInterface $input, OutputInterface $output)
88
    {
89
        $ui = new SymfonyStyle($input, $output);
90
91
        $em = $this->getHelper('em')->getEntityManager();
92
93
        $cmf = new DisconnectedClassMetadataFactory();
94
        $cmf->setEntityManager($em);
95
        $metadatas = $cmf->getAllMetadata();
96
        $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
97
98
        // Process destination directory
99
        $destPath = realpath($input->getArgument('dest-path'));
100
101
        if ( ! file_exists($destPath)) {
102
            throw new \InvalidArgumentException(
103
                sprintf("Entities destination directory '<info>%s</info>' does not exist.", $input->getArgument('dest-path'))
104
            );
105
        }
106
107
        if ( ! is_writable($destPath)) {
108
            throw new \InvalidArgumentException(
109
                sprintf("Entities destination directory '<info>%s</info>' does not have write permissions.", $destPath)
110
            );
111
        }
112
113
        if (empty($metadatas)) {
114
            $ui->success('No Metadata Classes to process.');
115
            return;
116
        }
117
118
        $entityGenerator = new EntityGenerator();
119
120
        $entityGenerator->setGenerateAnnotations($input->getOption('generate-annotations'));
121
        $entityGenerator->setGenerateStubMethods($input->getOption('generate-methods'));
122
        $entityGenerator->setRegenerateEntityIfExists($input->getOption('regenerate-entities'));
123
        $entityGenerator->setUpdateEntityIfExists($input->getOption('update-entities'));
124
        $entityGenerator->setNumSpaces($input->getOption('num-spaces'));
125
        $entityGenerator->setBackupExisting(!$input->getOption('no-backup'));
126
127
        if (($extend = $input->getOption('extend')) !== null) {
128
            $entityGenerator->setClassToExtend($extend);
129
        }
130
131
        foreach ($metadatas as $metadata) {
132
            $ui->text(sprintf('Processing entity "<info>%s</info>"', $metadata->name));
133
        }
134
135
        // Generating Entities
136
        $entityGenerator->generate($metadatas, $destPath);
137
138
        // Outputting information message
139
        $ui->newLine();
140
        $ui->success(sprintf('Entity classes generated to "%s"', $destPath));
141
    }
142
}
143