1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema; |
6
|
|
|
|
7
|
|
|
use BadMethodCallException; |
8
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
9
|
|
|
use Doctrine\ODM\MongoDB\SchemaManager; |
10
|
|
|
use MongoDB\Driver\WriteConcern; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Throwable; |
15
|
|
|
use function array_filter; |
16
|
|
|
use function assert; |
17
|
|
|
use function is_string; |
18
|
|
|
use function sprintf; |
19
|
|
|
use function ucfirst; |
20
|
|
|
|
21
|
|
|
class CreateCommand extends AbstractCommand |
22
|
|
|
{ |
23
|
|
|
/** @var string[] */ |
24
|
|
|
private $createOrder = [self::COLLECTION, self::INDEX]; |
25
|
|
|
|
26
|
|
|
protected function configure() |
27
|
|
|
{ |
28
|
|
|
parent::configure(); |
29
|
|
|
|
30
|
|
|
$this |
31
|
|
|
->setName('odm:schema:create') |
32
|
|
|
->addOption('class', 'c', InputOption::VALUE_REQUIRED, 'Document class to process (default: all classes)') |
33
|
|
|
->addOption(self::COLLECTION, null, InputOption::VALUE_NONE, 'Create collections') |
34
|
|
|
->addOption(self::INDEX, null, InputOption::VALUE_NONE, 'Create indexes') |
35
|
|
|
->addOption('background', null, InputOption::VALUE_NONE, sprintf('Create indexes in background (requires "%s" option)', self::INDEX)) |
36
|
|
|
->setDescription('Create databases, collections and indexes for your documents'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
40
|
|
|
{ |
41
|
|
|
$create = array_filter($this->createOrder, static function ($option) use ($input) { |
42
|
|
|
return $input->getOption($option); |
43
|
|
|
}); |
44
|
|
|
|
45
|
|
|
// Default to the full creation order if no options were specified |
46
|
|
|
$create = empty($create) ? $this->createOrder : $create; |
47
|
|
|
|
48
|
|
|
$class = $input->getOption('class'); |
49
|
|
|
$background = (bool) $input->getOption('background'); |
50
|
|
|
|
51
|
|
|
$sm = $this->getSchemaManager(); |
52
|
|
|
$isErrored = false; |
53
|
|
|
|
54
|
|
|
foreach ($create as $option) { |
55
|
|
|
try { |
56
|
|
|
if (isset($class)) { |
57
|
|
|
$this->{'processDocument' . ucfirst($option)}($sm, $class, $this->getMaxTimeMsFromInput($input), $this->getWriteConcernFromInput($input), $background); |
58
|
|
|
} else { |
59
|
|
|
$this->{'process' . ucfirst($option)}($sm, $this->getMaxTimeMsFromInput($input), $this->getWriteConcernFromInput($input), $background); |
60
|
|
|
} |
61
|
|
|
$output->writeln(sprintf( |
62
|
|
|
'Created <comment>%s%s</comment> for <info>%s</info>', |
63
|
|
|
$option, |
64
|
|
|
is_string($class) ? ($option === self::INDEX ? '(es)' : '') : ($option === self::INDEX ? 'es' : 's'), |
65
|
|
|
is_string($class) ? $class : 'all classes' |
66
|
|
|
)); |
67
|
|
|
} catch (Throwable $e) { |
68
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
69
|
|
|
$isErrored = true; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $isErrored ? 255 : 0; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function processDocumentCollection(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern) |
77
|
|
|
{ |
78
|
|
|
$sm->createDocumentCollection($document, $maxTimeMs, $writeConcern); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function processCollection(SchemaManager $sm, ?int $maxTimeMs, ?WriteConcern $writeConcern) |
82
|
|
|
{ |
83
|
|
|
$sm->createCollections($maxTimeMs, $writeConcern); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function processDocumentDb(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern) |
87
|
|
|
{ |
88
|
|
|
throw new BadMethodCallException('A database is created automatically by MongoDB (>= 3.0).'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function processDb(SchemaManager $sm, ?int $maxTimeMs, ?WriteConcern $writeConcern) |
92
|
|
|
{ |
93
|
|
|
throw new BadMethodCallException('A database is created automatically by MongoDB (>= 3.0).'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function processDocumentIndex(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern, bool $background = false) |
97
|
|
|
{ |
98
|
|
|
$sm->ensureDocumentIndexes($document, $maxTimeMs, $writeConcern, $background); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function processIndex(SchemaManager $sm, ?int $maxTimeMs, ?WriteConcern $writeConcern, bool $background = false) |
102
|
|
|
{ |
103
|
|
|
$sm->ensureIndexes($maxTimeMs, $writeConcern, $background); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function processDocumentProxy(SchemaManager $sm, string $document) |
107
|
|
|
{ |
108
|
|
|
$classMetadata = $this->getMetadataFactory()->getMetadataFor($document); |
109
|
|
|
assert($classMetadata instanceof ClassMetadata); |
110
|
|
|
|
111
|
|
|
$this->getDocumentManager()->getProxyFactory()->generateProxyClasses([$classMetadata]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function processProxy(SchemaManager $sm) |
115
|
|
|
{ |
116
|
|
|
/** @var ClassMetadata[] $metadatas */ |
117
|
|
|
$metadatas = $this->getMetadataFactory()->getAllMetadata(); |
118
|
|
|
$this->getDocumentManager()->getProxyFactory()->generateProxyClasses($metadatas); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|