1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the TraitorBundle, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2017 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
|
2 |
|
public function getAlias() |
21
|
|
|
{ |
22
|
2 |
|
return 'runopencode_traitor'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
2 |
|
public function getNamespace() |
29
|
|
|
{ |
30
|
2 |
|
return 'http://www.runopencode.com/xsd-schema/traitor-bundle'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function getXsdValidationBasePath() |
37
|
|
|
{ |
38
|
|
|
return __DIR__.'/../Resources/config/schema'; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
2 |
|
public function load(array $configs, ContainerBuilder $container) |
45
|
|
|
{ |
46
|
2 |
|
$configuration = new Configuration(); |
47
|
2 |
|
$config = $this->processConfiguration($configuration, $configs); |
48
|
|
|
|
49
|
|
|
$this |
50
|
2 |
|
->setInjectionMapAsContainerParameter($config, $container) |
51
|
2 |
|
->setFiltersAsContainerParameter($config, $container) |
52
|
2 |
|
->setExclusionsAsContainerParameter($config, $container) |
53
|
|
|
; |
54
|
2 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $config |
58
|
|
|
* @param ContainerBuilder $container |
59
|
|
|
* @return Extension $this |
60
|
|
|
*/ |
61
|
2 |
|
protected function setInjectionMapAsContainerParameter(array $config, ContainerBuilder $container) |
62
|
|
|
{ |
63
|
2 |
|
$injection = array(); |
64
|
|
|
|
65
|
2 |
|
foreach ($config['inject'] as $trait => $injection) { |
66
|
|
|
$injection[ltrim($trait, '\\')] = $injection; |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
if ($config['use_common_traits']) { |
70
|
|
|
|
71
|
1 |
|
$injection = array_merge($injection, array( |
72
|
1 |
|
'Symfony\Component\DependencyInjection\ContainerAwareTrait' => array('setContainer', array('@service_container')), |
73
|
|
|
'Psr\Log\LoggerAwareTrait' => array('setLogger', array('@logger')), |
74
|
|
|
'RunOpenCode\Bundle\Traitor\Traits\DoctrineAwareTrait' => array('setDoctrine', array('@doctrine')), |
75
|
|
|
'RunOpenCode\Bundle\Traitor\Traits\EventDispatcherAwareTrait' => array('setEventDispatcher', array('@event_dispatcher')), |
76
|
|
|
'RunOpenCode\Bundle\Traitor\Traits\FilesystemAwareTrait' => array('setFilesystem', array('@filesystem')), |
77
|
|
|
'RunOpenCode\Bundle\Traitor\Traits\KernelAwareTrait' => array('setKernel', array('@kernel')), |
78
|
|
|
'RunOpenCode\Bundle\Traitor\Traits\MailerAwareInterface' => array('setMailer', array('@mailer')), |
79
|
|
|
'RunOpenCode\Bundle\Traitor\Traits\PropertyAccessorAwareTrait' => array('setPropertyAccessor', array('@property_accessor')), |
80
|
|
|
'RunOpenCode\Bundle\Traitor\Traits\RequestStackAwareTrait' => array('setRequestStack', array('@request_stack')), |
81
|
|
|
'RunOpenCode\Bundle\Traitor\Traits\RouterAwareTrait' => array('setRouter', array('@router')), |
82
|
|
|
'RunOpenCode\Bundle\Traitor\Traits\AuthorizationCheckerAwareTrait' => array('setAuthorizationChecker', array('@security.authorization_checker')), |
83
|
|
|
'RunOpenCode\Bundle\Traitor\Traits\SessionAwareTrait' => array('setSession', array('@session')), |
84
|
|
|
'RunOpenCode\Bundle\Traitor\Traits\TwigAwareTrait' => array('setTwig', array('@twig')), |
85
|
|
|
'RunOpenCode\Bundle\Traitor\Traits\TranslatorAwareTrait' => array('setTranslator', array('@translator')), |
86
|
|
|
'RunOpenCode\Bundle\Traitor\Traits\ValidatorAwareTrait' => array('setValidator', array('@validator')), |
87
|
|
|
'RunOpenCode\Bundle\Traitor\Traits\TokenStorageAwareTrait' => array('setTokenStorage', array('@security.token_storage')) |
88
|
|
|
)); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
$container->setParameter('runopencode.traitor.injection_map', $injection); |
92
|
|
|
|
93
|
2 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param array $config |
98
|
|
|
* @param ContainerBuilder $container |
99
|
|
|
* @return Extension $this |
100
|
|
|
*/ |
101
|
2 |
|
protected function setFiltersAsContainerParameter(array $config, ContainerBuilder $container) |
102
|
|
|
{ |
103
|
2 |
|
if (isset($config['filters'])) { |
104
|
|
|
|
105
|
1 |
|
if (count($config['filters']['tags']) > 0) { |
106
|
1 |
|
$container->setParameter('runopencode.traitor.filter.tags', $config['filters']['tags']); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
View Code Duplication |
if (count($config['filters']['namespaces']) > 0) { |
|
|
|
|
110
|
|
|
|
111
|
|
|
$container->setParameter('runopencode.traitor.filter.namespaces', array_map(function($namespace) { |
112
|
1 |
|
return rtrim(ltrim($namespace, '\\'), '\\') . '\\'; |
113
|
1 |
|
}, $config['filters']['namespaces'])); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $config |
122
|
|
|
* @param ContainerBuilder $container |
123
|
|
|
* @return Extension $this |
124
|
|
|
*/ |
125
|
2 |
|
protected function setExclusionsAsContainerParameter(array $config, ContainerBuilder $container) |
126
|
|
|
{ |
127
|
2 |
|
if (isset($config['exclude'])) { |
128
|
|
|
|
129
|
|
|
if (count($config['exclude']['services']) > 0) { |
130
|
|
|
$container->setParameter('runopencode.traitor.exclude.services', $config['exclude']['services']); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
View Code Duplication |
if (count($config['exclude']['namespaces']) > 0) { |
|
|
|
|
134
|
|
|
|
135
|
|
|
$container->setParameter('runopencode.traitor.exclude.namespaces', array_map(function($namespace) { |
136
|
|
|
return rtrim(ltrim($namespace, '\\'), '\\') . '\\'; |
137
|
|
|
}, $config['exclude']['namespaces'])); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (count($config['exclude']['classes']) > 0) { |
141
|
|
|
|
142
|
|
|
$container->setParameter('runopencode.traitor.exclude.classes', array_map(function($fqcn) { |
143
|
|
|
return ltrim($fqcn, '\\'); |
144
|
|
|
}, $config['exclude']['classes'])); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
2 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.