1 | <?php |
||
11 | class Configuration implements ConfigurationInterface |
||
12 | { |
||
13 | const DEFAULT_PREFIX = 'assets'; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $kernelRootDir; |
||
19 | |||
20 | /** |
||
21 | * @param string $kernelRootDir |
||
22 | */ |
||
23 | 35 | public function __construct($kernelRootDir) |
|
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | 35 | public function getConfigTreeBuilder() |
|
32 | { |
||
33 | 35 | $self = $this; |
|
34 | |||
35 | 35 | return $this->createRoot('rj_frontend', 'array') |
|
|
|||
36 | 35 | ->children() |
|
37 | 35 | ->booleanNode('override_default_package')->defaultTrue()->end() |
|
38 | 35 | ->arrayNode('fallback_patterns') |
|
39 | 35 | ->prototype('scalar')->end() |
|
40 | 35 | ->defaultValue(['.*bundles\/.*']) |
|
41 | 35 | ->end() |
|
42 | 35 | ->append($this->addLivereloadSection()) |
|
43 | 35 | ->append($this->addPackagePrefixSection(self::DEFAULT_PREFIX)) |
|
44 | 35 | ->append($this->addPackageManifestSection()) |
|
45 | 35 | ->arrayNode('packages') |
|
46 | 35 | ->useAttributeAsKey('name') |
|
47 | 35 | ->prototype('array') |
|
48 | 35 | ->children() |
|
49 | 35 | ->append($this->addPackagePrefixSection()) |
|
50 | 35 | ->append($this->addPackageManifestSection()) |
|
51 | 35 | ->end() |
|
52 | 35 | ->beforeNormalization() |
|
53 | 35 | ->ifTrue(function ($config) use ($self) { |
|
54 | 19 | return $self->mustApplyManifestDefaultPath($config); |
|
55 | 35 | }) |
|
56 | 35 | ->then(function ($config) use ($self) { |
|
57 | 1 | return $self->applyManifestDefaultPath($config); |
|
58 | 35 | }) |
|
59 | 35 | ->end() |
|
60 | 35 | ->end() |
|
61 | 35 | ->validate() |
|
62 | 35 | ->ifTrue(function ($config) { |
|
63 | 16 | return in_array('default', array_keys($config)); |
|
64 | 35 | }) |
|
65 | 35 | ->thenInvalid("'default' is a reserved package name") |
|
66 | 35 | ->end() |
|
67 | 35 | ->end() |
|
68 | 35 | ->end() |
|
69 | 35 | ->beforeNormalization() |
|
70 | 35 | ->ifTrue(function ($config) use ($self) { |
|
71 | 35 | return $self->mustApplyManifestDefaultPath($config); |
|
72 | 35 | }) |
|
73 | 35 | ->then(function ($config) use ($self) { |
|
74 | 3 | return $self->applyManifestDefaultPath($config); |
|
75 | 35 | }) |
|
76 | 35 | ->end() |
|
77 | 35 | ->end(); |
|
78 | } |
||
79 | |||
80 | 35 | private function addLivereloadSection() |
|
91 | |||
92 | 35 | private function addPackagePrefixSection($defaultValue = null) |
|
93 | { |
||
94 | 35 | $node = $this->createRoot('prefix') |
|
95 | 35 | ->prototype('scalar')->end() |
|
96 | 35 | ->defaultValue([$defaultValue]) |
|
97 | 35 | ->requiresAtLeastOneElement() |
|
98 | 35 | ->beforeNormalization() |
|
99 | 35 | ->ifString() |
|
100 | ->then(function ($v) { return [$v]; }) |
||
101 | 35 | ->end() |
|
102 | 35 | ->validate() |
|
103 | 35 | ->ifTrue(function ($prefixes) { |
|
104 | 23 | return Util::containsUrl($prefixes) |
|
105 | 23 | && Util::containsNotUrl($prefixes); |
|
106 | 35 | }) |
|
107 | 35 | ->thenInvalid('Packages cannot have both URL and path prefixes') |
|
108 | 35 | ->end() |
|
109 | 35 | ->validate() |
|
110 | 35 | ->ifTrue(function ($prefixes) { |
|
111 | 22 | return count($prefixes) > 1 |
|
112 | 22 | && Util::containsNotUrl($prefixes); |
|
113 | 35 | }) |
|
114 | 35 | ->thenInvalid('Packages can only have one path prefix') |
|
115 | 35 | ->end() |
|
116 | ; |
||
117 | |||
118 | 35 | return $defaultValue === null |
|
119 | 35 | ? $node->isRequired() |
|
120 | 35 | : $node |
|
121 | ; |
||
122 | } |
||
123 | |||
124 | 35 | private function addPackageManifestSection() |
|
145 | |||
146 | /** |
||
147 | * Returns true if the manifest's path has not been defined AND: |
||
148 | * - a prefix has not been defined |
||
149 | * - OR if a prefix has been defined, it's not a URL. |
||
150 | * |
||
151 | * Note that the manifest's configuration can be a string, in which case it |
||
152 | * represents the path to the manifest file. |
||
153 | * |
||
154 | * This method is public because of the inability to use $this in closures |
||
155 | * in PHP 5.3. |
||
156 | * |
||
157 | * @param array $config |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | 35 | public function mustApplyManifestDefaultPath(array $config) |
|
169 | |||
170 | /** |
||
171 | * Apply a default manifest path computed from the defined prefix. |
||
172 | * |
||
173 | * After calling this method, the manifest's path will be |
||
174 | * %kernel.root_dir%/../web/$prefix/manifest.json, where $prefix is the |
||
175 | * configured prefix. |
||
176 | * |
||
177 | * Note that this method is used for both the default package's config and |
||
178 | * for each custom package's config. |
||
179 | * |
||
180 | * This method is public because of the inability to use $this in closures |
||
181 | * in PHP 5.3 |
||
182 | * |
||
183 | * @param array $config |
||
184 | * |
||
185 | * @return array |
||
186 | */ |
||
187 | 4 | public function applyManifestDefaultPath(array $config) |
|
209 | |||
210 | /** |
||
211 | * @param string $root |
||
212 | * @param string|null $type |
||
213 | * |
||
214 | * @return ArrayNodeDefinition|NodeDefinition |
||
215 | */ |
||
216 | 35 | private function createRoot($root, $type = null) |
|
226 | } |
||
227 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: