1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ODM\MongoDB\Tools\Console\Command; |
6
|
|
|
|
7
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
8
|
|
|
use Doctrine\ODM\MongoDB\Tools\Console\MetadataFilter; |
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 array_filter; |
14
|
|
|
use function count; |
15
|
|
|
use function file_exists; |
16
|
|
|
use function is_dir; |
17
|
|
|
use function is_writable; |
18
|
|
|
use function mkdir; |
19
|
|
|
use function realpath; |
20
|
|
|
use function sprintf; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Command to (re)generate the proxy classes used by doctrine. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
class GenerateProxiesCommand 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:proxies') |
35
|
|
|
->setDescription('Generates proxy classes for document classes.') |
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 proxy classes for document classes. |
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
|
|
|
$dm = $this->getHelper('documentManager')->getDocumentManager(); |
61
|
|
|
|
62
|
|
|
$metadatas = array_filter($dm->getMetadataFactory()->getAllMetadata(), function (ClassMetadata $classMetadata) { |
63
|
|
|
return ! $classMetadata->isEmbeddedDocument && ! $classMetadata->isMappedSuperclass && ! $classMetadata->isQueryResultDocument; |
64
|
|
|
}); |
65
|
|
|
$metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); |
66
|
|
|
$destPath = $input->getArgument('dest-path'); |
67
|
|
|
|
68
|
|
|
// Process destination directory |
69
|
|
|
if ($destPath === null) { |
70
|
|
|
$destPath = $dm->getConfiguration()->getProxyDir(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (! is_dir($destPath)) { |
74
|
|
|
mkdir($destPath, 0775, true); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$destPath = realpath($destPath); |
78
|
|
|
|
79
|
|
View Code Duplication |
if (! file_exists($destPath)) { |
|
|
|
|
80
|
|
|
throw new \InvalidArgumentException( |
81
|
|
|
sprintf("Proxies destination directory '<info>%s</info>' does not exist.", $destPath) |
82
|
|
|
); |
83
|
|
|
} elseif (! is_writable($destPath)) { |
84
|
|
|
throw new \InvalidArgumentException( |
85
|
|
|
sprintf("Proxies destination directory '<info>%s</info>' does not have write permissions.", $destPath) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
if (count($metadatas)) { |
|
|
|
|
90
|
|
|
foreach ($metadatas as $metadata) { |
91
|
|
|
$output->write( |
92
|
|
|
sprintf('Processing document "<info>%s</info>"', $metadata->name) . PHP_EOL |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Generating Proxies |
97
|
|
|
$dm->getProxyFactory()->generateProxyClasses($metadatas, $destPath); |
98
|
|
|
|
99
|
|
|
// Outputting information message |
100
|
|
|
$output->write(PHP_EOL . sprintf('Proxy classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL); |
101
|
|
|
} else { |
102
|
|
|
$output->write('No Metadata Classes to process.' . PHP_EOL); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.