|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Doctrine Naming Strategy Bundle, 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\DoctrineNamingStrategy\DependencyInjection; |
|
11
|
|
|
|
|
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
15
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension as BaseExtension; |
|
16
|
|
|
use Symfony\Component\Config\FileLocator; |
|
17
|
|
|
|
|
18
|
|
|
class Extension extends BaseExtension |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* {@inheritdoc} |
|
22
|
|
|
*/ |
|
23
|
3 |
|
public function getAlias() |
|
24
|
|
|
{ |
|
25
|
3 |
|
return "runopencode_doctrine_naming_strategy"; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
3 |
|
public function getNamespace() |
|
33
|
|
|
{ |
|
34
|
3 |
|
return 'http://www.runopencode.com/xsd-schema/doctrine-naming-strategy-bundle'; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getXsdValidationBasePath() |
|
41
|
|
|
{ |
|
42
|
|
|
return __DIR__.'/../Resources/config/schema'; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
3 |
|
public function load(array $config, ContainerBuilder $container) |
|
49
|
|
|
{ |
|
50
|
3 |
|
$configuration = new Configuration(); |
|
51
|
3 |
|
$config = $this->processConfiguration($configuration, $config); |
|
52
|
|
|
|
|
53
|
3 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
54
|
3 |
|
$loader->load('services.xml'); |
|
55
|
|
|
|
|
56
|
3 |
|
$this->configureUnderscoredBundlePrefixNamer($container, $config); |
|
57
|
3 |
|
$this->configureUnderscoredClassNamespacePrefixNamer($container, $config); |
|
58
|
3 |
|
$this->configureNamerCollection($container, $config); |
|
59
|
3 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Configure 'runopencode.doctrine.orm.naming_strategy.underscored_bundle_prefix' naming strategy. |
|
63
|
|
|
* |
|
64
|
|
|
* @param ContainerBuilder $container |
|
65
|
|
|
* @param array $config |
|
66
|
|
|
* @return Extension $this |
|
67
|
|
|
*/ |
|
68
|
3 |
View Code Duplication |
private function configureUnderscoredBundlePrefixNamer(ContainerBuilder $container, array $config) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
if ( |
|
71
|
3 |
|
$container->hasDefinition('runopencode.doctrine.orm.naming_strategy.underscored_bundle_prefix') |
|
72
|
|
|
&& |
|
73
|
3 |
|
isset($config['underscored_bundle_prefix']) |
|
74
|
|
|
) { |
|
75
|
1 |
|
$definition = $container->getDefinition('runopencode.doctrine.orm.naming_strategy.underscored_bundle_prefix'); |
|
76
|
|
|
|
|
77
|
1 |
|
if ('uppercase' === $config['underscored_bundle_prefix']['case']) { |
|
78
|
|
|
$config['underscored_bundle_prefix']['case'] = CASE_UPPER; |
|
79
|
|
|
} else { |
|
80
|
1 |
|
$config['underscored_bundle_prefix']['case'] = CASE_LOWER; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
$args = $definition->getArguments(); |
|
84
|
1 |
|
$args[1] = $config['underscored_bundle_prefix']; |
|
85
|
|
|
|
|
86
|
1 |
|
$definition->setArguments($args); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Configure 'runopencode.doctrine.orm.naming_strategy.underscored_class_namespace_prefix' naming strategy. |
|
94
|
|
|
* |
|
95
|
|
|
* @param ContainerBuilder $container |
|
96
|
|
|
* @param array $config |
|
97
|
|
|
* @return Extension $this |
|
98
|
|
|
*/ |
|
99
|
3 |
View Code Duplication |
private function configureUnderscoredClassNamespacePrefixNamer(ContainerBuilder $container, array $config) |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
|
|
if ( |
|
102
|
3 |
|
$container->hasDefinition('runopencode.doctrine.orm.naming_strategy.underscored_class_namespace_prefix') |
|
103
|
|
|
&& |
|
104
|
3 |
|
isset($config['underscored_class_namespace_prefix']) |
|
105
|
|
|
) { |
|
106
|
1 |
|
$definition = $container->getDefinition('runopencode.doctrine.orm.naming_strategy.underscored_class_namespace_prefix'); |
|
107
|
|
|
|
|
108
|
1 |
|
if ('uppercase' === $config['underscored_class_namespace_prefix']['case']) { |
|
109
|
|
|
$config['underscored_class_namespace_prefix']['case'] = CASE_UPPER; |
|
110
|
|
|
} else { |
|
111
|
1 |
|
$config['underscored_class_namespace_prefix']['case'] = CASE_LOWER; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
$args = $definition->getArguments(); |
|
115
|
1 |
|
$args[0] = $config['underscored_class_namespace_prefix']; |
|
116
|
|
|
|
|
117
|
1 |
|
$definition->setArguments($args); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
3 |
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Configure 'runopencode.doctrine.orm.naming_strategy.underscored_namer_collection' naming strategy. |
|
125
|
|
|
* |
|
126
|
|
|
* @param ContainerBuilder $container |
|
127
|
|
|
* @param array $config |
|
128
|
|
|
* @return Extension $this |
|
129
|
|
|
*/ |
|
130
|
3 |
|
private function configureNamerCollection(ContainerBuilder $container, array $config) |
|
131
|
|
|
{ |
|
132
|
|
|
if ( |
|
133
|
3 |
|
$container->hasDefinition('runopencode.doctrine.orm.naming_strategy.underscored_namer_collection') |
|
134
|
|
|
&& |
|
135
|
3 |
|
isset($config['underscored_namer_collection']) |
|
136
|
|
|
) { |
|
137
|
1 |
|
$definition = $container->getDefinition('runopencode.doctrine.orm.naming_strategy.underscored_namer_collection'); |
|
138
|
|
|
|
|
139
|
1 |
|
$definition->setArguments(array( |
|
140
|
1 |
|
new Reference($config['underscored_namer_collection']['default']), |
|
141
|
1 |
|
array_map(function($namerId) { |
|
142
|
1 |
|
return new Reference($namerId); |
|
143
|
1 |
|
}, $config['underscored_namer_collection']['namers']), |
|
144
|
|
|
array( |
|
145
|
1 |
|
'joinTableFieldSuffix' => $config['underscored_namer_collection']['joinTableFieldSuffix'] |
|
146
|
|
|
) |
|
147
|
|
|
)); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
3 |
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.