[HttpKernel] changed Kernel::locateResource() to also work with directories

This commit is contained in:
Fabien Potencier 2011-01-21 16:40:04 +01:00
parent 645528c9c6
commit bd6bc4db62
2 changed files with 21 additions and 2 deletions

View File

@ -259,6 +259,8 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
/**
* Returns the file path for a given resource.
*
* A Resource can be a file or a directory.
*
* The resource name must follow the following pattern:
*
* @BundleName/path/to/a/file.something
@ -296,7 +298,7 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
$isResource = 0 === strpos($path, 'Resources');
$files = array();
if (true === $isResource && null !== $dir && is_file($file = $dir.'/'.$bundle.'/'.substr($path, 10))) {
if (true === $isResource && null !== $dir && file_exists($file = $dir.'/'.$bundle.'/'.substr($path, 10))) {
if ($first) {
return $file;
}
@ -305,7 +307,7 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
}
foreach ($this->getBundle($bundle, false) as $bundle) {
if (is_file($file = $bundle->getPath().'/'.$path)) {
if (file_exists($file = $bundle->getPath().'/'.$path)) {
if ($first) {
return $file;
}

View File

@ -130,6 +130,23 @@ class KernelTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(__DIR__.'/Fixtures/foo/foo.txt', __DIR__.'/Fixtures/Bundle1/Resources/foo.txt'), $kernel->locateResource('@foo/Resources/foo.txt', __DIR__.'/Fixtures', false));
}
public function testLocateResourceOnDirectories()
{
$kernel = $this->getKernel();
$this->assertEquals(__DIR__.'/Fixtures/foo/', $kernel->locateResource('@foo/Resources/', __DIR__.'/Fixtures'));
$this->assertEquals(__DIR__.'/Fixtures/foo/', $kernel->locateResource('@foo/Resources', __DIR__.'/Fixtures'));
$kernel
->expects($this->any())
->method('getBundle')
->will($this->returnValue(array($this->getBundle(__DIR__.'/Fixtures/Bundle1'))))
;
$this->assertEquals(__DIR__.'/Fixtures/Bundle1/Resources/', $kernel->locateResource('@foo/Resources/'));
$this->assertEquals(__DIR__.'/Fixtures/Bundle1/Resources/', $kernel->locateResource('@foo/Resources/'));
}
public function testInitializeBundles()
{
$parent = $this->getBundle(null, '', 'ParentABundle');