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