-
Notifications
You must be signed in to change notification settings - Fork 231
/
DoctrineMongoDBBundle.php
117 lines (96 loc) · 4.41 KB
/
DoctrineMongoDBBundle.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
<?php
declare(strict_types=1);
namespace Doctrine\Bundle\MongoDBBundle;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CacheCompatibilityPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CreateHydratorDirectoryPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CreateProxyDirectoryPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DeprecateChangedClassParametersPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\FixturesCompilerPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\DoctrineMongoDBExtension;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass;
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
use Symfony\Bridge\Doctrine\DependencyInjection\Security\UserProvider\EntityFactory;
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use function assert;
use function spl_autoload_register;
use function spl_autoload_unregister;
/**
* Doctrine MongoDB ODM bundle.
*/
class DoctrineMongoDBBundle extends Bundle
{
/** @var callable|null */
private $autoloader;
/** @return void */
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new CacheCompatibilityPass());
$container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine_mongodb.odm.connections', 'doctrine_mongodb.odm.%s_connection.event_manager', 'doctrine_mongodb.odm'), PassConfig::TYPE_BEFORE_OPTIMIZATION);
$container->addCompilerPass(new CreateProxyDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);
$container->addCompilerPass(new CreateHydratorDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);
$container->addCompilerPass(new DoctrineValidationPass('mongodb'));
$container->addCompilerPass(new ServiceRepositoryCompilerPass());
$container->addCompilerPass(new FixturesCompilerPass());
$container->addCompilerPass(new DeprecateChangedClassParametersPass());
if (! $container->hasExtension('security')) {
return;
}
$security = $container->getExtension('security');
if (! ($security instanceof SecurityExtension)) {
return;
}
$security->addUserProviderFactory(new EntityFactory('mongodb', 'doctrine_mongodb.odm.security.user.provider'));
}
public function getContainerExtension(): ?ExtensionInterface
{
return new DoctrineMongoDBExtension();
}
/** @return void */
public function boot()
{
$registry = $this->container->get('doctrine_mongodb');
assert($registry instanceof ManagerRegistry);
$this->registerAutoloader($registry->getManager());
$this->registerCommandLoggers();
}
private function registerAutoloader(DocumentManager $documentManager): void
{
$configuration = $documentManager->getConfiguration();
if ($configuration->getAutoGenerateProxyClasses() !== Configuration::AUTOGENERATE_FILE_NOT_EXISTS) {
return;
}
$this->autoloader = $configuration->getProxyManagerConfiguration()->getProxyAutoloader();
spl_autoload_register($this->autoloader);
}
private function unregisterAutoloader(): void
{
if ($this->autoloader === null) {
return;
}
spl_autoload_unregister($this->autoloader);
$this->autoloader = null;
}
private function registerCommandLoggers(): void
{
$commandLoggerRegistry = $this->container->get('doctrine_mongodb.odm.command_logger_registry');
$commandLoggerRegistry->register();
}
private function unregisterCommandLoggers(): void
{
$commandLoggerRegistry = $this->container->get('doctrine_mongodb.odm.command_logger_registry');
$commandLoggerRegistry->unregister();
}
/** @return void */
public function shutdown()
{
$this->unregisterAutoloader();
$this->unregisterCommandLoggers();
}
}