Completed
Pull Request — master (#793)
by Gabriel
02:14
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
            $namespace      = $this->container->getParameter('doctrine.orm.proxy_namespace');
52
            $dir            = $this->container->getParameter('doctrine.orm.proxy_dir');
53
            $proxyGenerator = null;
54
55
            if ($this->container->getParameter('doctrine.orm.auto_generate_proxy_classes')) {
56
                // See https://github.com/symfony/symfony/pull/3419 for usage of references
57
                $container = &$this->container;
58
59
                $proxyGenerator = function ($proxyDir, $proxyNamespace, $class) use (&$container) {
60
                    $originalClassName = ClassUtils::getRealClass($class);
61
                    /** @var Registry $registry */
62
                    $registry = $container->get('doctrine');
63
64
                    // Tries to auto-generate the proxy file
65
                    /** @var $em \Doctrine\ORM\EntityManager */
66
                    foreach ($registry->getManagers() as $em) {
67
                        if (! $em->getConfiguration()->getAutoGenerateProxyClasses()) {
68
                            continue;
69
                        }
70
71
                        $metadataFactory = $em->getMetadataFactory();
72
73
                        if ($metadataFactory->isTransient($originalClassName)) {
74
                            continue;
75
                        }
76
77
                        $classMetadata = $metadataFactory->getMetadataFor($originalClassName);
78
79
                        $em->getProxyFactory()->generateProxyClasses([$classMetadata]);
80
81
                        clearstatcache(true, Autoloader::resolveFile($proxyDir, $proxyNamespace, $class));
82
83
                        break;
84
                    }
85
                };
86
            }
87
88
            $this->autoloader = Autoloader::register($dir, $namespace, $proxyGenerator);
89
        }
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public function shutdown()
96
    {
97
        if ($this->autoloader !== null) {
98
            spl_autoload_unregister($this->autoloader);
99
            $this->autoloader = null;
100
        }
101
102
        // Clear all entity managers to clear references to entities for GC
103 View Code Duplication
        if ($this->container->hasParameter('doctrine.entity_managers')) {
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...
104
            foreach ($this->container->getParameter('doctrine.entity_managers') as $id) {
105
                if (! method_exists($this->container, 'initialized') || $this->container->initialized($id)) {
106
                    $this->container->get($id)->clear();
107
                }
108
            }
109
        }
110
111
        // Close all connections to avoid reaching too many connections in the process when booting again later (tests)
112 View Code Duplication
        if ($this->container->hasParameter('doctrine.connections')) {
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...
113
            foreach ($this->container->getParameter('doctrine.connections') as $id) {
114
                if (! method_exists($this->container, 'initialized') || $this->container->initialized($id)) {
115
                    $this->container->get($id)->close();
116
                }
117
            }
118
        }
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124
    public function registerCommands(Application $application)
125
    {
126
    }
127
}
128