|
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\Schema; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\ODM\MongoDB\SchemaManager; |
|
23
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
24
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
25
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @author Bulat Shakirzyanov <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class CreateCommand extends AbstractCommand |
|
31
|
|
|
{ |
|
32
|
|
|
private $createOrder = array(self::DB, self::COLLECTION, self::INDEX); |
|
33
|
|
|
|
|
34
|
|
|
private $timeout; |
|
35
|
|
|
|
|
36
|
|
|
protected function configure() |
|
37
|
|
|
{ |
|
38
|
|
|
$this |
|
39
|
|
|
->setName('odm:schema:create') |
|
40
|
|
|
->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)') |
|
41
|
|
|
->addOption('timeout', 't', InputOption::VALUE_OPTIONAL, 'Timeout (ms) for acknowledged index creation') |
|
42
|
|
|
->addOption(self::DB, null, InputOption::VALUE_NONE, 'Create databases') |
|
43
|
|
|
->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Create collections') |
|
44
|
|
|
->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Create indexes') |
|
45
|
|
|
->setDescription('Create databases, collections and indexes for your documents') |
|
46
|
|
|
; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
50
|
|
|
{ |
|
51
|
|
|
foreach ($this->createOrder as $option) { |
|
52
|
|
|
if ($input->getOption($option)) { |
|
53
|
|
|
$create[] = $option; |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// Default to the full creation order if no options were specified |
|
58
|
|
|
$create = empty($create) ? $this->createOrder : $create; |
|
59
|
|
|
|
|
60
|
|
|
$class = $input->getOption('class'); |
|
61
|
|
|
|
|
62
|
|
|
$timeout = $input->getOption('timeout'); |
|
63
|
|
|
$this->timeout = isset($timeout) ? (int) $timeout : null; |
|
64
|
|
|
|
|
65
|
|
|
$sm = $this->getSchemaManager(); |
|
66
|
|
|
$isErrored = false; |
|
67
|
|
|
|
|
68
|
|
View Code Duplication |
foreach ($create as $option) { |
|
|
|
|
|
|
69
|
|
|
try { |
|
70
|
|
|
if (isset($class)) { |
|
71
|
|
|
$this->{'processDocument' . ucfirst($option)}($sm, $class); |
|
72
|
|
|
} else { |
|
73
|
|
|
$this->{'process' . ucfirst($option)}($sm); |
|
74
|
|
|
} |
|
75
|
|
|
$output->writeln(sprintf( |
|
76
|
|
|
'Created <comment>%s%s</comment> for <info>%s</info>', |
|
77
|
|
|
$option, |
|
78
|
|
|
(isset($class) ? (self::INDEX === $option ? '(es)' : '') : (self::INDEX === $option ? 'es' : 's')), |
|
79
|
|
|
(isset($class) ? $class : 'all classes') |
|
80
|
|
|
)); |
|
81
|
|
|
} catch (\Exception $e) { |
|
82
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
83
|
|
|
$isErrored = true; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return ($isErrored) ? 255 : 0; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function processDocumentCollection(SchemaManager $sm, $document) |
|
91
|
|
|
{ |
|
92
|
|
|
$sm->createDocumentCollection($document); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected function processCollection(SchemaManager $sm) |
|
96
|
|
|
{ |
|
97
|
|
|
$sm->createCollections(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function processDocumentDb(SchemaManager $sm, $document) |
|
101
|
|
|
{ |
|
102
|
|
|
$sm->createDocumentDatabase($document); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function processDb(SchemaManager $sm) |
|
106
|
|
|
{ |
|
107
|
|
|
$sm->createDatabases(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
protected function processDocumentIndex(SchemaManager $sm, $document) |
|
111
|
|
|
{ |
|
112
|
|
|
$sm->ensureDocumentIndexes($document, $this->timeout); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
protected function processIndex(SchemaManager $sm) |
|
116
|
|
|
{ |
|
117
|
|
|
$sm->ensureIndexes($this->timeout); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
protected function processDocumentProxy(SchemaManager $sm, $document) |
|
|
|
|
|
|
121
|
|
|
{ |
|
122
|
|
|
$this->getDocumentManager()->getProxyFactory()->generateProxyClasses(array($this->getMetadataFactory()->getMetadataFor($document))); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected function processProxy(SchemaManager $sm) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
$this->getDocumentManager()->getProxyFactory()->generateProxyClasses($this->getMetadataFactory()->getAllMetadata()); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.