|
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 DropCommand extends AbstractCommand |
|
31
|
|
|
{ |
|
32
|
|
|
private $dropOrder = array(self::INDEX, self::COLLECTION, self::DB); |
|
33
|
|
|
|
|
34
|
|
|
protected function configure() |
|
35
|
|
|
{ |
|
36
|
|
|
$this |
|
37
|
|
|
->setName('odm:schema:drop') |
|
38
|
|
|
->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)') |
|
39
|
|
|
->addOption(self::DB, null, InputOption::VALUE_NONE, 'Drop databases') |
|
40
|
|
|
->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Drop collections') |
|
41
|
|
|
->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Drop indexes') |
|
42
|
|
|
->setDescription('Drop databases, collections and indexes for your documents') |
|
43
|
|
|
; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
47
|
|
|
{ |
|
48
|
|
|
foreach ($this->dropOrder as $option) { |
|
49
|
|
|
if ($input->getOption($option)) { |
|
50
|
|
|
$drop[] = $option; |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Default to the full drop order if no options were specified |
|
55
|
|
|
$drop = empty($drop) ? $this->dropOrder : $drop; |
|
56
|
|
|
|
|
57
|
|
|
$class = $input->getOption('class'); |
|
58
|
|
|
$sm = $this->getSchemaManager(); |
|
59
|
|
|
$isErrored = false; |
|
60
|
|
|
|
|
61
|
|
View Code Duplication |
foreach ($drop as $option) { |
|
|
|
|
|
|
62
|
|
|
try { |
|
63
|
|
|
if (isset($class)) { |
|
64
|
|
|
$this->{'processDocument' . ucfirst($option)}($sm, $class); |
|
65
|
|
|
} else { |
|
66
|
|
|
$this->{'process' . ucfirst($option)}($sm); |
|
67
|
|
|
} |
|
68
|
|
|
$output->writeln(sprintf( |
|
69
|
|
|
'Dropped <comment>%s%s</comment> for <info>%s</info>', |
|
70
|
|
|
$option, |
|
71
|
|
|
(isset($class) ? (self::INDEX === $option ? '(es)' : '') : (self::INDEX === $option ? 'es' : 's')), |
|
72
|
|
|
(isset($class) ? $class : 'all classes') |
|
73
|
|
|
)); |
|
74
|
|
|
} catch (\Exception $e) { |
|
75
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
76
|
|
|
$isErrored = true; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return ($isErrored) ? 255 : 0; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function processDocumentCollection(SchemaManager $sm, $document) |
|
84
|
|
|
{ |
|
85
|
|
|
$sm->dropDocumentCollection($document); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function processCollection(SchemaManager $sm) |
|
89
|
|
|
{ |
|
90
|
|
|
$sm->dropCollections(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
protected function processDocumentDb(SchemaManager $sm, $document) |
|
94
|
|
|
{ |
|
95
|
|
|
$sm->dropDocumentDatabase($document); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function processDb(SchemaManager $sm) |
|
99
|
|
|
{ |
|
100
|
|
|
$sm->dropDatabases(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function processDocumentIndex(SchemaManager $sm, $document) |
|
104
|
|
|
{ |
|
105
|
|
|
$sm->deleteDocumentIndexes($document); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected function processIndex(SchemaManager $sm) |
|
109
|
|
|
{ |
|
110
|
|
|
$sm->deleteIndexes(); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
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.