1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema; |
6
|
|
|
|
7
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
8
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory; |
9
|
|
|
use Doctrine\ODM\MongoDB\SchemaManager; |
10
|
|
|
use MongoDB\Driver\WriteConcern; |
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use function is_string; |
15
|
|
|
|
16
|
|
|
abstract class AbstractCommand extends Command |
17
|
|
|
{ |
18
|
|
|
public const DB = 'db'; |
19
|
|
|
public const COLLECTION = 'collection'; |
20
|
|
|
public const INDEX = 'index'; |
21
|
|
|
|
22
|
|
|
protected function configure() |
23
|
|
|
{ |
24
|
|
|
parent::configure(); |
25
|
|
|
|
26
|
|
|
$this |
27
|
|
|
->addOption('maxTimeMs', null, InputOption::VALUE_REQUIRED, 'An optional maxTimeMs that will be used for all schema operations.') |
28
|
|
|
->addOption('w', null, InputOption::VALUE_REQUIRED, 'An optional w option for the write concern that will be used for all schema operations.') |
29
|
|
|
->addOption('wTimeout', null, InputOption::VALUE_REQUIRED, 'An optional wTimeout option for the write concern that will be used for all schema operations. This option will be ignored if no w option was specified.') |
30
|
|
|
->addOption('journal', null, InputOption::VALUE_REQUIRED, 'An optional journal option for the write concern that will be used for all schema operations. This option will be ignored if no w option was specified.'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
abstract protected function processDocumentCollection(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern); |
34
|
|
|
|
35
|
|
|
abstract protected function processCollection(SchemaManager $sm, ?int $maxTimeMs, ?WriteConcern $writeConcern); |
36
|
|
|
|
37
|
|
|
abstract protected function processDocumentDb(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern); |
38
|
|
|
|
39
|
|
|
abstract protected function processDb(SchemaManager $sm, ?int $maxTimeMs, ?WriteConcern $writeConcern); |
40
|
|
|
|
41
|
|
|
abstract protected function processDocumentIndex(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern); |
42
|
|
|
|
43
|
|
|
abstract protected function processIndex(SchemaManager $sm, ?int $maxTimeMs, ?WriteConcern $writeConcern); |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return SchemaManager |
47
|
|
|
*/ |
48
|
|
|
protected function getSchemaManager() |
49
|
|
|
{ |
50
|
|
|
return $this->getDocumentManager()->getSchemaManager(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return DocumentManager |
55
|
|
|
*/ |
56
|
|
|
protected function getDocumentManager() |
57
|
|
|
{ |
58
|
|
|
return $this->getHelper('documentManager')->getDocumentManager(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return ClassMetadataFactory |
63
|
|
|
*/ |
64
|
|
|
protected function getMetadataFactory() |
65
|
|
|
{ |
66
|
|
|
return $this->getDocumentManager()->getMetadataFactory(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function getMaxTimeMsFromInput(InputInterface $input) : ?int |
70
|
|
|
{ |
71
|
|
|
$maxTimeMs = $input->getOption('maxTimeMs'); |
72
|
|
|
|
73
|
|
|
return is_string($maxTimeMs) ? (int) $maxTimeMs : null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function getWriteConcernFromInput(InputInterface $input) : ?WriteConcern |
77
|
|
|
{ |
78
|
|
|
$w = $input->getOption('w'); |
79
|
|
|
if (! is_string($w)) { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$wTimeout = $input->getOption('wTimeout'); |
84
|
|
|
$wTimeout = is_string($wTimeout) ? (int) $wTimeout : 0; |
85
|
|
|
$journal = $input->getOption('journal'); |
86
|
|
|
$journal = is_string($journal) ? (bool) $journal : false; |
87
|
|
|
|
88
|
|
|
return new WriteConcern($w, $wTimeout, $journal); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|