Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 112 lines (94 sloc) 4.639 kb
577df30 @fabpot renamed Symfony\Framework to Symfony\Bundle
fabpot authored
1 <?php
2
08fd722 @blue-eyes normalized license messages in PHP files
blue-eyes authored
3 /*
6bed7e1 @stof Updated license header
stof authored
4 * This file is part of the Doctrine MongoDBBundle
5 *
6 * The code was originally distributed inside the Symfony framework.
08fd722 @blue-eyes normalized license messages in PHP files
blue-eyes authored
7 *
b278f2c @fabpot replaced symfony-project.org by symfony.com
fabpot authored
8 * (c) Fabien Potencier <fabien@symfony.com>
6bed7e1 @stof Updated license header
stof authored
9 * (c) Doctrine Project
08fd722 @blue-eyes normalized license messages in PHP files
blue-eyes authored
10 *
11 * For the full copyright and license information, please view the LICENSE
12 * file that was distributed with this source code.
13 */
14
7bf1302 @stof Changed the namespace to Doctrine\Bundle\MongoDBBundle
stof authored
15 namespace Doctrine\Bundle\MongoDBBundle;
577df30 @fabpot renamed Symfony\Framework to Symfony\Bundle
fabpot authored
16
1ca46b8 @stof Fixed the autoloader and removed the exception
stof authored
17 use Doctrine\Common\Util\ClassUtils;
7bf1302 @stof Changed the namespace to Doctrine\Bundle\MongoDBBundle
stof authored
18 use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CreateHydratorDirectoryPass;
19 use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\CreateProxyDirectoryPass;
20 use Doctrine\Bundle\MongoDBBundle\DependencyInjection\DoctrineMongoDBExtension;
4d86370 @stof Added the compiler pass to load the validation conditionally
stof authored
21 use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass;
43e34f4 @jmikola [DependencyInjection] Create event managers per connection (BC break)
jmikola authored
22 use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
bba3fe2 @stof Added a user provider
stof authored
23 use Symfony\Bridge\Doctrine\DependencyInjection\Security\UserProvider\EntityFactory;
a97293b @kriswallsmith sorted use statements
kriswallsmith authored
24 use Symfony\Component\DependencyInjection\Compiler\PassConfig;
25 use Symfony\Component\DependencyInjection\ContainerBuilder;
26 use Symfony\Component\HttpKernel\Bundle\Bundle;
577df30 @fabpot renamed Symfony\Framework to Symfony\Bundle
fabpot authored
27
28 /**
29 * Doctrine MongoDB ODM bundle.
30 *
62f4fce @jmikola Update author attribution for Bulat
jmikola authored
31 * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
03f2979 @kriswallsmith updated email address
kriswallsmith authored
32 * @author Kris Wallsmith <kris@symfony.com>
577df30 @fabpot renamed Symfony\Framework to Symfony\Bundle
fabpot authored
33 * @author Jonathan H. Wage <jonwage@gmail.com>
34 */
4a92955 @avalanche123 [DoctrineMongoDBBundle] removed trailing space
avalanche123 authored
35 class DoctrineMongoDBBundle extends Bundle
577df30 @fabpot renamed Symfony\Framework to Symfony\Bundle
fabpot authored
36 {
1180918 @stof Added an autoloader for proxies
stof authored
37 private $autoloader;
38
5842458 @fabpot added the concept of a main DIC extension for bundles
fabpot authored
39 public function build(ContainerBuilder $container)
9a8c63a @avalanche123 [DoctrineMongoDBBundle] switched to compiler passes for proxy/hydrator d...
avalanche123 authored
40 {
43e34f4 @jmikola [DependencyInjection] Create event managers per connection (BC break)
jmikola authored
41 $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine.odm.mongodb.connections', 'doctrine.odm.mongodb.%s_connection.event_manager', 'doctrine.odm.mongodb'), PassConfig::TYPE_BEFORE_OPTIMIZATION);
9a8c63a @avalanche123 [DoctrineMongoDBBundle] switched to compiler passes for proxy/hydrator d...
avalanche123 authored
42 $container->addCompilerPass(new CreateProxyDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);
43 $container->addCompilerPass(new CreateHydratorDirectoryPass(), PassConfig::TYPE_BEFORE_REMOVING);
4d86370 @stof Added the compiler pass to load the validation conditionally
stof authored
44 $container->addCompilerPass(new DoctrineValidationPass('mongodb'));
bba3fe2 @stof Added a user provider
stof authored
45
46 if ($container->hasExtension('security')) {
47 $container->getExtension('security')->addUserProviderFactory(new EntityFactory('mongodb', 'doctrine.odm.mongodb.security.user.provider'));
48 }
9a8c63a @avalanche123 [DoctrineMongoDBBundle] switched to compiler passes for proxy/hydrator d...
avalanche123 authored
49 }
6f9a3aa @kriswallsmith changed yaml extension alias from "doctrine_mongo_db" to "doctrine_mongo...
kriswallsmith authored
50
51 public function getContainerExtension()
52 {
53 return new DoctrineMongoDBExtension();
54 }
1180918 @stof Added an autoloader for proxies
stof authored
55
56 public function boot()
57 {
58 // Register an autoloader for proxies to avoid issues when unserializing them
59 // when the ODM is used.
60 if ($this->container->hasParameter('doctrine.odm.mongodb.proxy_namespace')) {
61 $namespace = $this->container->getParameter('doctrine.odm.mongodb.proxy_namespace');
62 $dir = $this->container->getParameter('doctrine.odm.mongodb.proxy_dir');
63 // See https://github.com/symfony/symfony/pull/3419 for usage of
64 // references
65 $container =& $this->container;
66
67 $this->autoloader = function($class) use ($namespace, $dir, &$container) {
68 if (0 === strpos($class, $namespace)) {
69 $className = str_replace('\\', '', substr($class, strlen($namespace) +1));
70 $file = $dir.DIRECTORY_SEPARATOR.$className.'.php';
71
72 if (!is_file($file) && $container->getParameter('kernel.debug')) {
1ca46b8 @stof Fixed the autoloader and removed the exception
stof authored
73 $originalClassName = ClassUtils::getRealClass($class);
1180918 @stof Added an autoloader for proxies
stof authored
74 $registry = $container->get('doctrine.odm.mongodb');
75
76 // Tries to auto-generate the proxy file
77 foreach ($registry->getManagers() as $dm) {
78
79 if ($dm->getConfiguration()->getAutoGenerateProxyClasses()) {
80 $classes = $dm->getMetadataFactory()->getAllMetadata();
81
82 foreach ($classes as $class) {
83 $name = str_replace('\\', '', $class->name);
84
85 if ($name == $originalClassName) {
86 $dm->getProxyFactory()->generateProxyClasses(array($class));
87 }
88 }
89 }
90 }
91
92 clearstatcache($file);
93 }
94
1ca46b8 @stof Fixed the autoloader and removed the exception
stof authored
95 if (is_file($file)) {
96 require $file;
97 }
1180918 @stof Added an autoloader for proxies
stof authored
98 }
99 };
100 spl_autoload_register($this->autoloader);
101 }
102 }
103
104 public function shutdown()
105 {
106 if (null !== $this->autoloader) {
107 spl_autoload_unregister($this->autoloader);
108 $this->autoloader = null;
109 }
110 }
22d95dd @fabpot [DoctrineMongoDBBundle] removed DoctrineMongoDBExtension constructor
fabpot authored
111 }
Something went wrong with that request. Please try again.