Completed
Push — master ( 4009e3...35344a )
by Andreas
10s
created

Command/GeneratePersistentCollectionsCommand.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Doctrine\ODM\MongoDB\Tools\Console\Command;
4
5
use Doctrine\ODM\MongoDB\Tools\Console\MetadataFilter;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console;
9
10
/**
11
 * Command to (re)generate the persistent collection classes used by doctrine.
12
 *
13
 * @since 1.1
14
 */
15
class GeneratePersistentCollectionsCommand extends Console\Command\Command
16
{
17
    /**
18
     * @see Console\Command\Command
19
     */
20 View Code Duplication
    protected function configure()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
    {
22
        $this
23
            ->setName('odm:generate:persistent-collections')
24
            ->setDescription('Generates persistent collection classes for custom collections.')
25
            ->setDefinition(array(
26
                new InputOption(
27
                    'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
28
                    'A string pattern used to match documents that should be processed.'
29
                ),
30
                new InputArgument(
31
                    'dest-path', InputArgument::OPTIONAL,
32
                    'The path to generate your proxy classes. If none is provided, it will attempt to grab from configuration.'
33
                ),
34
            ))
35
            ->setHelp(<<<EOT
36
Generates persistent collection classes for custom collections.
37
EOT
38
            );
39
    }
40
41
    /**
42
     * @see Console\Command\Command
43
     */
44
    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
45
    {
46
        $dm = $this->getHelper('documentManager')->getDocumentManager();
47
48
        $metadatas = $dm->getMetadataFactory()->getAllMetadata();
49
        $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter'));
50
51
        // Process destination directory
52
        if (($destPath = $input->getArgument('dest-path')) === null) {
53
            $destPath = $dm->getConfiguration()->getPersistentCollectionDir();
54
        }
55
56
        if ( ! is_dir($destPath)) {
57
            mkdir($destPath, 0775, true);
58
        }
59
60
        $destPath = realpath($destPath);
61
62 View Code Duplication
        if ( ! file_exists($destPath)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
            throw new \InvalidArgumentException(
64
                sprintf("Persistent collections destination directory '<info>%s</info>' does not exist.", $destPath)
65
            );
66
        } elseif ( ! is_writable($destPath)) {
67
            throw new \InvalidArgumentException(
68
                sprintf("Persistent collections destination directory '<info>%s</info>' does not have write permissions.", $destPath)
69
            );
70
        }
71
72
        if (count($metadatas)) {
73
            $generated = [];
74
            $collectionGenerator = $dm->getConfiguration()->getPersistentCollectionGenerator();
75
            foreach ($metadatas as $metadata) {
76
                $output->write(
77
                    sprintf('Processing document "<info>%s</info>"', $metadata->name) . PHP_EOL
78
                );
79
                foreach ($metadata->getAssociationNames() as $fieldName) {
80
                    $mapping = $metadata->getFieldMapping($fieldName);
81
                    if (empty($mapping['collectionClass']) || isset($generated[$mapping['collectionClass']])) {
82
                        continue;
83
                    }
84
                    $generated[$mapping['collectionClass']] = true;
85
                    $output->write(
86
                        sprintf('Generating class for "<info>%s</info>"', $mapping['collectionClass']) . PHP_EOL
87
                    );
88
                    $collectionGenerator->generateClass($mapping['collectionClass'], $destPath);
89
                }
90
            }
91
92
            // Outputting information message
93
            $output->write(PHP_EOL . sprintf('Persistent collections classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL);
94
        } else {
95
            $output->write('No Metadata Classes to process.' . PHP_EOL);
96
        }
97
    }
98
}
99