1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the TraitorBundle, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2016 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace RunOpenCode\Bundle\Traitor\DependencyInjection; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
13
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension; |
14
|
|
|
|
15
|
|
|
class Extension extends BaseExtension |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
4 |
|
public function load(array $configs, ContainerBuilder $container) |
21
|
|
|
{ |
22
|
4 |
|
$configuration = new Configuration(); |
23
|
4 |
|
$config = $this->processConfiguration($configuration, $configs); |
24
|
|
|
|
25
|
4 |
|
$this |
26
|
4 |
|
->setInjectionMapAsContainerParameter($config, $container) |
27
|
4 |
|
->setFiltersAsContainerParameter($config, $container) |
28
|
4 |
|
->setExclusionsAsContainerParameter($config, $container) |
29
|
|
|
; |
30
|
4 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
4 |
|
public function getAlias() |
36
|
|
|
{ |
37
|
4 |
|
return 'run_open_code_traitor'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $config |
42
|
|
|
* @param ContainerBuilder $container |
43
|
|
|
* @return Extension $this |
44
|
|
|
*/ |
45
|
4 |
|
protected function setInjectionMapAsContainerParameter(array $config, ContainerBuilder $container) |
46
|
|
|
{ |
47
|
4 |
|
$injection = array(); |
48
|
|
|
|
49
|
4 |
|
foreach ($config['inject'] as $trait => $injection) { |
50
|
|
|
$injection[ltrim($trait, '\\')] = $injection; |
51
|
4 |
|
} |
52
|
|
|
|
53
|
4 |
|
if ($config['use_common_traits']) { |
54
|
|
|
|
55
|
2 |
|
$injection = array_merge($injection, array( |
56
|
2 |
|
'Symfony\Component\DependencyInjection\ContainerAwareTrait' => array('setContainer', array('@service_container')), |
57
|
2 |
|
'Psr\Log\LoggerAwareTrait' => array('setLogger', array('@logger')), |
58
|
2 |
|
'RunOpenCode\Bundle\Traitor\Traits\DoctrineAwareTrait' => array('setDoctrine', array('@doctrine')), |
59
|
2 |
|
'RunOpenCode\Bundle\Traitor\Traits\EventDispatcherAwareTrait' => array('setEventDispatcher', array('@event_dispatcher')), |
60
|
2 |
|
'RunOpenCode\Bundle\Traitor\Traits\FilesystemAwareTrait' => array('setFilesystem', array('@filesystem')), |
61
|
2 |
|
'RunOpenCode\Bundle\Traitor\Traits\KernelAwareTrait' => array('setKernel', array('@kernel')), |
62
|
2 |
|
'RunOpenCode\Bundle\Traitor\Traits\MailerAwareInterface' => array('setMailer', array('@mailer')), |
63
|
2 |
|
'RunOpenCode\Bundle\Traitor\Traits\PropertyAccessorAwareTrait' => array('setPropertyAccessor', array('@property_accessor')), |
64
|
2 |
|
'RunOpenCode\Bundle\Traitor\Traits\RequestStackAwareTrait' => array('setRequestStack', array('@request_stack')), |
65
|
2 |
|
'RunOpenCode\Bundle\Traitor\Traits\RouterAwareTrait' => array('setRouter', array('@router')), |
66
|
2 |
|
'RunOpenCode\Bundle\Traitor\Traits\AuthorizationCheckerAwareTrait' => array('setAuthorizationChecker', array('@security.authorization_checker')), |
67
|
2 |
|
'RunOpenCode\Bundle\Traitor\Traits\SessionAwareTrait' => array('setSession', array('@session')), |
68
|
2 |
|
'RunOpenCode\Bundle\Traitor\Traits\TwigAwareTrait' => array('setTwig', array('@twig')), |
69
|
2 |
|
'RunOpenCode\Bundle\Traitor\Traits\TranslatorAwareTrait' => array('setTranslator', array('@translator')), |
70
|
2 |
|
'RunOpenCode\Bundle\Traitor\Traits\ValidatorAwareTrait' => array('setValidator', array('@validator')), |
71
|
2 |
|
'RunOpenCode\Bundle\Traitor\Traits\TokenStorageAwareTrait' => array('setTokenStorage', array('@security.token_storage')) |
72
|
2 |
|
)); |
73
|
2 |
|
} |
74
|
|
|
|
75
|
4 |
|
$container->setParameter('roc.traitor.injection_map', $injection); |
76
|
|
|
|
77
|
4 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $config |
82
|
|
|
* @param ContainerBuilder $container |
83
|
|
|
* @return Extension $this |
84
|
|
|
*/ |
85
|
4 |
|
protected function setFiltersAsContainerParameter(array $config, ContainerBuilder $container) |
86
|
|
|
{ |
87
|
4 |
|
if (isset($config['filters'])) { |
88
|
|
|
|
89
|
2 |
|
if (count($config['filters']['tags']) > 0) { |
90
|
2 |
|
$container->setParameter('roc.traitor.filter.tags', $config['filters']['tags']); |
91
|
2 |
|
} |
92
|
|
|
|
93
|
2 |
View Code Duplication |
if (count($config['filters']['namespaces']) > 0) { |
|
|
|
|
94
|
|
|
|
95
|
|
|
$container->setParameter('roc.traitor.filter.namespaces', array_map(function($namespace) { |
96
|
2 |
|
return rtrim(ltrim($namespace, '\\'), '\\') . '\\'; |
97
|
4 |
|
}, $config['filters']['namespaces'])); |
98
|
2 |
|
} |
99
|
2 |
|
} |
100
|
|
|
|
101
|
4 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array $config |
106
|
|
|
* @param ContainerBuilder $container |
107
|
|
|
* @return Extension $this |
108
|
|
|
*/ |
109
|
4 |
|
protected function setExclusionsAsContainerParameter(array $config, ContainerBuilder $container) |
110
|
|
|
{ |
111
|
4 |
|
if (isset($config['exclude'])) { |
112
|
|
|
|
113
|
|
|
if (count($config['exclude']['services']) > 0) { |
114
|
|
|
$container->setParameter('roc.traitor.exclude.services', $config['exclude']['services']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
View Code Duplication |
if (count($config['exclude']['namespaces']) > 0) { |
|
|
|
|
118
|
|
|
|
119
|
|
|
$container->setParameter('roc.traitor.exclude.namespaces', array_map(function($namespace) { |
120
|
|
|
return rtrim(ltrim($namespace, '\\'), '\\') . '\\'; |
121
|
|
|
}, $config['exclude']['namespaces'])); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (count($config['exclude']['classes']) > 0) { |
125
|
|
|
|
126
|
|
|
$container->setParameter('roc.traitor.exclude.classes', array_map(function($fqcn) { |
127
|
|
|
return ltrim($fqcn, '\\'); |
128
|
|
|
}, $config['exclude']['classes'])); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
4 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
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.