1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* generate MongoDB Fulltext-Search Indexes |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\GeneratorBundle\Command; |
7
|
|
|
|
8
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
10
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
11
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
12
|
|
|
use Symfony\Component\Console\Command\Command; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Here, we generate all MongoDB Fulltext-Search Indexes |
19
|
|
|
* |
20
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
21
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
22
|
|
|
* @link http://swisscom.ch |
23
|
|
|
*/ |
24
|
|
|
class SchemaExportCommand extends Command |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var documentManager |
29
|
|
|
*/ |
30
|
|
|
private $documentManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Filesystem |
34
|
|
|
*/ |
35
|
|
|
private $fileSystem; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* GenerateBuildIndexesCommand constructor. |
39
|
|
|
* |
40
|
|
|
* @param DocumentManager $documentManager The Doctrine Document Manager |
41
|
|
|
* @param Filesystem $fileSystem Sf file manager |
42
|
|
|
* @param String $name The Name of this Command |
|
|
|
|
43
|
|
|
*/ |
44
|
4 |
|
public function __construct( |
45
|
|
|
DocumentManager $documentManager, |
46
|
|
|
Filesystem $fileSystem, |
47
|
|
|
$name = null |
48
|
|
|
) { |
49
|
4 |
|
parent::__construct($name); |
50
|
|
|
|
51
|
4 |
|
$this->documentManager = $documentManager; |
|
|
|
|
52
|
4 |
|
$this->fileSystem = $fileSystem; |
53
|
4 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritDoc} |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
4 |
|
protected function configure() |
61
|
|
|
{ |
62
|
4 |
|
parent::configure(); |
63
|
|
|
|
64
|
2 |
|
$this |
65
|
4 |
|
->setName('graviton:generate:schema-export') |
66
|
4 |
|
->setDescription( |
67
|
2 |
|
'Generates Mongo DB schema output to root folder, schema.json' |
68
|
2 |
|
); |
69
|
4 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
* |
74
|
|
|
* @param InputInterface $input input |
75
|
|
|
* @param OutputInterface $output output |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
80
|
|
|
{ |
81
|
|
|
// Check mongo db version |
82
|
|
|
$mongoVersion = $this->getMongoDBVersion('Graviton\\CoreBundle\\Document\\App'); |
83
|
|
|
if ((float) $mongoVersion < 2.6) { |
84
|
|
|
$output->writeln("MongoDB Version < 2.6 installed: " . $mongoVersion); |
85
|
|
|
exit(); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** @var Application $application */ |
89
|
|
|
$application = $this->getApplication(); |
90
|
|
|
$container = $application->getKernel()->getContainer(); |
91
|
|
|
$rootDir = $container->getParameter('kernel.root_dir'); |
92
|
|
|
$baseDir = $rootDir . ((strpos($rootDir, 'vendor')) ? '/../../../../' : '/../'); |
93
|
|
|
|
94
|
|
|
$schema = []; |
95
|
|
|
/** @var ClassMetadata $metadata */ |
96
|
|
|
foreach ($this->documentManager->getMetadataFactory()->getAllMetadata() as $metadata) { |
97
|
|
|
$name = $metadata->getCollection(); |
98
|
|
|
if (array_key_exists($name, $schema)) { |
99
|
|
|
throw new InvalidConfigurationException(); |
100
|
|
|
} |
101
|
|
|
$schema[$name] = [ |
102
|
|
|
'class' => $metadata->getName(), |
103
|
|
|
'fields' => $metadata->fieldMappings, |
104
|
|
|
'relations' => $metadata->associationMappings |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->fileSystem->dumpFile( |
109
|
|
|
$baseDir . '/schema.json', |
110
|
|
|
json_encode($schema, JSON_PRETTY_PRINT) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Gets the installed MongoDB Version |
116
|
|
|
* @param String $className The Classname of the collection, needed to fetch the right DB connection |
117
|
|
|
* @return String getMongoDBVersion The version of the MongoDB as a string |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
private function getMongoDBVersion($className) |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$buildInfo = $this->documentManager->getDocumentDatabase($className)->command(['buildinfo' => 1]); |
122
|
|
|
if (isset($buildInfo['version'])) { |
123
|
|
|
return $buildInfo['version']; |
124
|
|
|
} else { |
125
|
|
|
return 'unkown'; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.