1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema; |
4
|
|
|
|
5
|
|
|
use Doctrine\ODM\MongoDB\SchemaManager; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
class DropCommand extends AbstractCommand |
11
|
|
|
{ |
12
|
|
|
private $dropOrder = array(self::INDEX, self::COLLECTION, self::DB); |
13
|
|
|
|
14
|
|
|
protected function configure() |
15
|
|
|
{ |
16
|
|
|
$this |
17
|
|
|
->setName('odm:schema:drop') |
18
|
|
|
->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)') |
19
|
|
|
->addOption(self::DB, null, InputOption::VALUE_NONE, 'Drop databases') |
20
|
|
|
->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Drop collections') |
21
|
|
|
->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Drop indexes') |
22
|
|
|
->setDescription('Drop databases, collections and indexes for your documents') |
23
|
|
|
; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
27
|
|
|
{ |
28
|
|
|
$drop = array_filter($this->dropOrder, function ($option) use ($input) { |
29
|
|
|
return $input->getOption($option); |
30
|
|
|
}); |
31
|
|
|
|
32
|
|
|
// Default to the full drop order if no options were specified |
33
|
|
|
$drop = empty($drop) ? $this->dropOrder : $drop; |
34
|
|
|
|
35
|
|
|
$class = $input->getOption('class'); |
36
|
|
|
$sm = $this->getSchemaManager(); |
37
|
|
|
$isErrored = false; |
38
|
|
|
|
39
|
|
View Code Duplication |
foreach ($drop as $option) { |
|
|
|
|
40
|
|
|
try { |
41
|
|
|
if (isset($class)) { |
42
|
|
|
$this->{'processDocument' . ucfirst($option)}($sm, $class); |
43
|
|
|
} else { |
44
|
|
|
$this->{'process' . ucfirst($option)}($sm); |
45
|
|
|
} |
46
|
|
|
$output->writeln(sprintf( |
47
|
|
|
'Dropped <comment>%s%s</comment> for <info>%s</info>', |
48
|
|
|
$option, |
49
|
|
|
(isset($class) ? (self::INDEX === $option ? '(es)' : '') : (self::INDEX === $option ? 'es' : 's')), |
50
|
|
|
($class ?? 'all classes') |
51
|
|
|
)); |
52
|
|
|
} catch (\Exception $e) { |
53
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
54
|
|
|
$isErrored = true; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $isErrored ? 255 : 0; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function processDocumentCollection(SchemaManager $sm, $document) |
62
|
|
|
{ |
63
|
|
|
$sm->dropDocumentCollection($document); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function processCollection(SchemaManager $sm) |
67
|
|
|
{ |
68
|
|
|
$sm->dropCollections(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function processDocumentDb(SchemaManager $sm, $document) |
72
|
|
|
{ |
73
|
|
|
$sm->dropDocumentDatabase($document); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function processDb(SchemaManager $sm) |
77
|
|
|
{ |
78
|
|
|
$sm->dropDatabases(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function processDocumentIndex(SchemaManager $sm, $document) |
82
|
|
|
{ |
83
|
|
|
$sm->deleteDocumentIndexes($document); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function processIndex(SchemaManager $sm) |
87
|
|
|
{ |
88
|
|
|
$sm->deleteIndexes(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.