[Config] fix filelocator with empty name

This commit is contained in:
Tobias Schultze 2014-03-14 22:17:30 +01:00
parent fb31e47949
commit ddd2fe2032
13 changed files with 85 additions and 83 deletions

View File

@ -31,18 +31,14 @@ class FileLocator implements FileLocatorInterface
} }
/** /**
* Returns a full path for a given file name. * {@inheritdoc}
*
* @param mixed $name The file name to locate
* @param string $currentPath The current path
* @param bool $first Whether to return the first occurrence or an array of filenames
*
* @return string|array The full path to the file|An array of file paths
*
* @throws \InvalidArgumentException When file is not found
*/ */
public function locate($name, $currentPath = null, $first = true) public function locate($name, $currentPath = null, $first = true)
{ {
if ('' == $name) {
throw new \InvalidArgumentException('An empty file name is not valid to be located.');
}
if ($this->isAbsolutePath($name)) { if ($this->isAbsolutePath($name)) {
if (!file_exists($name)) { if (!file_exists($name)) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $name)); throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $name));
@ -84,10 +80,10 @@ class FileLocator implements FileLocatorInterface
*/ */
private function isAbsolutePath($file) private function isAbsolutePath($file)
{ {
if ($file[0] == '/' || $file[0] == '\\' if ($file[0] === '/' || $file[0] === '\\'
|| (strlen($file) > 3 && ctype_alpha($file[0]) || (strlen($file) > 3 && ctype_alpha($file[0])
&& $file[1] == ':' && $file[1] === ':'
&& ($file[2] == '\\' || $file[2] == '/') && ($file[2] === '\\' || $file[2] === '/')
) )
|| null !== parse_url($file, PHP_URL_SCHEME) || null !== parse_url($file, PHP_URL_SCHEME)
) { ) {

View File

@ -19,11 +19,11 @@ interface FileLocatorInterface
/** /**
* Returns a full path for a given file name. * Returns a full path for a given file name.
* *
* @param mixed $name The file name to locate * @param string $name The file name to locate
* @param string $currentPath The current path * @param string|null $currentPath The current path
* @param bool $first Whether to return the first occurrence or an array of filenames * @param bool $first Whether to return the first occurrence or an array of filenames
* *
* @return string|array The full path to the file|An array of file paths * @return string|array The full path to the file or an array of file paths
* *
* @throws \InvalidArgumentException When file is not found * @throws \InvalidArgumentException When file is not found
*/ */

View File

@ -34,14 +34,7 @@ class DelegatingLoader extends Loader
} }
/** /**
* Loads a resource. * {@inheritdoc}
*
* @param mixed $resource A resource
* @param string $type The resource type
*
* @return mixed
*
* @throws FileLoaderLoadException if no loader is found.
*/ */
public function load($resource, $type = null) public function load($resource, $type = null)
{ {

View File

@ -22,8 +22,14 @@ use Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceExceptio
*/ */
abstract class FileLoader extends Loader abstract class FileLoader extends Loader
{ {
/**
* @var array
*/
protected static $loading = array(); protected static $loading = array();
/**
* @var FileLocatorInterface
*/
protected $locator; protected $locator;
private $currentDir; private $currentDir;
@ -38,11 +44,21 @@ abstract class FileLoader extends Loader
$this->locator = $locator; $this->locator = $locator;
} }
/**
* Sets the current directory.
*
* @param string $dir
*/
public function setCurrentDir($dir) public function setCurrentDir($dir)
{ {
$this->currentDir = $dir; $this->currentDir = $dir;
} }
/**
* Returns the file locator used by this loader.
*
* @return FileLocatorInterface
*/
public function getLocator() public function getLocator()
{ {
return $this->locator; return $this->locator;
@ -51,10 +67,10 @@ abstract class FileLoader extends Loader
/** /**
* Imports a resource. * Imports a resource.
* *
* @param mixed $resource A Resource * @param mixed $resource A Resource
* @param string $type The resource type * @param string|null $type The resource type or null if unknown
* @param bool $ignoreErrors Whether to ignore import errors or not * @param bool $ignoreErrors Whether to ignore import errors or not
* @param string $sourceResource The original resource importing the new resource * @param string|null $sourceResource The original resource importing the new resource
* *
* @return mixed * @return mixed
* *

View File

@ -23,9 +23,7 @@ abstract class Loader implements LoaderInterface
protected $resolver; protected $resolver;
/** /**
* Gets the loader resolver. * {@inheritdoc}
*
* @return LoaderResolverInterface A LoaderResolverInterface instance
*/ */
public function getResolver() public function getResolver()
{ {
@ -33,9 +31,7 @@ abstract class Loader implements LoaderInterface
} }
/** /**
* Sets the loader resolver. * {@inheritdoc}
*
* @param LoaderResolverInterface $resolver A LoaderResolverInterface instance
*/ */
public function setResolver(LoaderResolverInterface $resolver) public function setResolver(LoaderResolverInterface $resolver)
{ {
@ -45,8 +41,8 @@ abstract class Loader implements LoaderInterface
/** /**
* Imports a resource. * Imports a resource.
* *
* @param mixed $resource A Resource * @param mixed $resource A resource
* @param string $type The resource type * @param string|null $type The resource type or null if unknown
* *
* @return mixed * @return mixed
*/ */
@ -58,12 +54,12 @@ abstract class Loader implements LoaderInterface
/** /**
* Finds a loader able to load an imported resource. * Finds a loader able to load an imported resource.
* *
* @param mixed $resource A Resource * @param mixed $resource A resource
* @param string $type The resource type * @param string|null $type The resource type or null if unknown
* *
* @return LoaderInterface A LoaderInterface instance * @return LoaderInterface A LoaderInterface instance
* *
* @throws FileLoaderLoadException if no loader is found * @throws FileLoaderLoadException If no loader is found
*/ */
public function resolve($resource, $type = null) public function resolve($resource, $type = null)
{ {

View File

@ -21,18 +21,20 @@ interface LoaderInterface
/** /**
* Loads a resource. * Loads a resource.
* *
* @param mixed $resource The resource * @param mixed $resource The resource
* @param string $type The resource type * @param string|null $type The resource type or null if unknown
*
* @throws \Exception If something went wrong
*/ */
public function load($resource, $type = null); public function load($resource, $type = null);
/** /**
* Returns true if this class supports the given resource. * Returns whether this class supports the given resource.
* *
* @param mixed $resource A resource * @param mixed $resource A resource
* @param string $type The resource type * @param string|null $type The resource type or null if unknown
* *
* @return bool true if this class supports the given resource, false otherwise * @return bool True if this class supports the given resource, false otherwise
*/ */
public function supports($resource, $type = null); public function supports($resource, $type = null);

View File

@ -40,12 +40,7 @@ class LoaderResolver implements LoaderResolverInterface
} }
/** /**
* Returns a loader able to load the resource. * {@inheritdoc}
*
* @param mixed $resource A resource
* @param string $type The resource type
*
* @return LoaderInterface|false A LoaderInterface instance
*/ */
public function resolve($resource, $type = null) public function resolve($resource, $type = null)
{ {

View File

@ -21,10 +21,10 @@ interface LoaderResolverInterface
/** /**
* Returns a loader able to load the resource. * Returns a loader able to load the resource.
* *
* @param mixed $resource A resource * @param mixed $resource A resource
* @param string $type The resource type * @param string|null $type The resource type or null if unknown
* *
* @return LoaderInterface A LoaderInterface instance * @return LoaderInterface|false The loader or false if none is able to load the resource
*/ */
public function resolve($resource, $type = null); public function resolve($resource, $type = null);
} }

View File

@ -24,8 +24,8 @@ class DirectoryResource implements ResourceInterface, \Serializable
/** /**
* Constructor. * Constructor.
* *
* @param string $resource The file path to the resource * @param string $resource The file path to the resource
* @param string $pattern A pattern to restrict monitored files * @param string|null $pattern A pattern to restrict monitored files
*/ */
public function __construct($resource, $pattern = null) public function __construct($resource, $pattern = null)
{ {
@ -34,9 +34,7 @@ class DirectoryResource implements ResourceInterface, \Serializable
} }
/** /**
* Returns a string representation of the Resource. * {@inheritdoc}
*
* @return string A string representation of the Resource
*/ */
public function __toString() public function __toString()
{ {
@ -44,26 +42,25 @@ class DirectoryResource implements ResourceInterface, \Serializable
} }
/** /**
* Returns the resource tied to this Resource. * {@inheritdoc}
*
* @return mixed The resource
*/ */
public function getResource() public function getResource()
{ {
return $this->resource; return $this->resource;
} }
/**
* Returns the pattern to restrict monitored files
*
* @return string|null
*/
public function getPattern() public function getPattern()
{ {
return $this->pattern; return $this->pattern;
} }
/** /**
* Returns true if the resource has not been updated since the given timestamp. * {@inheritdoc}
*
* @param int $timestamp The last time the resource was loaded
*
* @return bool true if the resource has not been updated, false otherwise
*/ */
public function isFresh($timestamp) public function isFresh($timestamp)
{ {

View File

@ -20,6 +20,9 @@ namespace Symfony\Component\Config\Resource;
*/ */
class FileResource implements ResourceInterface, \Serializable class FileResource implements ResourceInterface, \Serializable
{ {
/**
* @var string|false
*/
private $resource; private $resource;
/** /**
@ -33,9 +36,7 @@ class FileResource implements ResourceInterface, \Serializable
} }
/** /**
* Returns a string representation of the Resource. * {@inheritdoc}
*
* @return string A string representation of the Resource
*/ */
public function __toString() public function __toString()
{ {
@ -43,9 +44,7 @@ class FileResource implements ResourceInterface, \Serializable
} }
/** /**
* Returns the resource tied to this Resource. * {@inheritdoc}
*
* @return mixed The resource
*/ */
public function getResource() public function getResource()
{ {
@ -53,15 +52,11 @@ class FileResource implements ResourceInterface, \Serializable
} }
/** /**
* Returns true if the resource has not been updated since the given timestamp. * {@inheritdoc}
*
* @param int $timestamp The last time the resource was loaded
*
* @return bool true if the resource has not been updated, false otherwise
*/ */
public function isFresh($timestamp) public function isFresh($timestamp)
{ {
if (!file_exists($this->resource)) { if (false === $this->resource || !file_exists($this->resource)) {
return false; return false;
} }

View File

@ -28,14 +28,14 @@ interface ResourceInterface
/** /**
* Returns true if the resource has not been updated since the given timestamp. * Returns true if the resource has not been updated since the given timestamp.
* *
* @param int $timestamp The last time the resource was loaded * @param int $timestamp The last time the resource was loaded
* *
* @return bool true if the resource has not been updated, false otherwise * @return bool True if the resource has not been updated, false otherwise
*/ */
public function isFresh($timestamp); public function isFresh($timestamp);
/** /**
* Returns the resource tied to this Resource. * Returns the tied resource.
* *
* @return mixed The resource * @return mixed The resource
*/ */

View File

@ -87,6 +87,7 @@ class FileLocatorTest extends \PHPUnit_Framework_TestCase
/** /**
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
* @expectedExceptionMessage The file "foobar.xml" does not exist
*/ */
public function testLocateThrowsAnExceptionIfTheFileDoesNotExists() public function testLocateThrowsAnExceptionIfTheFileDoesNotExists()
{ {
@ -104,4 +105,15 @@ class FileLocatorTest extends \PHPUnit_Framework_TestCase
$loader->locate(__DIR__.'/Fixtures/foobar.xml', __DIR__); $loader->locate(__DIR__.'/Fixtures/foobar.xml', __DIR__);
} }
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage An empty file name is not valid to be located.
*/
public function testLocateEmpty()
{
$loader = new FileLocator(array(__DIR__.'/Fixtures'));
$loader->locate(null, __DIR__);
}
} }

View File

@ -47,7 +47,7 @@ class FileLocator extends BaseFileLocator
*/ */
public function locate($file, $currentPath = null, $first = true) public function locate($file, $currentPath = null, $first = true)
{ {
if ('@' === $file[0]) { if (isset($file[0]) && '@' === $file[0]) {
return $this->kernel->locateResource($file, $this->path, $first); return $this->kernel->locateResource($file, $this->path, $first);
} }