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.manager_type and |
21
|
|
|
* gesdinet_jwt_refresh_token.refresh_token_class config parameters. |
22
|
|
|
* Depending on the value of manager_type Doctrine's ORM or ODM mappings will be used. |
23
|
|
|
* If refresh_token_class parameter contains user-defined entity, RefreshToken will be registered as a mapped |
24
|
|
|
* superclass, not as an entity, to prevent Doctrine creating table for it and avoid conflicts with user-defined |
25
|
|
|
* entity. |
26
|
|
|
* |
27
|
|
|
* @param ContainerBuilder $container |
28
|
|
|
*/ |
29
|
|
|
public function process(ContainerBuilder $container) |
30
|
|
|
{ |
31
|
|
|
$config = $container->getExtensionConfig('gesdinet_jwt_refresh_token')[0]; |
32
|
|
|
|
33
|
|
View Code Duplication |
if (!class_exists('Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass') |
|
|
|
|
34
|
|
|
&& (isset($config['manager_type']) && 'mongodb' === strtolower($config['manager_type'])) |
35
|
|
|
) { |
36
|
|
|
// skip MongoDB ODM mappings |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
if (!class_exists('Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass') |
|
|
|
|
41
|
|
|
&& (!isset($config['manager_type']) || 'mongodb' !== strtolower($config['manager_type'])) |
42
|
|
|
) { |
43
|
|
|
// skip ORM mappings |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$mappingPass = isset($config['manager_type']) && 'mongodb' === strtolower($config['manager_type']) |
48
|
|
|
? $this->getODMCompilerPass($config) |
49
|
|
|
: $this->getORMCompilerPass($config); |
50
|
|
|
|
51
|
|
|
$mappingPass->process($container); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array $config |
56
|
|
|
* |
57
|
|
|
* @return CompilerPassInterface |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
protected function getORMCompilerPass(array $config) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$nameSpace = 'Gesdinet\JWTRefreshTokenBundle\Entity'; |
62
|
|
|
$mappings = array( |
63
|
|
|
realpath(dirname(dirname(__DIR__)).'/Resources/config/orm/doctrine-orm') => $nameSpace, |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
if (isset($config['refresh_token_class']) || isset($config['refresh_token_entity'])) { |
67
|
|
|
$mappings[realpath(dirname(dirname(__DIR__)).'/Resources/config/orm/doctrine-superclass')] = $nameSpace; |
68
|
|
|
} else { |
69
|
|
|
$mappings[realpath(dirname(dirname(__DIR__)).'/Resources/config/orm/doctrine-entity')] = $nameSpace; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return DoctrineOrmMappingsPass::createYamlMappingDriver($mappings); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $config |
77
|
|
|
* |
78
|
|
|
* @return CompilerPassInterface |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
protected function getODMCompilerPass(array $config) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$nameSpace = 'Gesdinet\JWTRefreshTokenBundle\Document'; |
83
|
|
|
$mappings = array( |
84
|
|
|
realpath(dirname(__DIR__, 2).'/Resources/config/mongodb/doctrine-mongodb') => $nameSpace, |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
if (isset($config['refresh_token_class']) || isset($config['refresh_token_entity'])) { |
88
|
|
|
$mappings[realpath(dirname(dirname(__DIR__)).'/Resources/config/mongodb/doctrine-superclass')] = $nameSpace; |
89
|
|
|
} else { |
90
|
|
|
$mappings[realpath(dirname(dirname(__DIR__)).'/Resources/config/mongodb/doctrine-document')] = $nameSpace; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return DoctrineMongoDBMappingsPass::createYamlMappingDriver($mappings, array()); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.