1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the QueryResourcesLoaderBundle, 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\QueryResourcesLoader\DependencyInjection; |
11
|
|
|
|
12
|
|
|
use RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection\Configuration\Configuration; |
13
|
|
|
use Symfony\Component\Config\FileLocator; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
15
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension; |
16
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
17
|
|
|
use Symfony\Component\Config\Resource\FileExistenceResource; |
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Extension |
22
|
|
|
* |
23
|
|
|
* Bundle extension. |
24
|
|
|
* |
25
|
|
|
* @package RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection |
26
|
|
|
*/ |
27
|
|
|
class Extension extends BaseExtension |
28
|
|
|
{ |
29
|
8 |
|
public function getAlias() |
30
|
|
|
{ |
31
|
8 |
|
return 'run_open_code_query_resources_loader'; |
32
|
|
|
} |
33
|
|
|
|
34
|
8 |
|
public function getNamespace() |
35
|
|
|
{ |
36
|
8 |
|
return 'http://www.runopencode.com/xsd-schema/query-resources-loader-bundle'; |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
public function getXsdValidationBasePath() |
40
|
|
|
{ |
41
|
2 |
|
return __DIR__.'/../Resources/config/schema'; |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
6 |
|
public function load(array $configs, ContainerBuilder $container) |
48
|
|
|
{ |
49
|
6 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
50
|
6 |
|
$loader->load('services.xml'); |
51
|
|
|
|
52
|
6 |
|
$configuration = new Configuration(); |
53
|
6 |
|
$config = $this->processConfiguration($configuration, $configs); |
54
|
|
|
|
55
|
|
|
$this |
56
|
6 |
|
->configureTwigGlobals($config, $container) |
57
|
6 |
|
->configureTwigEnvironment($config, $container) |
58
|
6 |
|
->configureTwigWarmUpCommand($config, $container) |
59
|
6 |
|
->configureTwigResourcePaths($config, $container) |
60
|
6 |
|
->configureTwigBundlePaths($config, $container) |
61
|
|
|
; |
62
|
|
|
|
63
|
6 |
|
if (null !== $config['default_executor']) { |
64
|
1 |
|
$container->setParameter('run_open_code.query_resources_loader.default_executor', $config['default_executor']); |
65
|
|
|
} |
66
|
|
|
|
67
|
6 |
|
if (isset($config['twig']['autoescape_service'], $config['twig']['autoescape_service_method'])) { |
68
|
1 |
|
$config['twig']['autoescape'] = array(new Reference($config['twig']['autoescape_service']), $config['twig']['autoescape_service_method']); |
69
|
|
|
} |
70
|
|
|
|
71
|
6 |
|
unset($config['twig']['autoescape_service'], $config['twig']['autoescape_service_method'], $config['twig']['globals']); |
72
|
|
|
|
73
|
6 |
|
$container->getDefinition('run_open_code.query_resources_loader.twig')->replaceArgument(1, $config['twig']); |
74
|
6 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param array $config |
78
|
|
|
* @param ContainerBuilder $container |
79
|
|
|
* @return Extension $this |
80
|
|
|
*/ |
81
|
6 |
|
protected function configureTwigGlobals(array $config, ContainerBuilder $container) |
82
|
|
|
{ |
83
|
6 |
|
if (false !== $container->hasDefinition('run_open_code.query_resources_loader.twig') && !empty($config['twig']['globals'])) { |
84
|
|
|
|
85
|
2 |
|
$definition = $container->getDefinition('run_open_code.query_resources_loader.twig'); |
86
|
|
|
|
87
|
2 |
|
foreach ($config['twig']['globals'] as $key => $global) { |
88
|
|
|
|
89
|
2 |
|
if (isset($global['type']) && 'service' === $global['type']) { |
90
|
1 |
|
$definition->addMethodCall('addGlobal', array($key, new Reference($global['id']))); |
91
|
|
|
} else { |
92
|
2 |
|
$definition->addMethodCall('addGlobal', array($key, $global['value'])); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
6 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $config |
102
|
|
|
* @param ContainerBuilder $container |
103
|
|
|
* @return Extension $this |
104
|
|
|
*/ |
105
|
6 |
|
protected function configureTwigEnvironment(array $config, ContainerBuilder $container) |
106
|
|
|
{ |
107
|
6 |
|
$configurator = $container->getDefinition('run_open_code.query_resources_loader.twig.configurator.environment'); |
108
|
6 |
|
$configurator->replaceArgument(0, $config['twig']['date']['format']); |
109
|
6 |
|
$configurator->replaceArgument(1, $config['twig']['date']['interval_format']); |
110
|
6 |
|
$configurator->replaceArgument(2, $config['twig']['date']['timezone']); |
111
|
6 |
|
$configurator->replaceArgument(3, $config['twig']['number_format']['decimals']); |
112
|
6 |
|
$configurator->replaceArgument(4, $config['twig']['number_format']['decimal_point']); |
113
|
6 |
|
$configurator->replaceArgument(5, $config['twig']['number_format']['thousands_separator']); |
114
|
|
|
|
115
|
6 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param array $config |
120
|
|
|
* @param ContainerBuilder $container |
121
|
|
|
* @return Extension $this |
122
|
|
|
*/ |
123
|
6 |
|
protected function configureTwigWarmUpCommand(array $config, ContainerBuilder $container) |
124
|
|
|
{ |
125
|
6 |
|
$container->getDefinition('run_open_code.query_resources_loader.twig.query_sources_iterator')->replaceArgument(2, $config['twig']['paths']); |
126
|
|
|
|
127
|
6 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param array $config |
132
|
|
|
* @param ContainerBuilder $container |
133
|
|
|
* @return Extension $this |
134
|
|
|
*/ |
135
|
6 |
|
protected function configureTwigResourcePaths(array $config, ContainerBuilder $container) |
136
|
|
|
{ |
137
|
6 |
|
$twigFilesystemLoaderDefinition = $container->getDefinition('run_open_code.query_resources_loader.twig.loader.filesystem'); |
138
|
|
|
|
139
|
|
|
// register user-configured paths |
140
|
6 |
|
foreach ($config['twig']['paths'] as $path => $namespace) { |
141
|
2 |
|
if (!$namespace) { |
142
|
2 |
|
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path)); |
143
|
|
|
} else { |
144
|
2 |
|
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($path, $namespace)); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
6 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param array $config |
153
|
|
|
* @param ContainerBuilder $container |
154
|
|
|
* @return Extension $this |
155
|
|
|
*/ |
156
|
6 |
|
protected function configureTwigBundlePaths(array $config, ContainerBuilder $container) |
|
|
|
|
157
|
|
|
{ |
158
|
6 |
|
$twigFilesystemLoaderDefinition = $container->getDefinition('run_open_code.query_resources_loader.twig.loader.filesystem'); |
159
|
|
|
|
160
|
6 |
|
$addTwigPath = function($dir, $bundle) use ($twigFilesystemLoaderDefinition) { |
161
|
|
|
|
162
|
|
|
$name = $bundle; |
163
|
|
|
|
164
|
|
View Code Duplication |
if ('Bundle' === substr($name, -6)) { |
|
|
|
|
165
|
|
|
$name = substr($name, 0, -6); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir, $name)); |
169
|
6 |
|
}; |
170
|
|
|
|
171
|
|
|
// register bundles as Twig namespaces |
172
|
6 |
|
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) { |
173
|
|
|
|
174
|
|
|
$dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/query'; |
175
|
|
|
|
176
|
|
|
if (is_dir($dir)) { |
177
|
|
|
$addTwigPath($dir, $bundle); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$container->addResource(new FileExistenceResource($dir)); |
181
|
|
|
|
182
|
|
|
$reflection = new \ReflectionClass($class); |
183
|
|
|
$dir = dirname($reflection->getFileName()).'/Resources/query'; |
184
|
|
|
|
185
|
|
|
if (is_dir($dir)) { |
186
|
|
|
$addTwigPath($dir, $bundle); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$container->addResource(new FileExistenceResource($dir)); |
190
|
|
|
} |
191
|
|
|
|
192
|
6 |
|
return $this; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
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.