Skip to content

Commit

Permalink
[Config] fix filelocator with empty name
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobion committed Mar 14, 2014
1 parent 0ccf5bc commit fb1b5aa
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 40 deletions.
20 changes: 8 additions & 12 deletions src/Symfony/Component/Config/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,14 @@ public function __construct($paths = array())
}

/**
* Returns a full path for a given file name.
*
* @param mixed $name The file name to locate
* @param string $currentPath The current path
* @param Boolean $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
* {@inheritDoc}
*/
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 (!file_exists($name)) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $name));
Expand Down Expand Up @@ -84,10 +80,10 @@ public function locate($name, $currentPath = null, $first = true)
*/
private function isAbsolutePath($file)
{
if ($file[0] == '/' || $file[0] == '\\'
if ($file[0] === '/' || $file[0] === '\\'
|| (strlen($file) > 3 && ctype_alpha($file[0])
&& $file[1] == ':'
&& ($file[2] == '\\' || $file[2] == '/')
&& $file[1] === ':'
&& ($file[2] === '\\' || $file[2] === '/')
)
|| null !== parse_url($file, PHP_URL_SCHEME)
) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Config/Loader/DelegatingLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ public function load($resource, $type = null)
*/
public function supports($resource, $type = null)
{
return false === $this->resolver->resolve($resource, $type) ? false : true;
return false !== $this->resolver->resolve($resource, $type);
}
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/Config/Loader/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@
*/
abstract class FileLoader extends Loader
{
/**
* @var array
*/
protected static $loading = array();

/**
* @var FileLocatorInterface
*/
protected $locator;

private $currentDir;
Expand All @@ -38,11 +44,21 @@ public function __construct(FileLocatorInterface $locator)
$this->locator = $locator;
}

/**
* Sets the current directory.
*
* @param string $dir
*/
public function setCurrentDir($dir)
{
$this->currentDir = $dir;
}

/**
* Returns the file locator used by this loader.
*
* @return FileLocatorInterface
*/
public function getLocator()
{
return $this->locator;
Expand Down
25 changes: 11 additions & 14 deletions src/Symfony/Component/Config/Resource/DirectoryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class DirectoryResource implements ResourceInterface, \Serializable
/**
* Constructor.
*
* @param string $resource The file path to the resource
* @param string $pattern A pattern to restrict monitored files
* @param string $resource The file path to the resource
* @param string|null $pattern A pattern to restrict monitored files
*/
public function __construct($resource, $pattern = null)
{
Expand All @@ -34,36 +34,33 @@ public function __construct($resource, $pattern = null)
}

/**
* Returns a string representation of the Resource.
*
* @return string A string representation of the Resource
* {@inheritDoc}
*/
public function __toString()
{
return (string) $this->resource;
return $this->resource;
}

/**
* Returns the resource tied to this Resource.
*
* @return mixed The resource
* {@inheritDoc}
*/
public function getResource()
{
return $this->resource;
}

/**
* Returns the pattern to restrict monitored files
*
* @return string|null
*/
public function getPattern()
{
return $this->pattern;
}

/**
* Returns true if the resource has not been updated since the given timestamp.
*
* @param integer $timestamp The last time the resource was loaded
*
* @return Boolean true if the resource has not been updated, false otherwise
* {@inheritDoc}
*/
public function isFresh($timestamp)
{
Expand Down
19 changes: 7 additions & 12 deletions src/Symfony/Component/Config/Resource/FileResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*/
class FileResource implements ResourceInterface, \Serializable
{
/**
* @var string|false
*/
private $resource;

/**
Expand All @@ -33,35 +36,27 @@ public function __construct($resource)
}

/**
* Returns a string representation of the Resource.
*
* @return string A string representation of the Resource
* {@inheritDoc}
*/
public function __toString()
{
return (string) $this->resource;
}

/**
* Returns the resource tied to this Resource.
*
* @return mixed The resource
* {@inheritDoc}
*/
public function getResource()
{
return $this->resource;
}

/**
* Returns true if the resource has not been updated since the given timestamp.
*
* @param integer $timestamp The last time the resource was loaded
*
* @return Boolean true if the resource has not been updated, false otherwise
* {@inheritDoc}
*/
public function isFresh($timestamp)
{
if (!file_exists($this->resource)) {
if (false === $this->resource || !file_exists($this->resource)) {
return false;
}

Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Config/Tests/FileLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function testLocate()

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The file "foobar.xml" does not exist
*/
public function testLocateThrowsAnExceptionIfTheFileDoesNotExists()
{
Expand All @@ -104,4 +105,15 @@ public function testLocateThrowsAnExceptionIfTheFileDoesNotExistsInAbsolutePath(

$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__);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ class TestFileLoader extends FileLoader

public function load($resource, $type = null)
{
return $resource;
}

public function supports($resource, $type = null)
Expand Down

0 comments on commit fb1b5aa

Please sign in to comment.