|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
|
6
|
|
|
use Doctrine\ODM\MongoDB\SchemaManager; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
class CreateCommand extends AbstractCommand |
|
12
|
|
|
{ |
|
13
|
|
|
private $createOrder = array(self::DB, self::COLLECTION, self::INDEX); |
|
14
|
|
|
|
|
15
|
|
|
private $timeout; |
|
16
|
|
|
|
|
17
|
|
|
protected function configure() |
|
18
|
|
|
{ |
|
19
|
|
|
$this |
|
20
|
|
|
->setName('odm:schema:create') |
|
21
|
|
|
->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)') |
|
22
|
|
|
->addOption('timeout', 't', InputOption::VALUE_OPTIONAL, 'Timeout (ms) for acknowledged index creation') |
|
23
|
|
|
->addOption(self::DB, null, InputOption::VALUE_NONE, 'Create databases') |
|
24
|
|
|
->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Create collections') |
|
25
|
|
|
->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Create indexes') |
|
26
|
|
|
->setDescription('Create databases, collections and indexes for your documents') |
|
27
|
|
|
; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
31
|
|
|
{ |
|
32
|
|
|
if ($input->getOption(self::DB)) { |
|
33
|
|
|
@trigger_error('The ' . self::DB . ' option is deprecated and will be removed in ODM 2.0', E_USER_DEPRECATED); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$create = array_filter($this->createOrder, function ($option) use ($input) { |
|
37
|
|
|
return $input->getOption($option); |
|
38
|
|
|
}); |
|
39
|
|
|
|
|
40
|
|
|
// Default to the full creation order if no options were specified |
|
41
|
|
|
$create = empty($create) ? $this->createOrder : $create; |
|
42
|
|
|
|
|
43
|
|
|
$class = $input->getOption('class'); |
|
44
|
|
|
|
|
45
|
|
|
$timeout = $input->getOption('timeout'); |
|
46
|
|
|
$this->timeout = isset($timeout) ? (int) $timeout : null; |
|
47
|
|
|
|
|
48
|
|
|
$sm = $this->getSchemaManager(); |
|
49
|
|
|
$isErrored = false; |
|
50
|
|
|
|
|
51
|
|
View Code Duplication |
foreach ($create as $option) { |
|
|
|
|
|
|
52
|
|
|
try { |
|
53
|
|
|
if (isset($class)) { |
|
54
|
|
|
$this->{'processDocument' . ucfirst($option)}($sm, $class); |
|
55
|
|
|
} else { |
|
56
|
|
|
$this->{'process' . ucfirst($option)}($sm); |
|
57
|
|
|
} |
|
58
|
|
|
$output->writeln(sprintf( |
|
59
|
|
|
'Created <comment>%s%s</comment> for <info>%s</info>', |
|
60
|
|
|
$option, |
|
61
|
|
|
(isset($class) ? (self::INDEX === $option ? '(es)' : '') : (self::INDEX === $option ? 'es' : 's')), |
|
62
|
|
|
($class ?? 'all classes') |
|
63
|
|
|
)); |
|
64
|
|
|
} catch (\Exception $e) { |
|
65
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
66
|
|
|
$isErrored = true; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $isErrored ? 255 : 0; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function processDocumentCollection(SchemaManager $sm, $document) |
|
74
|
|
|
{ |
|
75
|
|
|
$sm->createDocumentCollection($document); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function processCollection(SchemaManager $sm) |
|
79
|
|
|
{ |
|
80
|
|
|
$sm->createCollections(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function processDocumentDb(SchemaManager $sm, $document) |
|
84
|
|
|
{ |
|
85
|
|
|
$sm->createDocumentDatabase($document); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function processDb(SchemaManager $sm) |
|
89
|
|
|
{ |
|
90
|
|
|
$sm->createDatabases(); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
protected function processDocumentIndex(SchemaManager $sm, $document) |
|
94
|
|
|
{ |
|
95
|
|
|
$sm->ensureDocumentIndexes($document, $this->timeout); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function processIndex(SchemaManager $sm) |
|
99
|
|
|
{ |
|
100
|
|
|
$sm->ensureIndexes($this->timeout); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function processDocumentProxy(SchemaManager $sm, $document) |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
$classMetadata = $this->getMetadataFactory()->getMetadataFor($document); |
|
106
|
|
|
|
|
107
|
|
|
if (!$classMetadata->isEmbeddedDocument && !$classMetadata->isMappedSuperclass && !$classMetadata->isQueryResultDocument) { |
|
|
|
|
|
|
108
|
|
|
$this->getDocumentManager()->getProxyFactory()->generateProxyClasses(array($classMetadata)); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
protected function processProxy(SchemaManager $sm) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
$classes = array_filter($this->getMetadataFactory()->getAllMetadata(), function (ClassMetadata $classMetadata) { |
|
115
|
|
|
return !$classMetadata->isEmbeddedDocument && !$classMetadata->isMappedSuperclass && !$classMetadata->isQueryResultDocument; |
|
116
|
|
|
}); |
|
117
|
|
|
|
|
118
|
|
|
$this->getDocumentManager()->getProxyFactory()->generateProxyClasses($classes); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: