Completed
Push — master ( 7f2a01...44feb9 )
by
unknown
06:18 queued 04:12
created

DoctrineBundle.php (2 issues)

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
namespace Doctrine\Bundle\DoctrineBundle;
4
5
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\EntityListenerPass;
6
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
7
use Doctrine\Common\Util\ClassUtils;
8
use Doctrine\ORM\Proxy\Autoloader;
9
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\DoctrineValidationPass;
10
use Symfony\Bridge\Doctrine\DependencyInjection\CompilerPass\RegisterEventListenersAndSubscribersPass;
11
use Symfony\Bridge\Doctrine\DependencyInjection\Security\UserProvider\EntityFactory;
12
use Symfony\Component\Console\Application;
13
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\HttpKernel\Bundle\Bundle;
16
17
/**
18
 * Bundle.
19
 */
20
class DoctrineBundle extends Bundle
21
{
22
    /** @var callable|null */
23
    private $autoloader;
24
25
    /**
26
     * {@inheritDoc}
27
     */
28
    public function build(ContainerBuilder $container)
29
    {
30
        parent::build($container);
31
32
        $container->addCompilerPass(new RegisterEventListenersAndSubscribersPass('doctrine.connections', 'doctrine.dbal.%s_connection.event_manager', 'doctrine'), PassConfig::TYPE_BEFORE_OPTIMIZATION);
33
34
        if ($container->hasExtension('security')) {
35
            $container->getExtension('security')->addUserProviderFactory(new EntityFactory('entity', 'doctrine.orm.security.user.provider'));
36
        }
37
38
        $container->addCompilerPass(new DoctrineValidationPass('orm'));
39
        $container->addCompilerPass(new EntityListenerPass());
40
        $container->addCompilerPass(new ServiceRepositoryCompilerPass());
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function boot()
47
    {
48
        // Register an autoloader for proxies to avoid issues when unserializing them
49
        // when the ORM is used.
50
        if (! $this->container->hasParameter('doctrine.orm.proxy_namespace')) {
51
            return;
52
        }
53
54
        $namespace      = $this->container->getParameter('doctrine.orm.proxy_namespace');
55
        $dir            = $this->container->getParameter('doctrine.orm.proxy_dir');
56
        $proxyGenerator = null;
57
58
        if ($this->container->getParameter('doctrine.orm.auto_generate_proxy_classes')) {
59
            // See https://github.com/symfony/symfony/pull/3419 for usage of references
60
            $container = &$this->container;
61
62
            $proxyGenerator = function ($proxyDir, $proxyNamespace, $class) use (&$container) {
63
                $originalClassName = ClassUtils::getRealClass($class);
64
                /** @var Registry $registry */
65
                $registry = $container->get('doctrine');
66
67
                // Tries to auto-generate the proxy file
68
                /** @var $em \Doctrine\ORM\EntityManager */
69
                foreach ($registry->getManagers() as $em) {
70
                    if (! $em->getConfiguration()->getAutoGenerateProxyClasses()) {
71
                        continue;
72
                    }
73
74
                    $metadataFactory = $em->getMetadataFactory();
75
76
                    if ($metadataFactory->isTransient($originalClassName)) {
77
                        continue;
78
                    }
79
80
                    $classMetadata = $metadataFactory->getMetadataFor($originalClassName);
81
82
                    $em->getProxyFactory()->generateProxyClasses([$classMetadata]);
83
84
                    clearstatcache(true, Autoloader::resolveFile($proxyDir, $proxyNamespace, $class));
85
86
                    break;
87
                }
88
            };
89
        }
90
91
        $this->autoloader = Autoloader::register($dir, $namespace, $proxyGenerator);
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function shutdown()
98
    {
99
        if ($this->autoloader !== null) {
100
            spl_autoload_unregister($this->autoloader);
101
            $this->autoloader = null;
102
        }
103
104
        // Clear all entity managers to clear references to entities for GC
105
        if ($this->container->hasParameter('doctrine.entity_managers')) {
106 View Code Duplication
            foreach ($this->container->getParameter('doctrine.entity_managers') as $id) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
                if (method_exists($this->container, 'initialized') && ! $this->container->initialized($id)) {
108
                    continue;
109
                }
110
111
                $this->container->get($id)->clear();
112
            }
113
        }
114
115
        // Close all connections to avoid reaching too many connections in the process when booting again later (tests)
116
        if (! $this->container->hasParameter('doctrine.connections')) {
117
            return;
118
        }
119
120 View Code Duplication
        foreach ($this->container->getParameter('doctrine.connections') as $id) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
            if (method_exists($this->container, 'initialized') && ! $this->container->initialized($id)) {
122
                continue;
123
            }
124
125
            $this->container->get($id)->close();
126
        }
127
    }
128
129
    /**
130
     * {@inheritDoc}
131
     */
132
    public function registerCommands(Application $application)
133
    {
134
    }
135
}
136