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 Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo; |
23
|
|
|
use Doctrine\ODM\MongoDB\Tools\Console\MetadataFilter; |
24
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
25
|
|
|
use Symfony\Component\Console\Input\InputOption; |
26
|
|
|
use Symfony\Component\Console; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Command to (re)generate the persistent collection classes used by doctrine. |
30
|
|
|
* |
31
|
|
|
* @since 1.1 |
32
|
|
|
* @author Maciej Malarz <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class GenerateProxiesCommand extends Console\Command\Command |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @see Console\Command\Command |
38
|
|
|
*/ |
39
|
|
|
protected function configure() |
40
|
|
|
{ |
41
|
|
|
$this |
42
|
|
|
->setName('odm:generate:persistent-collections') |
43
|
|
|
->setDescription('Generates persistent collection classes for custom collections.') |
44
|
|
|
->setDefinition(array( |
45
|
|
|
new InputOption( |
46
|
|
|
'filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
47
|
|
|
'A string pattern used to match documents that should be processed.' |
48
|
|
|
), |
49
|
|
|
new InputArgument( |
50
|
|
|
'dest-path', InputArgument::OPTIONAL, |
51
|
|
|
'The path to generate your proxy classes. If none is provided, it will attempt to grab from configuration.' |
52
|
|
|
), |
53
|
|
|
)) |
54
|
|
|
->setHelp(<<<EOT |
55
|
|
|
Generates persistent collection classes for custom collections. |
56
|
|
|
EOT |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @see Console\Command\Command |
62
|
|
|
*/ |
63
|
|
|
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) |
64
|
|
|
{ |
65
|
|
|
$dm = $this->getHelper('documentManager')->getDocumentManager(); |
66
|
|
|
|
67
|
|
|
$metadatas = $dm->getMetadataFactory()->getAllMetadata(); |
68
|
|
|
$metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); |
69
|
|
|
|
70
|
|
|
// Process destination directory |
71
|
|
|
if (($destPath = $input->getArgument('dest-path')) === null) { |
72
|
|
|
$destPath = $dm->getConfiguration()->getPersistentCollectionDir(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ( ! is_dir($destPath)) { |
76
|
|
|
mkdir($destPath, 0775, true); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$destPath = realpath($destPath); |
80
|
|
|
|
81
|
|
|
if ( ! file_exists($destPath)) { |
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
|
|
|
$collFactory = $dm->getUnitOfWork()->getPersistentCollectionFactory(); |
93
|
|
|
foreach ($metadatas as $metadata) { |
94
|
|
|
$output->write( |
95
|
|
|
sprintf('Processing document "<info>%s</info>"', $metadata->name) . PHP_EOL |
96
|
|
|
); |
97
|
|
|
foreach ($metadata->getAssociationNames() as $fieldName) { |
98
|
|
|
$mapping = $metadata->getFieldMapping($fieldName); |
99
|
|
|
if (empty($mapping['collectionClass'])) { |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
$output->write( |
103
|
|
|
sprintf('Generating class for "<info>%s</info>"', $mapping['collectionClass']) . PHP_EOL |
104
|
|
|
); |
105
|
|
|
$collFactory->generateClass($mapping['collectionClass'], $destPath); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Outputting information message |
110
|
|
|
$output->write(PHP_EOL . sprintf('Persistent collections classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL); |
111
|
|
|
} else { |
112
|
|
|
$output->write('No Metadata Classes to process.' . PHP_EOL); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|