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
|
|
|
use Doctrine\ODM\MongoDB\Tools\DocumentGenerator; |
27
|
|
|
use Doctrine\ODM\MongoDB\Tools\DisconnectedClassMetadataFactory; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Command to generate document classes and method stubs from your mapping information. |
31
|
|
|
* |
32
|
|
|
* @since 1.0 |
33
|
|
|
* @author Benjamin Eberlei <[email protected]> |
34
|
|
|
* @author Guilherme Blanco <[email protected]> |
35
|
|
|
* @author Jonathan Wage <[email protected]> |
36
|
|
|
* @author Roman Borschel <[email protected]> |
37
|
|
|
*/ |
38
|
|
|
class GenerateDocumentsCommand extends Console\Command\Command |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @see Console\Command\Command |
42
|
|
|
*/ |
43
|
|
|
protected function configure() |
44
|
|
|
{ |
45
|
|
|
$this |
46
|
|
|
->setName('odm:generate:documents') |
47
|
|
|
->setDescription('Generate document classes and method stubs from your mapping information.') |
48
|
|
|
->setDefinition(array( |
49
|
|
|
new InputOption( |
50
|
|
|
'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
51
|
|
|
'A string pattern used to match documents that should be processed.' |
52
|
|
|
), |
53
|
|
|
new InputArgument( |
54
|
|
|
'dest-path', InputArgument::REQUIRED, 'The path to generate your document classes.' |
55
|
|
|
), |
56
|
|
|
new InputOption( |
57
|
|
|
'generate-annotations', null, InputOption::VALUE_OPTIONAL, |
58
|
|
|
'Flag to define if the generator should generate annotation metadata on documents.', false |
59
|
|
|
), |
60
|
|
|
new InputOption( |
61
|
|
|
'generate-methods', null, InputOption::VALUE_OPTIONAL, |
62
|
|
|
'Flag to define if the generator should generate stub methods on documents.', true |
63
|
|
|
), |
64
|
|
|
new InputOption( |
65
|
|
|
'regenerate-documents', null, InputOption::VALUE_OPTIONAL, |
66
|
|
|
'Flag to define if the generator should regenerate a document if it exists.', false |
67
|
|
|
), |
68
|
|
|
new InputOption( |
69
|
|
|
'update-documents', null, InputOption::VALUE_OPTIONAL, |
70
|
|
|
'Flag to define if the generator should only update a document if it exists.', true |
71
|
|
|
), |
72
|
|
|
new InputOption( |
73
|
|
|
'extend', null, InputOption::VALUE_OPTIONAL, |
74
|
|
|
'Defines a base class to be extended by generated document classes.' |
75
|
|
|
), |
76
|
|
|
new InputOption( |
77
|
|
|
'num-spaces', null, InputOption::VALUE_OPTIONAL, |
78
|
|
|
'Defines the number of indentation spaces.', 4 |
79
|
|
|
), |
80
|
|
|
new InputOption( |
81
|
|
|
'no-backup', null, InputOption::VALUE_NONE, |
82
|
|
|
'Flag to define if the generator should provide a backup file of exisiting code.' |
83
|
|
|
) |
84
|
|
|
)) |
85
|
|
|
->setHelp(<<<EOT |
86
|
|
|
Generate document classes and method stubs from your mapping information. |
87
|
|
|
|
88
|
|
|
If you use the <comment>--update-documents</comment> or <comment>--regenerate-documents</comment> flags your exisiting |
89
|
|
|
code gets overwritten. The DocumentGenerator will only append new code to your |
90
|
|
|
file and will not delete the old code. However this approach may still be prone |
91
|
|
|
to error and we suggest you use code repositories such as GIT or SVN to make |
92
|
|
|
backups of your code. |
93
|
|
|
|
94
|
|
|
It makes sense to generate the document code if you are using documents as Data |
95
|
|
|
Access Objects only and dont put much additional logic on them. If you are |
96
|
|
|
however putting much more logic on the documents you should refrain from using |
97
|
|
|
the document-generator and code your documents manually. |
98
|
|
|
|
99
|
|
|
<error>Important:</error> Even if you specified Inheritance options in your |
100
|
|
|
XML or YAML Mapping files the generator cannot generate the base and |
101
|
|
|
child classes for you correctly, because it doesn't know which |
102
|
|
|
class is supposed to extend which. You have to adjust the document |
103
|
|
|
code manually for inheritance to work! |
104
|
|
|
EOT |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @see Console\Command\Command |
110
|
|
|
*/ |
111
|
|
|
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) |
112
|
|
|
{ |
113
|
|
|
$dm = $this->getHelper('documentManager')->getDocumentManager(); |
114
|
|
|
|
115
|
|
|
$cmf = new DisconnectedClassMetadataFactory(); |
116
|
|
|
$cmf->setDocumentManager($dm); |
117
|
|
|
$cmf->setConfiguration($dm->getConfiguration()); |
118
|
|
|
$metadatas = $cmf->getAllMetadata(); |
119
|
|
|
$metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); |
120
|
|
|
|
121
|
|
|
// Process destination directory |
122
|
|
|
$destPath = realpath($input->getArgument('dest-path')); |
123
|
|
|
|
124
|
|
|
if ( ! file_exists($destPath)) { |
125
|
|
|
throw new \InvalidArgumentException( |
126
|
|
|
sprintf("Documents destination directory '<info>%s</info>' does not exist.", $destPath) |
127
|
|
|
); |
128
|
|
|
} elseif ( ! is_writable($destPath)) { |
129
|
|
|
throw new \InvalidArgumentException( |
130
|
|
|
sprintf("Documents destination directory '<info>%s</info>' does not have write permissions.", $destPath) |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (count($metadatas)) { |
135
|
|
|
// Create DocumentGenerator |
136
|
|
|
$documentGenerator = new DocumentGenerator(); |
137
|
|
|
|
138
|
|
|
$documentGenerator->setGenerateAnnotations($input->getOption('generate-annotations')); |
139
|
|
|
$documentGenerator->setGenerateStubMethods($input->getOption('generate-methods')); |
140
|
|
|
$documentGenerator->setRegenerateDocumentIfExists($input->getOption('regenerate-documents')); |
141
|
|
|
$documentGenerator->setUpdateDocumentIfExists($input->getOption('update-documents')); |
142
|
|
|
$documentGenerator->setBackupExisting(!$input->getOption('no-backup')); |
143
|
|
|
$documentGenerator->setNumSpaces($input->getOption('num-spaces')); |
144
|
|
|
|
145
|
|
|
if (($extend = $input->getOption('extend')) !== null) { |
146
|
|
|
$documentGenerator->setClassToExtend($extend); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
foreach ($metadatas as $metadata) { |
150
|
|
|
$output->writeln( |
151
|
|
|
sprintf('Processing document "<info>%s</info>".', $metadata->name) |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// Generating Documents |
156
|
|
|
$documentGenerator->generate($metadatas, $destPath); |
157
|
|
|
|
158
|
|
|
// Outputting information message |
159
|
|
|
$output->writeln(array( |
160
|
|
|
'', |
161
|
|
|
sprintf('Document classes have been generated to "<info>%s</info>".', $destPath) |
162
|
|
|
)); |
163
|
|
|
} else { |
164
|
|
|
$output->writeln('No Metadata Classes to process.'); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|