Skip to content

Commit

Permalink
[Config] Improve the component
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored and fabpot committed Feb 14, 2011
1 parent fa11ec6 commit f1d686b
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 30 deletions.
2 changes: 1 addition & 1 deletion ConfigCache.php
Expand Up @@ -46,7 +46,7 @@ public function __construct($cacheDir, $file, $debug)
*/
public function __toString()
{
return $this->cacheDir.'/'.$this->file.'.php';
return $this->getCacheFile();
}

/**
Expand Down
23 changes: 15 additions & 8 deletions FileLocator.php
Expand Up @@ -36,10 +36,11 @@ 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 mixed $name The file name to locate
* @param string $currentPath The current path
* @param Boolean $first Wether to return the first occurence or an array of filenames
*
* @return string The full path for the file
* @return string|array The full path to the file|An array of file paths
*
* @throws \InvalidArgumentException When file is not found
*/
Expand All @@ -54,21 +55,27 @@ public function locate($name, $currentPath = null, $first = true)
}

$filepaths = array();
if (null !== $currentPath && file_exists($currentPath.DIRECTORY_SEPARATOR.$name)) {
$filepaths[] = $currentPath.DIRECTORY_SEPARATOR.$name;
if (null !== $currentPath && file_exists($file = $currentPath.DIRECTORY_SEPARATOR.$name)) {
if (true === $first) {
return $file;
}
$filepaths[] = $file;
}

foreach ($this->paths as $path) {
if (file_exists($path.DIRECTORY_SEPARATOR.$name)) {
$filepaths[] = $path.DIRECTORY_SEPARATOR.$name;
if (file_exists($file = $path.DIRECTORY_SEPARATOR.$name)) {
if (true === $first) {
return $file;
}
$filepaths[] = $file;
}
}

if (!$filepaths) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist (in: %s%s).', $name, null !== $currentPath ? $currentPath.', ' : '', implode(', ', $this->paths)));
}

return true === $first ? $filepaths[0] : $filepaths;
return $filepaths;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions FileLocatorInterface.php
Expand Up @@ -19,10 +19,11 @@ interface FileLocatorInterface
/**
* Returns a full path for a given file name.
*
* @param mixed $name The file name to locate
* @param string $currentPath The current path
* @param mixed $name The file name to locate
* @param string $currentPath The current path
* @param Boolean $first Wether to return the first occurence or an array of filenames
*
* @return string The full path for the file
* @return string|array The full path to the file|An array of file paths
*
* @throws \InvalidArgumentException When file is not found
*/
Expand Down
15 changes: 3 additions & 12 deletions Loader/DelegatingLoader.php
Expand Up @@ -21,11 +21,6 @@
*/
class DelegatingLoader extends Loader
{
/**
* @var LoaderResolverInterface
*/
protected $resolver;

/**
* Constructor.
*
Expand All @@ -41,6 +36,8 @@ public function __construct(LoaderResolverInterface $resolver)
*
* @param mixed $resource A resource
* @param string $type The resource type
*
* @throws \InvalidArgumentException if no loader is found.
*/
public function load($resource, $type = null)
{
Expand All @@ -63,12 +60,6 @@ public function load($resource, $type = null)
*/
public function supports($resource, $type = null)
{
foreach ($this->resolver->getLoaders() as $loader) {
if ($loader->supports($resource, $type)) {
return true;
}
}

return false;
return false === $this->resolver->resolve($resource, $type) ? false : true;

This comment has been minimized.

Copy link
@dol

dol Mar 22, 2013

Isn't this line quite difficult to read? A comparison like this would do the same.

return true === $this->resolver->resolve($resource, $type);
}
}
9 changes: 5 additions & 4 deletions Loader/Loader.php
Expand Up @@ -63,17 +63,18 @@ public function import($resource, $type = null)
*/
public function resolve($resource, $type = null)
{
$loader = false;

if ($this->supports($resource, $type)) {
$loader = $this;
} elseif (null !== $this->resolver) {
$loader = $this->resolver->resolve($resource, $type);
return $this;
}

$loader = null === $this->resolver ? false : $this->resolver->resolve($resource, $type);

if (false === $loader) {
throw new \InvalidArgumentException(sprintf('Unable to load the "%s" resource.', is_string($resource) ? $resource : (is_object($resource) ? get_class($resource) : 'RESOURCE')));
}

return $loader;
}

}
1 change: 1 addition & 0 deletions Loader/LoaderInterface.php
Expand Up @@ -49,4 +49,5 @@ function getResolver();
* @param LoaderResolver $resolver A LoaderResolver instance
*/
function setResolver(LoaderResolver $resolver);

}
2 changes: 1 addition & 1 deletion Resource/FileResource.php
Expand Up @@ -53,7 +53,7 @@ public function getResource()
/**
* Returns true if the resource has not been updated since the given timestamp.
*
* @param timestamp $timestamp The last time the resource was loaded
* @param integer $timestamp The last time the resource was loaded
*
* @return Boolean true if the resource has not been updated, false otherwise
*/
Expand Down
2 changes: 1 addition & 1 deletion Resource/ResourceInterface.php
Expand Up @@ -28,7 +28,7 @@ function __toString();
/**
* Returns true if the resource has not been updated since the given timestamp.
*
* @param int $timestamp The last time the resource was loaded
* @param integer $timestamp The last time the resource was loaded
*
* @return Boolean true if the resource has not been updated, false otherwise
*/
Expand Down

0 comments on commit f1d686b

Please sign in to comment.