Completed
Pull Request — master (#658)
by Magnus
02:24
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\Common\Util\ClassUtils;
18
use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
19
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
20
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\ImportDoctrineCommand;
21
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\RunSqlDoctrineCommand;
22
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass;
23
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\RepositoryAliasPass;
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\DependencyInjection\IntrospectableContainerInterface;
29
use Symfony\Component\HttpKernel\Bundle\Bundle;
30
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass;
31
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
32
use Symfony\Bridge\Doctrine\DependencyInjection\Security\UserProvider\EntityFactory;
33
34
/**
35
 * Bundle.
36
 *
37
 * @author Fabien Potencier <[email protected]>
38
 * @author Jonathan H. Wage <[email protected]>
39
 */
40
class DoctrineBundle extends Bundle
41
{
42
    private $autoloader;
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function build(ContainerBuilder $container)
48
    {
49
        parent::build($container);
50
51
        $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine.connections', 'doctrine.dbal.%s_connection.event_manager', 'doctrine'), PassConfig::TYPE_BEFORE_OPTIMIZATION);
52
53
        if ($container->hasExtension('security')) {
54
            $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...
55
        }
56
57
        $container->addCompilerPass(new DoctrineValidationPass('orm'));
58
        $container->addCompilerPass(new EntityListenerPass());
59
        $container->addCompilerPass(new RepositoryAliasPass());
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function boot()
66
    {
67
        // Register an autoloader for proxies to avoid issues when unserializing them
68
        // when the ORM is used.
69
        if ($this->container->hasParameter('doctrine.orm.proxy_namespace')) {
70
            $namespace = $this->container->getParameter('doctrine.orm.proxy_namespace');
71
            $dir = $this->container->getParameter('doctrine.orm.proxy_dir');
72
            $proxyGenerator = null;
73
74
            if ($this->container->getParameter('doctrine.orm.auto_generate_proxy_classes')) {
75
                // See https://github.com/symfony/symfony/pull/3419 for usage of references
76
                $container = &$this->container;
77
78
                $proxyGenerator = function ($proxyDir, $proxyNamespace, $class) use (&$container) {
79
                    $originalClassName = ClassUtils::getRealClass($class);
80
                    /** @var $registry Registry */
81
                    $registry = $container->get('doctrine');
82
83
                    // Tries to auto-generate the proxy file
84
                    /** @var $em \Doctrine\ORM\EntityManager */
85
                    foreach ($registry->getManagers() as $em) {
86
                        if (!$em->getConfiguration()->getAutoGenerateProxyClasses()) {
87
                            continue;
88
                        }
89
90
                        $metadataFactory = $em->getMetadataFactory();
91
92
                        if ($metadataFactory->isTransient($originalClassName)) {
93
                            continue;
94
                        }
95
96
                        $classMetadata = $metadataFactory->getMetadataFor($originalClassName);
97
98
                        $em->getProxyFactory()->generateProxyClasses(array($classMetadata));
99
100
                        clearstatcache(true, Autoloader::resolveFile($proxyDir, $proxyNamespace, $class));
101
102
                        break;
103
                    }
104
                };
105
            }
106
107
            $this->autoloader = Autoloader::register($dir, $namespace, $proxyGenerator);
108
        }
109
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114
    public function shutdown()
115
    {
116
        if (null !== $this->autoloader) {
117
            spl_autoload_unregister($this->autoloader);
118
            $this->autoloader = null;
119
        }
120
121
        // Clear all entity managers to clear references to entities for GC
122 View Code Duplication
        if ($this->container->hasParameter('doctrine.entity_managers')) {
123
            foreach ($this->container->getParameter('doctrine.entity_managers') as $id) {
124
                if (!$this->container instanceof IntrospectableContainerInterface || $this->container->initialized($id)) {
125
                    $this->container->get($id)->clear();
126
                }
127
            }
128
        }
129
130
        // Close all connections to avoid reaching too many connections in the process when booting again later (tests)
131 View Code Duplication
        if ($this->container->hasParameter('doctrine.connections')) {
132
            foreach ($this->container->getParameter('doctrine.connections') as $id) {
133
                if (!$this->container instanceof IntrospectableContainerInterface || $this->container->initialized($id)) {
134
                    $this->container->get($id)->close();
135
                }
136
            }
137
        }
138
    }
139
140
    /**
141
     * {@inheritDoc}
142
     */
143
    public function registerCommands(Application $application)
144
    {
145
        // Use the default logic when the ORM is available.
146
        // This avoids listing all ORM commands by hand.
147
        if (class_exists('Doctrine\\ORM\\Version')) {
148
            parent::registerCommands($application);
149
150
            return;
151
        }
152
153
        // Register only the DBAL commands if the ORM is not available.
154
        $application->add(new CreateDatabaseDoctrineCommand());
155
        $application->add(new DropDatabaseDoctrineCommand());
156
        $application->add(new RunSqlDoctrineCommand());
157
        $application->add(new ImportDoctrineCommand());
158
    }
159
}
160