Completed
Push — master ( 601d80...357a47 )
by Andreas
01:53 queued 10s
created

Configuration::getOrmEntityManagersNode()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 162

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 162
rs 8
c 0
b 0
f 0
cc 3
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection;
4
5
use Doctrine\ORM\EntityManager;
6
use ReflectionClass;
7
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
8
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
9
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
10
use Symfony\Component\Config\Definition\ConfigurationInterface;
11
use Symfony\Component\DependencyInjection\Exception\LogicException;
12
use function array_key_exists;
13
use function in_array;
14
use function is_array;
15
16
/**
17
 * This class contains the configuration information for the bundle
18
 *
19
 * This information is solely responsible for how the different configuration
20
 * sections are normalized, and merged.
21
 */
22
class Configuration implements ConfigurationInterface
23
{
24
    /** @var bool */
25
    private $debug;
26
27
    /**
28
     * @param bool $debug Whether to use the debug mode
29
     */
30
    public function __construct($debug)
31
    {
32
        $this->debug = (bool) $debug;
33
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public function getConfigTreeBuilder() : TreeBuilder
39
    {
40
        $treeBuilder = new TreeBuilder('doctrine');
41
42
        if (method_exists($treeBuilder, 'getRootNode')) {
43
            $rootNode = $treeBuilder->getRootNode();
44
        } else {
45
            // BC layer for symfony/config 4.1 and older
46
            $rootNode = $treeBuilder->root('doctrine');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
47
        }
48
49
        $this->addDbalSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
50
        $this->addOrmSection($rootNode);
0 ignored issues
show
Compatibility introduced by
$rootNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
51
52
        return $treeBuilder;
53
    }
54
55
    /**
56
     * Add DBAL section to configuration tree
57
     */
58
    private function addDbalSection(ArrayNodeDefinition $node)
59
    {
60
        $node
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method children() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
61
            ->children()
62
            ->arrayNode('dbal')
63
                ->beforeNormalization()
64
                    ->ifTrue(static function ($v) {
65
                        return is_array($v) && ! array_key_exists('connections', $v) && ! array_key_exists('connection', $v);
66
                    })
67
                    ->then(static function ($v) {
68
                        // Key that should not be rewritten to the connection config
69
                        $excludedKeys = ['default_connection' => true, 'types' => true, 'type' => true];
70
                        $connection   = [];
71 View Code Duplication
                        foreach ($v as $key => $value) {
0 ignored issues
show
Duplication introduced by
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...
72
                            if (isset($excludedKeys[$key])) {
73
                                continue;
74
                            }
75
                            $connection[$key] = $v[$key];
76
                            unset($v[$key]);
77
                        }
78
                        $v['default_connection'] = isset($v['default_connection']) ? (string) $v['default_connection'] : 'default';
79
                        $v['connections']        = [$v['default_connection'] => $connection];
80
81
                        return $v;
82
                    })
83
                ->end()
84
                ->children()
85
                    ->scalarNode('default_connection')->end()
86
                ->end()
87
                ->fixXmlConfig('type')
88
                ->children()
89
                    ->arrayNode('types')
90
                        ->useAttributeAsKey('name')
91
                        ->prototype('array')
92
                            ->beforeNormalization()
93
                                ->ifString()
94
                                ->then(static function ($v) {
95
                                    return ['class' => $v];
96
                                })
97
                            ->end()
98
                            ->children()
99
                                ->scalarNode('class')->isRequired()->end()
100
                                ->booleanNode('commented')->defaultNull()->end()
101
                            ->end()
102
                        ->end()
103
                    ->end()
104
                ->end()
105
                ->fixXmlConfig('connection')
106
                ->append($this->getDbalConnectionsNode())
107
            ->end();
108
    }
109
110
    /**
111
     * Return the dbal connections node
112
     *
113
     * @return ArrayNodeDefinition
114
     */
115
    private function getDbalConnectionsNode()
116
    {
117
        $treeBuilder = new TreeBuilder('connections');
118
119
        if (method_exists($treeBuilder, 'getRootNode')) {
120
            $node = $treeBuilder->getRootNode();
121
        } else {
122
            // BC layer for symfony/config 4.1 and older
123
            $node = $treeBuilder->root('connections');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
124
        }
125
126
        /** @var ArrayNodeDefinition $connectionNode */
127
        $connectionNode = $node
128
            ->requiresAtLeastOneElement()
129
            ->useAttributeAsKey('name')
130
            ->prototype('array');
131
132
        $this->configureDbalDriverNode($connectionNode);
133
134
        $connectionNode
135
            ->fixXmlConfig('option')
136
            ->fixXmlConfig('mapping_type')
137
            ->fixXmlConfig('slave')
138
            ->fixXmlConfig('shard')
139
            ->fixXmlConfig('default_table_option')
140
            ->children()
141
                ->scalarNode('driver')->defaultValue('pdo_mysql')->end()
142
                ->scalarNode('platform_service')->end()
143
                ->booleanNode('auto_commit')->end()
144
                ->scalarNode('schema_filter')->end()
145
                ->booleanNode('logging')->defaultValue($this->debug)->end()
146
                ->booleanNode('profiling')->defaultValue($this->debug)->end()
147
                ->booleanNode('profiling_collect_backtrace')
148
                    ->defaultValue(false)
149
                    ->info('Enables collecting backtraces when profiling is enabled')
150
                ->end()
151
                ->scalarNode('server_version')->end()
152
                ->scalarNode('driver_class')->end()
153
                ->scalarNode('wrapper_class')->end()
154
                ->scalarNode('shard_manager_class')->end()
155
                ->scalarNode('shard_choser')->end()
156
                ->scalarNode('shard_choser_service')->end()
157
                ->booleanNode('keep_slave')->end()
158
                ->arrayNode('options')
159
                    ->useAttributeAsKey('key')
160
                    ->prototype('scalar')->end()
161
                ->end()
162
                ->arrayNode('mapping_types')
163
                    ->useAttributeAsKey('name')
164
                    ->prototype('scalar')->end()
165
                ->end()
166
                ->arrayNode('default_table_options')
167
                    ->info("This option is used by the schema-tool and affects generated SQL. Possible keys include 'charset','collate', and 'engine'.")
168
                    ->useAttributeAsKey('name')
169
                    ->prototype('scalar')->end()
170
                ->end()
171
            ->end();
172
173
        $slaveNode = $connectionNode
174
            ->children()
175
                ->arrayNode('slaves')
176
                    ->useAttributeAsKey('name')
177
                    ->prototype('array');
178
        $this->configureDbalDriverNode($slaveNode);
0 ignored issues
show
Compatibility introduced by
$slaveNode of type object<Symfony\Component...Builder\NodeDefinition> is not a sub-type of object<Symfony\Component...er\ArrayNodeDefinition>. It seems like you assume a child class of the class Symfony\Component\Config...\Builder\NodeDefinition to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
179
180
        $shardNode = $connectionNode
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method children() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
181
            ->children()
182
                ->arrayNode('shards')
183
                    ->prototype('array')
184
                    ->children()
185
                        ->integerNode('id')
186
                            ->min(1)
187
                            ->isRequired()
188
                        ->end()
189
                    ->end();
190
        $this->configureDbalDriverNode($shardNode);
191
192
        return $node;
193
    }
194
195
    /**
196
     * Adds config keys related to params processed by the DBAL drivers
197
     *
198
     * These keys are available for slave configurations too.
199
     */
200
    private function configureDbalDriverNode(ArrayNodeDefinition $node)
201
    {
202
        $node
203
            ->children()
204
                ->scalarNode('url')->info('A URL with connection information; any parameter value parsed from this string will override explicitly set parameters')->end()
205
                ->scalarNode('dbname')->end()
206
                ->scalarNode('host')->defaultValue('localhost')->end()
207
                ->scalarNode('port')->defaultNull()->end()
208
                ->scalarNode('user')->defaultValue('root')->end()
209
                ->scalarNode('password')->defaultNull()->end()
210
                ->scalarNode('application_name')->end()
211
                ->scalarNode('charset')->end()
212
                ->scalarNode('path')->end()
213
                ->booleanNode('memory')->end()
214
                ->scalarNode('unix_socket')->info('The unix socket to use for MySQL')->end()
215
                ->booleanNode('persistent')->info('True to use as persistent connection for the ibm_db2 driver')->end()
216
                ->scalarNode('protocol')->info('The protocol to use for the ibm_db2 driver (default to TCPIP if ommited)')->end()
217
                ->booleanNode('service')
218
                    ->info('True to use SERVICE_NAME as connection parameter instead of SID for Oracle')
219
                ->end()
220
                ->scalarNode('servicename')
221
                    ->info(
222
                        'Overrules dbname parameter if given and used as SERVICE_NAME or SID connection parameter ' .
223
                        'for Oracle depending on the service parameter.'
224
                    )
225
                ->end()
226
                ->scalarNode('sessionMode')
227
                    ->info('The session mode to use for the oci8 driver')
228
                ->end()
229
                ->scalarNode('server')
230
                    ->info('The name of a running database server to connect to for SQL Anywhere.')
231
                ->end()
232
                ->scalarNode('default_dbname')
233
                    ->info(
234
                        'Override the default database (postgres) to connect to for PostgreSQL connexion.'
235
                    )
236
                ->end()
237
                ->scalarNode('sslmode')
238
                    ->info(
239
                        'Determines whether or with what priority a SSL TCP/IP connection will be negotiated with ' .
240
                        'the server for PostgreSQL.'
241
                    )
242
                ->end()
243
                ->scalarNode('sslrootcert')
244
                    ->info(
245
                        'The name of a file containing SSL certificate authority (CA) certificate(s). ' .
246
                        'If the file exists, the server\'s certificate will be verified to be signed by one of these authorities.'
247
                    )
248
                ->end()
249
                ->scalarNode('sslcert')
250
                    ->info(
251
                        'The path to the SSL client certificate file for PostgreSQL.'
252
                    )
253
                ->end()
254
                ->scalarNode('sslkey')
255
                    ->info(
256
                        'The path to the SSL client key file for PostgreSQL.'
257
                    )
258
                ->end()
259
                ->scalarNode('sslcrl')
260
                    ->info(
261
                        'The file name of the SSL certificate revocation list for PostgreSQL.'
262
                    )
263
                ->end()
264
                ->booleanNode('pooled')->info('True to use a pooled server with the oci8/pdo_oracle driver')->end()
265
                ->booleanNode('MultipleActiveResultSets')->info('Configuring MultipleActiveResultSets for the pdo_sqlsrv driver')->end()
266
                ->booleanNode('use_savepoints')->info('Use savepoints for nested transactions')->end()
267
                ->scalarNode('instancename')
268
                ->info(
269
                    'Optional parameter, complete whether to add the INSTANCE_NAME parameter in the connection.' .
270
                    ' It is generally used to connect to an Oracle RAC server to select the name' .
271
                    ' of a particular instance.'
272
                )
273
                ->end()
274
                ->scalarNode('connectstring')
275
                ->info(
276
                    'Complete Easy Connect connection descriptor, see https://docs.oracle.com/database/121/NETAG/naming.htm.' .
277
                    'When using this option, you will still need to provide the user and password parameters, but the other ' .
278
                    'parameters will no longer be used. Note that when using this parameter, the getHost and getPort methods' .
279
                    ' from Doctrine\DBAL\Connection will no longer function as expected.'
280
                )
281
                ->end()
282
            ->end()
283
            ->beforeNormalization()
284
                ->ifTrue(static function ($v) {
285
                    return ! isset($v['sessionMode']) && isset($v['session_mode']);
286
                })
287
                ->then(static function ($v) {
288
                    $v['sessionMode'] = $v['session_mode'];
289
                    unset($v['session_mode']);
290
291
                    return $v;
292
                })
293
            ->end()
294
            ->beforeNormalization()
295
                ->ifTrue(static function ($v) {
296
                    return ! isset($v['MultipleActiveResultSets']) && isset($v['multiple_active_result_sets']);
297
                })
298
                ->then(static function ($v) {
299
                    $v['MultipleActiveResultSets'] = $v['multiple_active_result_sets'];
300
                    unset($v['multiple_active_result_sets']);
301
302
                    return $v;
303
                })
304
            ->end();
305
    }
306
307
    /**
308
     * Add the ORM section to configuration tree
309
     */
310
    private function addOrmSection(ArrayNodeDefinition $node)
311
    {
312
        $node
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Config...\Builder\NodeDefinition as the method children() does only exist in the following sub-classes of Symfony\Component\Config...\Builder\NodeDefinition: Symfony\Component\Config...der\ArrayNodeDefinition. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
313
            ->children()
314
                ->arrayNode('orm')
315
                    ->beforeNormalization()
316
                        ->ifTrue(static function ($v) {
317
                            if (! empty($v) && ! class_exists(EntityManager::class)) {
318
                                throw new LogicException('The doctrine/orm package is required when the doctrine.orm config is set.');
319
                            }
320
321
                            return $v === null || (is_array($v) && ! array_key_exists('entity_managers', $v) && ! array_key_exists('entity_manager', $v));
322
                        })
323
                        ->then(static function ($v) {
324
                            $v = (array) $v;
325
                            // Key that should not be rewritten to the connection config
326
                            $excludedKeys  = [
327
                                'default_entity_manager' => true,
328
                                'auto_generate_proxy_classes' => true,
329
                                'proxy_dir' => true,
330
                                'proxy_namespace' => true,
331
                                'resolve_target_entities' => true,
332
                                'resolve_target_entity' => true,
333
                            ];
334
                            $entityManager = [];
335 View Code Duplication
                            foreach ($v as $key => $value) {
0 ignored issues
show
Duplication introduced by
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...
336
                                if (isset($excludedKeys[$key])) {
337
                                    continue;
338
                                }
339
                                $entityManager[$key] = $v[$key];
340
                                unset($v[$key]);
341
                            }
342
                            $v['default_entity_manager'] = isset($v['default_entity_manager']) ? (string) $v['default_entity_manager'] : 'default';
343
                            $v['entity_managers']        = [$v['default_entity_manager'] => $entityManager];
344
345
                            return $v;
346
                        })
347
                    ->end()
348
                    ->children()
349
                        ->scalarNode('default_entity_manager')->end()
350
                        ->scalarNode('auto_generate_proxy_classes')->defaultValue(false)
351
                            ->info('Auto generate mode possible values are: "NEVER", "ALWAYS", "FILE_NOT_EXISTS", "EVAL"')
352
                            ->validate()
353
                                ->ifTrue(function ($v) {
354
                                    $generationModes = $this->getAutoGenerateModes();
355
356
                                    if (is_int($v) && in_array($v, $generationModes['values']/*array(0, 1, 2, 3)*/)) {
357
                                        return false;
358
                                    }
359
                                    if (is_bool($v)) {
360
                                        return false;
361
                                    }
362
                                    if (is_string($v)) {
363
                                        if (in_array(strtoupper($v), $generationModes['names']/*array('NEVER', 'ALWAYS', 'FILE_NOT_EXISTS', 'EVAL')*/)) {
364
                                            return false;
365
                                        }
366
                                    }
367
368
                                    return true;
369
                                })
370
                                ->thenInvalid('Invalid auto generate mode value %s')
371
                            ->end()
372
                            ->validate()
373
                                ->ifString()
374
                                ->then(static function ($v) {
375
                                    return constant('Doctrine\Common\Proxy\AbstractProxyFactory::AUTOGENERATE_' . strtoupper($v));
376
                                })
377
                            ->end()
378
                        ->end()
379
                        ->scalarNode('proxy_dir')->defaultValue('%kernel.cache_dir%/doctrine/orm/Proxies')->end()
380
                        ->scalarNode('proxy_namespace')->defaultValue('Proxies')->end()
381
                    ->end()
382
                    ->fixXmlConfig('entity_manager')
383
                    ->append($this->getOrmEntityManagersNode())
384
                    ->fixXmlConfig('resolve_target_entity', 'resolve_target_entities')
385
                    ->append($this->getOrmTargetEntityResolverNode())
386
                ->end()
387
            ->end();
388
    }
389
390
    /**
391
     * Return ORM target entity resolver node
392
     *
393
     * @return NodeDefinition
394
     */
395
    private function getOrmTargetEntityResolverNode()
396
    {
397
        $treeBuilder = new TreeBuilder('resolve_target_entities');
398
399
        if (method_exists($treeBuilder, 'getRootNode')) {
400
            $node = $treeBuilder->getRootNode();
401
        } else {
402
            // BC layer for symfony/config 4.1 and older
403
            $node = $treeBuilder->root('resolve_target_entities');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
404
        }
405
406
        $node
0 ignored issues
show
Bug introduced by
The method useAttributeAsKey() does not exist on Symfony\Component\Config...\Builder\NodeDefinition. Did you maybe mean attribute()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
407
            ->useAttributeAsKey('interface')
408
            ->prototype('scalar')
409
                ->cannotBeEmpty()
410
            ->end();
411
412
        return $node;
413
    }
414
415
    /**
416
     * Return ORM entity listener node
417
     *
418
     * @return NodeDefinition
419
     */
420
    private function getOrmEntityListenersNode()
421
    {
422
        $treeBuilder = new TreeBuilder('entity_listeners');
423
424
        if (method_exists($treeBuilder, 'getRootNode')) {
425
            $node = $treeBuilder->getRootNode();
426
        } else {
427
            // BC layer for symfony/config 4.1 and older
428
            $node = $treeBuilder->root('entity_listeners');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
429
        }
430
431
        $normalizer = static function ($mappings) {
432
            $entities = [];
433
434
            foreach ($mappings as $entityClass => $mapping) {
435
                $listeners = [];
436
437
                foreach ($mapping as $listenerClass => $listenerEvent) {
438
                    $events = [];
439
440
                    foreach ($listenerEvent as $eventType => $eventMapping) {
441
                        if ($eventMapping === null) {
442
                            $eventMapping = [null];
443
                        }
444
445
                        foreach ($eventMapping as $method) {
446
                            $events[] = [
447
                                'type' => $eventType,
448
                                'method' => $method,
449
                            ];
450
                        }
451
                    }
452
453
                    $listeners[] = [
454
                        'class' => $listenerClass,
455
                        'event' => $events,
456
                    ];
457
                }
458
459
                $entities[] = [
460
                    'class' => $entityClass,
461
                    'listener' => $listeners,
462
                ];
463
            }
464
465
            return ['entities' => $entities];
466
        };
467
468
        $node
469
            ->beforeNormalization()
470
                // Yaml normalization
471
                ->ifTrue(static function ($v) {
472
                    return is_array(reset($v)) && is_string(key(reset($v)));
473
                })
474
                ->then($normalizer)
475
            ->end()
476
            ->fixXmlConfig('entity', 'entities')
477
            ->children()
478
                ->arrayNode('entities')
479
                    ->useAttributeAsKey('class')
480
                    ->prototype('array')
481
                        ->fixXmlConfig('listener')
482
                        ->children()
483
                            ->arrayNode('listeners')
484
                                ->useAttributeAsKey('class')
485
                                ->prototype('array')
486
                                    ->fixXmlConfig('event')
487
                                    ->children()
488
                                        ->arrayNode('events')
489
                                            ->prototype('array')
490
                                                ->children()
491
                                                    ->scalarNode('type')->end()
492
                                                    ->scalarNode('method')->defaultNull()->end()
493
                                                ->end()
494
                                            ->end()
495
                                        ->end()
496
                                    ->end()
497
                                ->end()
498
                            ->end()
499
                        ->end()
500
                    ->end()
501
                ->end()
502
            ->end();
503
504
        return $node;
505
    }
506
507
    /**
508
     * Return ORM entity manager node
509
     *
510
     * @return ArrayNodeDefinition
511
     */
512
    private function getOrmEntityManagersNode()
513
    {
514
        $treeBuilder = new TreeBuilder('entity_managers');
515
516
        if (method_exists($treeBuilder, 'getRootNode')) {
517
            $node = $treeBuilder->getRootNode();
518
        } else {
519
            // BC layer for symfony/config 4.1 and older
520
            $node = $treeBuilder->root('entity_managers');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
521
        }
522
523
        $node
524
            ->requiresAtLeastOneElement()
525
            ->useAttributeAsKey('name')
526
            ->prototype('array')
527
                ->addDefaultsIfNotSet()
528
                ->append($this->getOrmCacheDriverNode('query_cache_driver'))
529
                ->append($this->getOrmCacheDriverNode('metadata_cache_driver'))
530
                ->append($this->getOrmCacheDriverNode('result_cache_driver'))
531
                ->append($this->getOrmEntityListenersNode())
532
                ->children()
533
                    ->scalarNode('connection')->end()
534
                    ->scalarNode('class_metadata_factory_name')->defaultValue('Doctrine\ORM\Mapping\ClassMetadataFactory')->end()
535
                    ->scalarNode('default_repository_class')->defaultValue('Doctrine\ORM\EntityRepository')->end()
536
                    ->scalarNode('auto_mapping')->defaultFalse()->end()
537
                    ->scalarNode('naming_strategy')->defaultValue('doctrine.orm.naming_strategy.default')->end()
538
                    ->scalarNode('quote_strategy')->defaultValue('doctrine.orm.quote_strategy.default')->end()
539
                    ->scalarNode('entity_listener_resolver')->defaultNull()->end()
540
                    ->scalarNode('repository_factory')->defaultValue('doctrine.orm.container_repository_factory')->end()
541
                ->end()
542
                ->children()
543
                    ->arrayNode('second_level_cache')
544
                        ->children()
545
                            ->append($this->getOrmCacheDriverNode('region_cache_driver'))
546
                            ->scalarNode('region_lock_lifetime')->defaultValue(60)->end()
547
                            ->booleanNode('log_enabled')->defaultValue($this->debug)->end()
548
                            ->scalarNode('region_lifetime')->defaultValue(0)->end()
549
                            ->booleanNode('enabled')->defaultValue(true)->end()
550
                            ->scalarNode('factory')->end()
551
                        ->end()
552
                        ->fixXmlConfig('region')
553
                        ->children()
554
                            ->arrayNode('regions')
555
                                ->useAttributeAsKey('name')
556
                                ->prototype('array')
557
                                    ->children()
558
                                        ->append($this->getOrmCacheDriverNode('cache_driver'))
559
                                        ->scalarNode('lock_path')->defaultValue('%kernel.cache_dir%/doctrine/orm/slc/filelock')->end()
560
                                        ->scalarNode('lock_lifetime')->defaultValue(60)->end()
561
                                        ->scalarNode('type')->defaultValue('default')->end()
562
                                        ->scalarNode('lifetime')->defaultValue(0)->end()
563
                                        ->scalarNode('service')->end()
564
                                        ->scalarNode('name')->end()
565
                                    ->end()
566
                                ->end()
567
                            ->end()
568
                        ->end()
569
                        ->fixXmlConfig('logger')
570
                        ->children()
571
                            ->arrayNode('loggers')
572
                                ->useAttributeAsKey('name')
573
                                ->prototype('array')
574
                                    ->children()
575
                                        ->scalarNode('name')->end()
576
                                        ->scalarNode('service')->end()
577
                                    ->end()
578
                                ->end()
579
                            ->end()
580
                        ->end()
581
                    ->end()
582
                ->end()
583
                ->fixXmlConfig('hydrator')
584
                ->children()
585
                    ->arrayNode('hydrators')
586
                        ->useAttributeAsKey('name')
587
                        ->prototype('scalar')->end()
588
                    ->end()
589
                ->end()
590
                ->fixXmlConfig('mapping')
591
                ->children()
592
                    ->arrayNode('mappings')
593
                        ->useAttributeAsKey('name')
594
                        ->prototype('array')
595
                            ->beforeNormalization()
596
                                ->ifString()
597
                                ->then(static function ($v) {
598
                                    return ['type' => $v];
599
                                })
600
                            ->end()
601
                            ->treatNullLike([])
602
                            ->treatFalseLike(['mapping' => false])
603
                            ->performNoDeepMerging()
604
                            ->children()
605
                                ->scalarNode('mapping')->defaultValue(true)->end()
606
                                ->scalarNode('type')->end()
607
                                ->scalarNode('dir')->end()
608
                                ->scalarNode('alias')->end()
609
                                ->scalarNode('prefix')->end()
610
                                ->booleanNode('is_bundle')->end()
611
                            ->end()
612
                        ->end()
613
                    ->end()
614
                    ->arrayNode('dql')
615
                        ->fixXmlConfig('string_function')
616
                        ->fixXmlConfig('numeric_function')
617
                        ->fixXmlConfig('datetime_function')
618
                        ->children()
619
                            ->arrayNode('string_functions')
620
                                ->useAttributeAsKey('name')
621
                                ->prototype('scalar')->end()
622
                            ->end()
623
                            ->arrayNode('numeric_functions')
624
                                ->useAttributeAsKey('name')
625
                                ->prototype('scalar')->end()
626
                            ->end()
627
                            ->arrayNode('datetime_functions')
628
                                ->useAttributeAsKey('name')
629
                                ->prototype('scalar')->end()
630
                            ->end()
631
                        ->end()
632
                    ->end()
633
                ->end()
634
                ->fixXmlConfig('filter')
635
                ->children()
636
                    ->arrayNode('filters')
637
                        ->info('Register SQL Filters in the entity manager')
638
                        ->useAttributeAsKey('name')
639
                        ->prototype('array')
640
                            ->beforeNormalization()
641
                                ->ifString()
642
                                ->then(static function ($v) {
643
                                    return ['class' => $v];
644
                                })
645
                            ->end()
646
                            ->beforeNormalization()
647
                                // The content of the XML node is returned as the "value" key so we need to rename it
648
                                ->ifTrue(static function ($v) {
649
                                    return is_array($v) && isset($v['value']);
650
                                })
651
                                ->then(static function ($v) {
652
                                    $v['class'] = $v['value'];
653
                                    unset($v['value']);
654
655
                                    return $v;
656
                                })
657
                            ->end()
658
                            ->fixXmlConfig('parameter')
659
                            ->children()
660
                                ->scalarNode('class')->isRequired()->end()
661
                                ->booleanNode('enabled')->defaultFalse()->end()
662
                                ->arrayNode('parameters')
663
                                    ->useAttributeAsKey('name')
664
                                    ->prototype('variable')->end()
665
                                ->end()
666
                            ->end()
667
                        ->end()
668
                    ->end()
669
                ->end()
670
            ->end();
671
672
        return $node;
673
    }
674
675
    /**
676
     * Return a ORM cache driver node for an given entity manager
677
     *
678
     * @param string $name
679
     *
680
     * @return ArrayNodeDefinition
681
     */
682
    private function getOrmCacheDriverNode($name)
683
    {
684
        $treeBuilder = new TreeBuilder($name);
685
686
        if (method_exists($treeBuilder, 'getRootNode')) {
687
            $node = $treeBuilder->getRootNode();
688
        } else {
689
            // BC layer for symfony/config 4.1 and older
690
            $node = $treeBuilder->root($name);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
691
        }
692
693
        $node
694
            ->addDefaultsIfNotSet()
695
            ->beforeNormalization()
696
                ->ifString()
697
                ->then(static function ($v) : array {
698
                    return ['type' => $v];
699
                })
700
            ->end()
701
            ->children()
702
                ->scalarNode('type')->defaultNull()->end()
703
                ->scalarNode('id')->end()
704
                ->scalarNode('pool')->end()
705
            ->end();
706
707
        return $node;
708
    }
709
710
    /**
711
     * Find proxy auto generate modes for their names and int values
712
     *
713
     * @return array
714
     */
715
    private function getAutoGenerateModes()
716
    {
717
        $constPrefix = 'AUTOGENERATE_';
718
        $prefixLen   = strlen($constPrefix);
719
        $refClass    = new ReflectionClass('Doctrine\Common\Proxy\AbstractProxyFactory');
720
        $constsArray = $refClass->getConstants();
721
        $namesArray  = [];
722
        $valuesArray = [];
723
724
        foreach ($constsArray as $key => $value) {
725
            if (strpos($key, $constPrefix) !== 0) {
726
                continue;
727
            }
728
729
            $namesArray[]  = substr($key, $prefixLen);
730
            $valuesArray[] = (int) $value;
731
        }
732
733
        return [
734
            'names' => $namesArray,
735
            'values' => $valuesArray,
736
        ];
737
    }
738
}
739