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
|
|
|
class ShardCommand extends AbstractCommand |
28
|
|
|
{ |
29
|
|
|
protected function configure() |
30
|
|
|
{ |
31
|
|
|
$this |
32
|
|
|
->setName('odm:schema:shard') |
33
|
|
|
->addOption('class', 'c', InputOption::VALUE_OPTIONAL, 'Document class to process (default: all classes)') |
34
|
|
|
->setDescription('Enable sharding for selected documents') |
35
|
|
|
; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param InputInterface $input |
40
|
|
|
* @param OutputInterface $output |
41
|
|
|
* @return int|null|void |
42
|
|
|
*/ |
43
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
44
|
|
|
{ |
45
|
|
|
$class = $input->getOption('class'); |
46
|
|
|
|
47
|
|
|
$sm = $this->getSchemaManager(); |
48
|
|
|
$isErrored = false; |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
View Code Duplication |
if (isset($class)) { |
|
|
|
|
52
|
|
|
$this->processDocumentIndex($sm, $class); |
53
|
|
|
$output->writeln(sprintf('Enabled sharding for <info>%s</info>', $class)); |
54
|
|
|
} else { |
55
|
|
|
$this->processIndex($sm); |
56
|
|
|
$output->writeln('Enabled sharding for <info>all classes</info>'); |
57
|
|
|
} |
58
|
|
|
} catch (\Exception $e) { |
59
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
60
|
|
|
$isErrored = true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $isErrored ? 255 : 0; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param SchemaManager $sm |
68
|
|
|
* @param object $document |
69
|
|
|
*/ |
70
|
|
|
protected function processDocumentIndex(SchemaManager $sm, $document) |
71
|
|
|
{ |
72
|
|
|
$sm->ensureDocumentSharding($document); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param SchemaManager $sm |
77
|
|
|
*/ |
78
|
|
|
protected function processIndex(SchemaManager $sm) |
79
|
|
|
{ |
80
|
|
|
$sm->ensureSharding(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param SchemaManager $sm |
85
|
|
|
* @param object $document |
86
|
|
|
* @throws \BadMethodCallException |
87
|
|
|
*/ |
88
|
|
|
protected function processDocumentCollection(SchemaManager $sm, $document) |
89
|
|
|
{ |
90
|
|
|
throw new \BadMethodCallException('Cannot update a document collection'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param SchemaManager $sm |
95
|
|
|
* @throws \BadMethodCallException |
96
|
|
|
*/ |
97
|
|
|
protected function processCollection(SchemaManager $sm) |
98
|
|
|
{ |
99
|
|
|
throw new \BadMethodCallException('Cannot update a collection'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param SchemaManager $sm |
104
|
|
|
* @param object $document |
105
|
|
|
* @throws \BadMethodCallException |
106
|
|
|
*/ |
107
|
|
|
protected function processDocumentDb(SchemaManager $sm, $document) |
108
|
|
|
{ |
109
|
|
|
throw new \BadMethodCallException('Cannot update a document database'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param SchemaManager $sm |
114
|
|
|
* @throws \BadMethodCallException |
115
|
|
|
*/ |
116
|
|
|
protected function processDb(SchemaManager $sm) |
117
|
|
|
{ |
118
|
|
|
throw new \BadMethodCallException('Cannot update a database'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
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.