Completed
Pull Request — master (#1219)
by Maciej
13:47
created

GeneratePersistentCollectionsCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
ccs 0
cts 20
cp 0
rs 9.4285
cc 1
eloc 14
nc 1
nop 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\ODM\MongoDB\Tools\Console\Command;
21
22
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
23
use Doctrine\ODM\MongoDB\Tools\Console\MetadataFilter;
24
use Symfony\Component\Console\Input\InputArgument;
25
use Symfony\Component\Console\Input\InputOption;
26
use Symfony\Component\Console;
27
28
/**
29
 * Command to (re)generate the persistent collection classes used by doctrine.
30
 *
31
 * @since 1.1
32
 */
33
class GeneratePersistentCollectionsCommand extends Console\Command\Command
34
{
35
    /**
36
     * @see Console\Command\Command
37
     */
38
    protected function configure()
39
    {
40
        $this
41
            ->setName('odm:generate:persistent-collections')
42
            ->setDescription('Generates persistent collection classes for custom collections.')
43
            ->setDefinition(array(
44
                new InputOption(
45
                    'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
46
                    'A string pattern used to match documents that should be processed.'
47
                ),
48
                new InputArgument(
49
                    'dest-path', InputArgument::OPTIONAL,
50
                    'The path to generate your proxy classes. If none is provided, it will attempt to grab from configuration.'
51
                ),
52
            ))
53
            ->setHelp(<<<EOT
54
Generates persistent collection classes for custom collections.
55
EOT
56
            );
57
    }
58
59
    /**
60
     * @see Console\Command\Command
61
     */
62
    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
63
    {
64
        $dm = $this->getHelper('documentManager')->getDocumentManager();
65
66
        $metadatas = $dm->getMetadataFactory()->getAllMetadata();
67
        $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
68
69
        // Process destination directory
70
        if (($destPath = $input->getArgument('dest-path')) === null) {
71
            $destPath = $dm->getConfiguration()->getPersistentCollectionDir();
72
        }
73
74
        if ( ! is_dir($destPath)) {
75
            mkdir($destPath, 0775, true);
76
        }
77
78
        $destPath = realpath($destPath);
79
80
        if ( ! file_exists($destPath)) {
81
            throw new \InvalidArgumentException(
82
                sprintf("Persistent collections destination directory '<info>%s</info>' does not exist.", $destPath)
83
            );
84
        } elseif ( ! is_writable($destPath)) {
85
            throw new \InvalidArgumentException(
86
                sprintf("Persistent collections destination directory '<info>%s</info>' does not have write permissions.", $destPath)
87
            );
88
        }
89
90
        if (count($metadatas)) {
91
            $collectionGenerator = $dm->getConfiguration()->getPersistentCollectionGenerator();
92
            foreach ($metadatas as $metadata) {
93
                $output->write(
94
                    sprintf('Processing document "<info>%s</info>"', $metadata->name) . PHP_EOL
95
                );
96
                foreach ($metadata->getAssociationNames() as $fieldName) {
97
                    $mapping = $metadata->getFieldMapping($fieldName);
98
                    if (empty($mapping['collectionClass'])) {
99
                        continue;
100
                    }
101
                    $output->write(
102
                        sprintf('Generating class for "<info>%s</info>"', $mapping['collectionClass']) . PHP_EOL
103
                    );
104
                    $collectionGenerator->generateClass($mapping['collectionClass'], $destPath);
105
                }
106
            }
107
108
            // Outputting information message
109
            $output->write(PHP_EOL . sprintf('Persistent collections classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL);
110
        } else {
111
            $output->write('No Metadata Classes to process.' . PHP_EOL);
112
        }
113
    }
114
}
115