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 Symfony\Component\Console\Input\InputArgument; |
23
|
|
|
use Symfony\Component\Console\Input\InputOption; |
24
|
|
|
use Symfony\Component\Console; |
25
|
|
|
use Doctrine\ODM\MongoDB\Tools\Console\MetadataFilter; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Command to (re)generate the proxy classes used by doctrine. |
29
|
|
|
* |
30
|
|
|
* @since 1.0 |
31
|
|
|
* @author Benjamin Eberlei <[email protected]> |
32
|
|
|
* @author Guilherme Blanco <[email protected]> |
33
|
|
|
* @author Jonathan Wage <[email protected]> |
34
|
|
|
* @author Roman Borschel <[email protected]> |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
class GenerateProxiesCommand extends Console\Command\Command |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @see Console\Command\Command |
40
|
|
|
*/ |
41
|
|
|
protected function configure() |
42
|
|
|
{ |
43
|
|
|
$this |
44
|
|
|
->setName('odm:generate:proxies') |
45
|
|
|
->setDescription('Generates proxy classes for document classes.') |
46
|
|
|
->setDefinition(array( |
47
|
|
|
new InputOption( |
48
|
|
|
'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
49
|
|
|
'A string pattern used to match documents that should be processed.' |
50
|
|
|
), |
51
|
|
|
new InputArgument( |
52
|
|
|
'dest-path', InputArgument::OPTIONAL, |
53
|
|
|
'The path to generate your proxy classes. If none is provided, it will attempt to grab from configuration.' |
54
|
|
|
), |
55
|
|
|
)) |
56
|
|
|
->setHelp(<<<EOT |
57
|
|
|
Generates proxy classes for document classes. |
58
|
|
|
EOT |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @see Console\Command\Command |
64
|
|
|
*/ |
65
|
|
|
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) |
66
|
|
|
{ |
67
|
|
|
$dm = $this->getHelper('documentManager')->getDocumentManager(); |
68
|
|
|
|
69
|
|
|
$metadatas = $dm->getMetadataFactory()->getAllMetadata(); |
70
|
|
|
$metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); |
71
|
|
|
|
72
|
|
|
// Process destination directory |
73
|
|
|
if (($destPath = $input->getArgument('dest-path')) === null) { |
74
|
|
|
$destPath = $dm->getConfiguration()->getProxyDir(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ( ! is_dir($destPath)) { |
78
|
|
|
mkdir($destPath, 0775, true); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$destPath = realpath($destPath); |
82
|
|
|
|
83
|
|
|
if ( ! file_exists($destPath)) { |
84
|
|
|
throw new \InvalidArgumentException( |
85
|
|
|
sprintf("Proxies destination directory '<info>%s</info>' does not exist.", $destPath) |
86
|
|
|
); |
87
|
|
|
} elseif ( ! is_writable($destPath)) { |
88
|
|
|
throw new \InvalidArgumentException( |
89
|
|
|
sprintf("Proxies destination directory '<info>%s</info>' does not have write permissions.", $destPath) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (count($metadatas)) { |
94
|
|
|
foreach ($metadatas as $metadata) { |
95
|
|
|
$output->write( |
96
|
|
|
sprintf('Processing document "<info>%s</info>"', $metadata->name) . PHP_EOL |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Generating Proxies |
101
|
|
|
$dm->getProxyFactory()->generateProxyClasses($metadatas, $destPath); |
102
|
|
|
|
103
|
|
|
// Outputting information message |
104
|
|
|
$output->write(PHP_EOL . sprintf('Proxy classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL); |
105
|
|
|
} else { |
106
|
|
|
$output->write('No Metadata Classes to process.' . PHP_EOL); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.