Skip to content

Commit 9bcd496

Browse files
committed
Merge remote branch 'vicb/resource_path'
* vicb/resource_path: Update for Bundle names long again (= include the 'Bundle' suffix) [Kernel] Make locateResource() throws an exception when a resource from a Bundle hides a custom resource [Kernel] Make resources overriding consistent across bundle directories and resource directories [Kernel] Improve test semantic [Kernel] Update tests with shorter bundle names [Kernel] Restore the tests for the locateResource method Resource paths should use the full bundle name (with the 'Bundle' postfix)
2 parents 68acb02 + 5317c96 commit 9bcd496

File tree

14 files changed

+248
-18
lines changed

14 files changed

+248
-18
lines changed

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,17 @@ public function getBundle($name, $first = true)
204204
*
205205
* The resource name must follow the following pattern:
206206
*
207-
* @BundleName/path/to/a/file.something
207+
* @<BundleName>/path/to/a/file.something
208208
*
209209
* where BundleName is the name of the bundle
210210
* and the remaining part is the relative path in the bundle.
211211
*
212-
* If $dir is passed, and the first segment of the path is Resources,
212+
* If $dir is passed, and the first segment of the path is "Resources",
213213
* this method will look for a file named:
214214
*
215-
* $dir/BundleName/path/without/Resources
215+
* $dir/<BundleName>/path/without/Resources
216+
*
217+
* before looking in the bundle resource folder.
216218
*
217219
* @param string $name A resource name to locate
218220
* @param string $dir A directory where to look for the resource first
@@ -221,7 +223,8 @@ public function getBundle($name, $first = true)
221223
* @return string|array The absolute path of the resource or an array if $first is false
222224
*
223225
* @throws \InvalidArgumentException if the file cannot be found or the name is not valid
224-
* @throws \RuntimeException if the name contains invalid/unsafe characters
226+
* @throws \RuntimeException if the name contains invalid/unsafe
227+
* @throws \RuntimeException if a custom resource is hidden by a resource in a derived bundle
225228
*/
226229
public function locateResource($name, $dir = null, $first = true)
227230
{
@@ -234,30 +237,41 @@ public function locateResource($name, $dir = null, $first = true)
234237
}
235238

236239
$name = substr($name, 1);
237-
list($bundle, $path) = explode('/', $name, 2);
238-
239-
$isResource = 0 === strpos($path, 'Resources');
240+
list($bundleName, $path) = explode('/', $name, 2);
241+
242+
$isResource = 0 === strpos($path, 'Resources') && null !== $dir;
243+
$overridePath = substr($path, 9);
244+
$resourceBundle = null;
245+
$bundles = $this->getBundle($bundleName, false);
246+
247+
foreach ($bundles as $bundle) {
248+
if ($isResource && file_exists($file = $dir.'/'.$bundle->getName().$overridePath)) {
249+
if (null !== $resourceBundle) {
250+
throw new \RuntimeException(sprintf('"%s" resource is hidden by a resource from the "%s" derived bundle. ' .
251+
'Create a "%s" file to override the bundle resource.',
252+
$file,
253+
$resourceBundle,
254+
$dir.'/'.$bundles[0]->getName().$overridePath
255+
));
256+
}
240257

241-
$files = array();
242-
if (true === $isResource && null !== $dir && file_exists($file = $dir.'/'.$bundle.'/'.substr($path, 10))) {
243-
if ($first) {
244-
return $file;
258+
if ($first) {
259+
return $file;
260+
}
261+
$files[] = $file;
245262
}
246263

247-
$files[] = $file;
248-
}
249-
250-
foreach ($this->getBundle($bundle, false) as $bundle) {
251264
if (file_exists($file = $bundle->getPath().'/'.$path)) {
252-
if ($first) {
265+
if ($first && !$isResource) {
253266
return $file;
254267
}
255268
$files[] = $file;
269+
$resourceBundle = $bundle->getName();
256270
}
257271
}
258272

259-
if ($files) {
260-
return $files;
273+
if (count($files) > 0) {
274+
return $first && $isResource ? $files[0] : $files;
261275
}
262276

263277
throw new \InvalidArgumentException(sprintf('Unable to find file "@%s".', $name));

tests/Symfony/Tests/Component/HttpKernel/Fixtures/ChildBundle/Resources/foo.txt

Whitespace-only changes.

tests/Symfony/Tests/Component/HttpKernel/Fixtures/ChildBundle/Resources/hide.txt

Whitespace-only changes.

tests/Symfony/Tests/Component/HttpKernel/Fixtures/Resources/BaseBundle/hide.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)