forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoctrineODMCommand.php
121 lines (106 loc) · 4.23 KB
/
DoctrineODMCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\DoctrineMongoDBBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\Command;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Doctrine\ODM\MongoDB\Tools\Console\Helper\DocumentManagerHelper;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Doctrine\ODM\MongoDB\Tools\DisconnectedClassMetadataFactory;
use Doctrine\ODM\MongoDB\Tools\DocumentGenerator;
/**
* Base class for Doctrine ODM console commands to extend.
*
* @author Justin Hileman <justin@shopopensky.com>
*/
abstract class DoctrineODMCommand extends Command
{
public static function setApplicationDocumentManager(Application $application, $dmName)
{
$container = $application->getKernel()->getContainer();
$dmName = $dmName ? $dmName : 'default';
$dmServiceName = sprintf('doctrine.odm.mongodb.%s_document_manager', $dmName);
if (!$container->has($dmServiceName)) {
throw new \InvalidArgumentException(sprintf('Could not find Doctrine ODM DocumentManager named "%s"', $dmName));
}
$dm = $container->get($dmServiceName);
$helperSet = $application->getHelperSet();
$helperSet->set(new DocumentManagerHelper($dm), 'dm');
}
protected function getDocumentGenerator()
{
$documentGenerator = new DocumentGenerator();
$documentGenerator->setAnnotationPrefix('mongodb:');
$documentGenerator->setGenerateAnnotations(false);
$documentGenerator->setGenerateStubMethods(true);
$documentGenerator->setRegenerateDocumentIfExists(false);
$documentGenerator->setUpdateDocumentIfExists(true);
$documentGenerator->setNumSpaces(4);
return $documentGenerator;
}
protected function getDoctrineDocumentManagers()
{
$documentManagerNames = $this->container->getParameter('doctrine.odm.mongodb.document_managers');
$documentManagers = array();
foreach ($documentManagerNames as $documentManagerName) {
$dm = $this->container->get(sprintf('doctrine.odm.mongodb.%s_document_manager', $documentManagerName));
$documentManagers[] = $dm;
}
return $documentManagers;
}
protected function getBundleMetadatas(Bundle $bundle)
{
$namespace = $bundle->getNamespace();
$bundleMetadatas = array();
$documentManagers = $this->getDoctrineDocumentManagers();
foreach ($documentManagers as $key => $dm) {
$cmf = new DisconnectedClassMetadataFactory();
$cmf->setDocumentManager($dm);
$cmf->setConfiguration($dm->getConfiguration());
$metadatas = $cmf->getAllMetadata();
foreach ($metadatas as $metadata) {
if (strpos($metadata->name, $namespace) === 0) {
$bundleMetadatas[$metadata->name] = $metadata;
}
}
}
return $bundleMetadatas;
}
protected function findBundle($bundleName)
{
$foundBundle = false;
foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) {
/* @var $bundle Bundle */
if (strtolower($bundleName) == strtolower($bundle->getName())) {
$foundBundle = $bundle;
break;
}
}
if (!$foundBundle) {
throw new \InvalidArgumentException("No bundle " . $bundleName . " was found.");
}
return $foundBundle;
}
/**
* Transform classname to a path $foundBundle substract it to get the destination
*
* @param Bundle $bundle
* @return string
*/
protected function findBasePathForBundle($bundle)
{
$path = str_replace('\\', '/', $bundle->getNamespace());
$search = str_replace('\\', '/', $bundle->getPath());
$destination = str_replace('/'.$path, '', $search, $c);
if ($c != 1) {
throw new \RuntimeException(sprintf('Can\'t find base path for bundle (path: "%s", destination: "%s").', $path, $destination));
}
return $destination;
}
}