Completed
Push — master ( 85c5ae...c0f1d0 )
by Andreas
21s queued 11s
created

Command/GeneratePersistentCollectionsCommand.php (1 issue)

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