Completed
Pull Request — master (#719)
by
unknown
07:43
created

DoctrineBundle.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Doctrine Bundle
5
 *
6
 * The code was originally distributed inside the Symfony framework.
7
 *
8
 * (c) Fabien Potencier <[email protected]>
9
 * (c) Doctrine Project, Benjamin Eberlei <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Doctrine\Bundle\DoctrineBundle;
16
17
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
18
use Doctrine\Common\Util\ClassUtils;
19
use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
20
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
21
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\ImportDoctrineCommand;
22
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\RunSqlDoctrineCommand;
23
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass;
24
use Doctrine\ORM\Proxy\Autoloader;
25
use Symfony\Component\Console\Application;
26
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
27
use Symfony\Component\DependencyInjection\ContainerBuilder;
28
use Symfony\Component\HttpKernel\Bundle\Bundle;
29
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass;
30
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
31
use Symfony\Bridge\Doctrine\DependencyInjection\Security\UserProvider\EntityFactory;
32
33
/**
34
 * Bundle.
35
 *
36
 * @author Fabien Potencier <[email protected]>
37
 * @author Jonathan H. Wage <[email protected]>
38
 */
39
class DoctrineBundle extends Bundle
40
{
41
    private $autoloader;
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function build(ContainerBuilder $container)
47
    {
48
        parent::build($container);
49
50
        $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine.connections', 'doctrine.dbal.%s_connection.event_manager', 'doctrine'), PassConfig::TYPE_BEFORE_OPTIMIZATION);
51
52
        if ($container->hasExtension('security')) {
53
            $container->getExtension('security')->addUserProviderFactory(new EntityFactory('entity', 'doctrine.orm.security.user.provider'));
0 ignored issues
show
The method addUserProviderFactory() does not seem to exist on object<Symfony\Component...ion\ExtensionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
        }
55
56
        $container->addCompilerPass(new DoctrineValidationPass('orm'));
57
        $container->addCompilerPass(new EntityListenerPass());
58
        $container->addCompilerPass(new ServiceRepositoryCompilerPass());
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     */
64
    public function boot()
65
    {
66
        // Register an autoloader for proxies to avoid issues when unserializing them
67
        // when the ORM is used.
68
        if ($this->container->hasParameter('doctrine.orm.proxy_namespace')) {
69
            $namespace = $this->container->getParameter('doctrine.orm.proxy_namespace');
70
            $dir = $this->container->getParameter('doctrine.orm.proxy_dir');
71
            $proxyGenerator = null;
72
73
            if ($this->container->getParameter('doctrine.orm.auto_generate_proxy_classes')) {
74
                // See https://github.com/symfony/symfony/pull/3419 for usage of references
75
                $container = &$this->container;
76
77
                $proxyGenerator = function ($proxyDir, $proxyNamespace, $class) use (&$container) {
78
                    $originalClassName = ClassUtils::getRealClass($class);
79
                    /** @var $registry Registry */
80
                    $registry = $container->get('doctrine');
81
82
                    // Tries to auto-generate the proxy file
83
                    /** @var $em \Doctrine\ORM\EntityManager */
84
                    foreach ($registry->getManagers() as $em) {
85
                        if (!$em->getConfiguration()->getAutoGenerateProxyClasses()) {
86
                            continue;
87
                        }
88
89
                        $metadataFactory = $em->getMetadataFactory();
90
91
                        if ($metadataFactory->isTransient($originalClassName)) {
92
                            continue;
93
                        }
94
95
                        $classMetadata = $metadataFactory->getMetadataFor($originalClassName);
96
97
                        $em->getProxyFactory()->generateProxyClasses(array($classMetadata));
98
99
                        clearstatcache(true, Autoloader::resolveFile($proxyDir, $proxyNamespace, $class));
100
101
                        break;
102
                    }
103
                };
104
            }
105
106
            $this->autoloader = Autoloader::register($dir, $namespace, $proxyGenerator);
107
        }
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113
    public function shutdown()
114
    {
115
        if (null !== $this->autoloader) {
116
            spl_autoload_unregister($this->autoloader);
117
            $this->autoloader = null;
118
        }
119
120
        // Clear all entity managers to clear references to entities for GC
121 View Code Duplication
        if ($this->container->hasParameter('doctrine.entity_managers')) {
122
            foreach ($this->container->getParameter('doctrine.entity_managers') as $id) {
123
                if (!method_exists($this->container, 'initialized') || $this->container->initialized($id)) {
124
                    $this->container->get($id)->clear();
125
                }
126
            }
127
        }
128
129
        // Close all connections to avoid reaching too many connections in the process when booting again later (tests)
130 View Code Duplication
        if ($this->container->hasParameter('doctrine.connections')) {
131
            foreach ($this->container->getParameter('doctrine.connections') as $id) {
132
                if (!method_exists($this->container, 'initialized') || $this->container->initialized($id)) {
133
                    $this->container->get($id)->close();
134
                }
135
            }
136
        }
137
    }
138
139
    /**
140
     * {@inheritDoc}
141
     */
142
    public function registerCommands(Application $application)
143
    {
144
    }
145
}
146