forked from doctrine/DoctrineCouchDBBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoctrineCouchDBBundle.php
100 lines (81 loc) · 4.01 KB
/
DoctrineCouchDBBundle.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
<?php
/*
* Doctrine CouchDB Bundle
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/
namespace Doctrine\Bundle\CouchDBBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Doctrine\Bundle\CouchDBBundle\DependencyInjection\Compiler\RegisterEventListenersAndSubscribersPass;
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass;
use Symfony\Bridge\Doctrine\DependencyInjection\Security\UserProvider\EntityFactory;
class DoctrineCouchDBBundle extends Bundle
{
/**
* @var Closure
*/
private $autoloader;
public function build(ContainerBuilder $container)
{
parent::build($container);
if ($container->hasExtension('security')) {
$container->getExtension('security')->addUserProviderFactory(new EntityFactory('couchdb', 'doctrine_couchdb.odm.security.user.provider'));
}
$container->addCompilerPass(new RegisterEventListenersAndSubscribersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
$container->addCompilerPass(new DoctrineValidationPass('couchdb'));
}
public function boot()
{
// force Doctrine annotations to be loaded
// should be removed when a better solution is found in Doctrine
class_exists('Doctrine\ODM\CouchDB\Mapping\Driver\AnnotationDriver');
// Register an autoloader for proxies to avoid issues when unserializing them
// when the ORM is used.
if ($this->container->hasParameter('doctrine_couchdb.odm.proxy_namespace')) {
$namespace = $this->container->getParameter('doctrine_couchdb.odm.proxy_namespace');
$dir = $this->container->getParameter('doctrine_couchdb.odm.proxy_dir');
$container = $this->container;
$this->autoloader = function($class) use ($namespace, $dir, $container) {
if (0 === strpos($class, $namespace)) {
$className = substr($class, strlen($namespace) +1);
$file = $dir.DIRECTORY_SEPARATOR.$className.'.php';
if (!is_file($file) && $container->getParameter('doctrine_couchdb.odm.auto_generate_proxy_classes')) {
$originalClassName = substr($className, 0, -5);
$registry = $container->get('doctrine_couchdb');
// Tries to auto-generate the proxy file
foreach ($registry->getManagers() as $manager) {
if ($manager->getConfiguration()->getAutoGenerateProxyClasses()) {
$classes = $manager->getMetadataFactory()->getAllMetadata();
foreach ($classes as $class) {
$name = str_replace('\\', '', $class->name);
if ($name == $originalClassName) {
$manager->getProxyFactory()->generateProxyClasses(array($class));
}
}
}
}
clearstatcache($file);
if (!is_file($file)) {
throw new \RuntimeException(sprintf('The proxy file "%s" does not exist. If you still have objects serialized in the session, you need to clear the session manually.', $file));
}
}
require $file;
}
};
spl_autoload_register($this->autoloader);
}
}
public function shutdown()
{
spl_autoload_unregister($this->autoloader);
}
}