1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gesdinet\JWTRefreshTokenBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass; |
6
|
|
|
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass; |
7
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class DoctrineMappingsCompilerPass. |
12
|
|
|
* |
13
|
|
|
* We can't add DoctrineOrmMappingsPass directly, because in GesdinetJWTRefreshTokenBundle->build we don't have current |
14
|
|
|
* bundle configuration yet. |
15
|
|
|
* This CompilerPass is effectively just a wrapper for DoctrineOrmMappingsPass, which passes mappings conditionally. |
16
|
|
|
*/ |
17
|
|
|
final class DoctrineMappingsCompilerPass implements CompilerPassInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Process Doctrine mappings based on gesdinet_jwt_refresh_token.refresh_token_entity config parameter. |
21
|
|
|
* If this parameter contains user-defined entity, RefreshToken will be registered as a mapped superclass, not as an |
22
|
|
|
* entity, to prevent Doctrine creating table for it and avoid conflicts with user-defined entity. |
23
|
|
|
* |
24
|
|
|
* @param ContainerBuilder $container |
25
|
|
|
*/ |
26
|
|
|
public function process(ContainerBuilder $container) |
27
|
|
|
{ |
28
|
|
|
$config = $container->getExtensionConfig('gesdinet_jwt_refresh_token')[0]; |
29
|
|
|
|
30
|
|
|
$mappingPass = 'mongodb' === strtolower($config['manager_type']) |
31
|
|
|
? $this->getODMCompilerPass($config) |
32
|
|
|
: $this->getORMCompilerPass($config); |
33
|
|
|
|
34
|
|
|
$mappingPass->process($container); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $config |
39
|
|
|
* |
40
|
|
|
* @return CompilerPassInterface |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
protected function getORMCompilerPass(array $config) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$nameSpace = 'Gesdinet\JWTRefreshTokenBundle\Entity'; |
45
|
|
|
$mappings = array( |
46
|
|
|
realpath(dirname(__DIR__, 2).'/Resources/config/orm/doctrine-orm') => $nameSpace, |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
if (isset($config['refresh_token_class']) || isset($config['refresh_token_entity'])) { |
50
|
|
|
$mappings[realpath(dirname(__DIR__, 2).'/Resources/config/orm/doctrine-superclass')] = $nameSpace; |
51
|
|
|
} else { |
52
|
|
|
$mappings[realpath(dirname(__DIR__, 2).'/Resources/config/orm/doctrine-entity')] = $nameSpace; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return DoctrineOrmMappingsPass::createYamlMappingDriver($mappings); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param array $config |
60
|
|
|
* |
61
|
|
|
* @return CompilerPassInterface |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
protected function getODMCompilerPass(array $config) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
$nameSpace = 'Gesdinet\JWTRefreshTokenBundle\Document'; |
66
|
|
|
$mappings = array( |
67
|
|
|
realpath(dirname(__DIR__, 2).'/Resources/config/mongodb/doctrine-mongodb') => $nameSpace, |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
if (isset($config['refresh_token_class']) || isset($config['refresh_token_entity'])) { |
71
|
|
|
$mappings[realpath(dirname(__DIR__, 2).'/Resources/config/mongodb/doctrine-superclass')] = $nameSpace; |
72
|
|
|
} else { |
73
|
|
|
$mappings[realpath(dirname(__DIR__, 2).'/Resources/config/mongodb/doctrine-document')] = $nameSpace; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return DoctrineMongoDBMappingsPass::createYamlMappingDriver($mappings, array()); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.