forked from doctrine/DoctrineMongoDBBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoctrineMongoDBBundle.php
109 lines (93 loc) · 4.51 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
<?php
/*
* This file is part of the Doctrine MongoDBBundle
*
* The code was originally distributed inside the Symfony framework.
*
* (c) Fabien Potencier <[email protected]>
* (c) Doctrine Project
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Doctrine\Bundle\MongoDBBundle;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CreateHydratorDirectoryPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CreateProxyDirectoryPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\DoctrineMongoDBExtension;
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass;
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
use Symfony\Bridge\Doctrine\DependencyInjection\Security\UserProvider\EntityFactory;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* Doctrine MongoDB ODM bundle.
*
* @author Bulat Shakirzyanov <[email protected]>
* @author Kris Wallsmith <[email protected]>
* @author Jonathan H. Wage <[email protected]>
*/
class DoctrineMongoDBBundle extends Bundle
{
private $autoloader;
public function build(ContainerBuilder $container)
{
$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'));
if ($container->hasExtension('security')) {
$container->getExtension('security')->addUserProviderFactory(new EntityFactory('mongodb', 'doctrine_mongodb.odm.security.user.provider'));
}
}
public function getContainerExtension()
{
return new DoctrineMongoDBExtension();
}
public function boot()
{
// Register an autoloader for proxies to avoid issues when unserializing them
// when the ODM is used.
if ($this->container->hasParameter('doctrine_mongodb.odm.proxy_namespace')) {
$namespace = $this->container->getParameter('doctrine_mongodb.odm.proxy_namespace');
$dir = $this->container->getParameter('doctrine_mongodb.odm.proxy_dir');
// See https://github.com/symfony/symfony/pull/3419 for usage of
// references
$container =& $this->container;
$this->autoloader = function($class) use ($namespace, $dir, &$container) {
if (0 === strpos($class, $namespace)) {
$fileName = str_replace('\\', '', substr($class, strlen($namespace) +1));
$file = $dir.DIRECTORY_SEPARATOR.$fileName.'.php';
if (!is_file($file) && $container->getParameter('doctrine_mongodb.odm.auto_generate_proxy_classes')) {
$originalClassName = ClassUtils::getRealClass($class);
$registry = $container->get('doctrine_mongodb');
// Tries to auto-generate the proxy file
foreach ($registry->getManagers() as $dm) {
if ($dm->getConfiguration()->getAutoGenerateProxyClasses()) {
$classes = $dm->getMetadataFactory()->getAllMetadata();
foreach ($classes as $classMetadata) {
if ($classMetadata->name == $originalClassName) {
$dm->getProxyFactory()->generateProxyClasses(array($classMetadata));
}
}
}
}
clearstatcache($file);
}
if (is_file($file)) {
require $file;
}
}
};
spl_autoload_register($this->autoloader);
}
}
public function shutdown()
{
if (null !== $this->autoloader) {
spl_autoload_unregister($this->autoloader);
$this->autoloader = null;
}
}
}