1 | <?php |
||
9 | class Configuration implements ConfigurationInterface |
||
10 | { |
||
11 | const DEFAULT_PREFIX = 'assets'; |
||
12 | |||
13 | private $kernelRootDir; |
||
14 | |||
15 | 12 | public function __construct($kernelRootDir) |
|
19 | |||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | 12 | public function getConfigTreeBuilder() |
|
24 | { |
||
25 | 12 | $self = $this; |
|
26 | |||
27 | 12 | return $this->createRoot('rj_frontend', 'array') |
|
|
|||
28 | 12 | ->children() |
|
29 | 12 | ->booleanNode('override_default_package')->defaultTrue()->end() |
|
30 | 12 | ->arrayNode('fallback_patterns') |
|
31 | 12 | ->prototype('scalar')->end() |
|
32 | 12 | ->defaultValue(array('.*bundles\/.*')) |
|
33 | 12 | ->end() |
|
34 | 12 | ->append($this->addLivereloadSection()) |
|
35 | 12 | ->append($this->addPackagePrefixSection(self::DEFAULT_PREFIX)) |
|
36 | 12 | ->append($this->addPackageManifestSection()) |
|
37 | 12 | ->arrayNode('packages') |
|
38 | 12 | ->useAttributeAsKey('name') |
|
39 | 12 | ->prototype('array') |
|
40 | 12 | ->children() |
|
41 | 12 | ->append($this->addPackagePrefixSection()) |
|
42 | 12 | ->append($this->addPackageManifestSection()) |
|
43 | 12 | ->end() |
|
44 | 12 | ->beforeNormalization() |
|
45 | ->ifTrue(function ($config) use ($self) { |
||
46 | 5 | return $self->mustApplyManifestDefaultPath($config); |
|
47 | 12 | }) |
|
48 | ->then(function ($config) use ($self) { |
||
49 | return $self->applyManifestDefaultPath($config); |
||
50 | 12 | }) |
|
51 | 12 | ->end() |
|
52 | 12 | ->end() |
|
53 | 12 | ->validate() |
|
54 | ->ifTrue(function ($config) { |
||
55 | 5 | return in_array('default', array_keys($config)); |
|
56 | 12 | }) |
|
57 | 12 | ->thenInvalid("'default' is a reserved package name") |
|
58 | 12 | ->end() |
|
59 | 12 | ->end() |
|
60 | 12 | ->end() |
|
61 | 12 | ->beforeNormalization() |
|
62 | ->ifTrue(function ($config) use ($self) { |
||
63 | 12 | return $self->mustApplyManifestDefaultPath($config); |
|
64 | 12 | }) |
|
65 | ->then(function ($config) use ($self) { |
||
66 | 1 | return $self->applyManifestDefaultPath($config); |
|
67 | 12 | }) |
|
68 | 12 | ->end() |
|
69 | 12 | ->end(); |
|
70 | } |
||
71 | |||
72 | 12 | private function addLivereloadSection() |
|
83 | |||
84 | 12 | private function addPackagePrefixSection($defaultValue = null) |
|
115 | |||
116 | 12 | private function addPackageManifestSection() |
|
137 | |||
138 | /** |
||
139 | * Returns true if the manifest's path has not been defined AND: |
||
140 | * - a prefix has not been defined |
||
141 | * - OR if a prefix has been defined, it's not a URL |
||
142 | * |
||
143 | * Note that the manifest's configuration can be a string, in which case it |
||
144 | * represents the path to the manifest file. |
||
145 | * |
||
146 | * This method is public because of the inability to use $this in closures |
||
147 | * in PHP 5.3. |
||
148 | * |
||
149 | * @param array $config |
||
150 | * @return boolean |
||
151 | */ |
||
152 | 12 | public function mustApplyManifestDefaultPath($config) |
|
160 | |||
161 | /** |
||
162 | * Apply a default manifest path computed from the defined prefix. |
||
163 | * |
||
164 | * After calling this method, the manifest's path will be |
||
165 | * %kernel.root_dir%/../web/$prefix/manifest.json, where $prefix is the |
||
166 | * configured prefix. |
||
167 | * |
||
168 | * Note that this method is used for both the default package's config and |
||
169 | * for each custom package's config. |
||
170 | * |
||
171 | * This method is public because of the inability to use $this in closures |
||
172 | * in PHP 5.3 |
||
173 | * |
||
174 | * @param array $config |
||
175 | * @return array |
||
176 | */ |
||
177 | 1 | public function applyManifestDefaultPath($config) |
|
178 | { |
||
179 | 1 | $prefix = isset($config['prefix']) ? $config['prefix'] : self::DEFAULT_PREFIX; |
|
180 | |||
181 | 1 | if (is_array($prefix)) { |
|
182 | $prefix = $prefix[0]; |
||
183 | } |
||
184 | |||
185 | 1 | if (!is_array($config['manifest'])) { |
|
186 | $config['manifest'] = array('enabled' => true); |
||
187 | } |
||
188 | |||
189 | 1 | $config['manifest']['path'] = implode('/', array( |
|
190 | 1 | $this->kernelRootDir, |
|
191 | 1 | '..', |
|
192 | 1 | 'web', |
|
193 | 1 | $prefix, |
|
194 | 1 | 'manifest.json', |
|
195 | )); |
||
196 | |||
197 | 1 | return $config; |
|
198 | } |
||
199 | |||
200 | 12 | private function createRoot($root, $type = null) |
|
210 | } |
||
211 |
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: