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

GenerateRepositoriesCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 1
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\EntityRepositoryGenerator;
24
use Symfony\Component\Console\Command\Command;
25
use Symfony\Component\Console\Input\InputArgument;
26
use Symfony\Component\Console\Input\InputInterface;
27
use Symfony\Component\Console\Input\InputOption;
28
use Symfony\Component\Console\Output\OutputInterface;
29
use Symfony\Component\Console\Style\SymfonyStyle;
30
31
/**
32
 * Command to generate repository classes for mapping information.
33
 *
34
 * @link    www.doctrine-project.org
35
 * @since   2.0
36
 * @author  Benjamin Eberlei <[email protected]>
37
 * @author  Guilherme Blanco <[email protected]>
38
 * @author  Jonathan Wage <[email protected]>
39
 * @author  Roman Borschel <[email protected]>
40
 */
41
class GenerateRepositoriesCommand extends Command
42
{
43
    /**
44
     * {@inheritdoc}
45
     */
46 3
    protected function configure()
47
    {
48 3
        $this->setName('orm:generate-repositories')
49 3
             ->setAliases(['orm:generate:repositories'])
50 3
             ->setDescription('Generate repository classes from your mapping information')
51 3
             ->addArgument('dest-path', InputArgument::REQUIRED, 'The path to generate your repository classes.')
52 3
             ->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be processed.')
53 3
             ->setHelp('Generate repository classes from your mapping information.');
54 3
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 3
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61 3
        $ui = new SymfonyStyle($input, $output);
62
63 3
        $em = $this->getHelper('em')->getEntityManager();
64
65 3
        $metadatas = $em->getMetadataFactory()->getAllMetadata();
66 3
        $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
67
68 3
        $repositoryName = $em->getConfiguration()->getDefaultRepositoryClassName();
69
70
        // Process destination directory
71 3
        $destPath = realpath($input->getArgument('dest-path'));
72
73 3
        if ( ! file_exists($destPath)) {
74
            throw new \InvalidArgumentException(
75
                sprintf("Entities destination directory '<info>%s</info>' does not exist.", $input->getArgument('dest-path'))
76
            );
77
        }
78
79 3
        if ( ! is_writable($destPath)) {
80
            throw new \InvalidArgumentException(
81
                sprintf("Entities destination directory '<info>%s</info>' does not have write permissions.", $destPath)
82
            );
83
        }
84
85 3
        if (empty($metadatas)) {
86 1
            $ui->success('No Metadata Classes to process.');
87 1
            return;
88
        }
89
90 2
        $numRepositories = 0;
91 2
        $generator       = new EntityRepositoryGenerator();
92
93 2
        $generator->setDefaultRepositoryName($repositoryName);
94
95 2
        foreach ($metadatas as $metadata) {
96 2
            if ($metadata->customRepositoryClassName) {
97 2
                $ui->text(sprintf('Processing repository "<info>%s</info>"', $metadata->customRepositoryClassName));
98
99 2
                $generator->writeEntityRepositoryClass($metadata->customRepositoryClassName, $destPath);
100
101 2
                ++$numRepositories;
102
            }
103
        }
104
105 2
        if ($numRepositories === 0) {
106
            $ui->text('No Repository classes were found to be processed.');
107
            return;
108
        }
109
110
        // Outputting information message
111 2
        $ui->newLine();
112 2
        $ui->text(sprintf('Repository classes generated to "<info>%s</INFO>"', $destPath));
113 2
    }
114
}
115