From f1d686b56a7eaa2c2bca243749ea05662d769afc Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Mon, 14 Feb 2011 16:29:31 +0100 Subject: [PATCH] [Config] Improve the component --- ConfigCache.php | 2 +- FileLocator.php | 23 +++++++++++++++-------- FileLocatorInterface.php | 7 ++++--- Loader/DelegatingLoader.php | 15 +++------------ Loader/Loader.php | 9 +++++---- Loader/LoaderInterface.php | 1 + Resource/FileResource.php | 2 +- Resource/ResourceInterface.php | 2 +- 8 files changed, 31 insertions(+), 30 deletions(-) diff --git a/ConfigCache.php b/ConfigCache.php index 7d3c25219..713d12f83 100644 --- a/ConfigCache.php +++ b/ConfigCache.php @@ -46,7 +46,7 @@ public function __construct($cacheDir, $file, $debug) */ public function __toString() { - return $this->cacheDir.'/'.$this->file.'.php'; + return $this->getCacheFile(); } /** diff --git a/FileLocator.php b/FileLocator.php index f62c8b972..916cf19e7 100644 --- a/FileLocator.php +++ b/FileLocator.php @@ -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 */ @@ -54,13 +55,19 @@ 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; } } @@ -68,7 +75,7 @@ public function locate($name, $currentPath = null, $first = true) 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; } /** diff --git a/FileLocatorInterface.php b/FileLocatorInterface.php index fff08e179..aa0c7d0f4 100644 --- a/FileLocatorInterface.php +++ b/FileLocatorInterface.php @@ -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 */ diff --git a/Loader/DelegatingLoader.php b/Loader/DelegatingLoader.php index cec066fb9..3f6b6da2a 100644 --- a/Loader/DelegatingLoader.php +++ b/Loader/DelegatingLoader.php @@ -21,11 +21,6 @@ */ class DelegatingLoader extends Loader { - /** - * @var LoaderResolverInterface - */ - protected $resolver; - /** * Constructor. * @@ -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) { @@ -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; } } diff --git a/Loader/Loader.php b/Loader/Loader.php index 20f24df00..9c7eebcda 100644 --- a/Loader/Loader.php +++ b/Loader/Loader.php @@ -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; } + } diff --git a/Loader/LoaderInterface.php b/Loader/LoaderInterface.php index b803373d0..801499f40 100644 --- a/Loader/LoaderInterface.php +++ b/Loader/LoaderInterface.php @@ -49,4 +49,5 @@ function getResolver(); * @param LoaderResolver $resolver A LoaderResolver instance */ function setResolver(LoaderResolver $resolver); + } diff --git a/Resource/FileResource.php b/Resource/FileResource.php index b4aa03146..561612afc 100644 --- a/Resource/FileResource.php +++ b/Resource/FileResource.php @@ -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 */ diff --git a/Resource/ResourceInterface.php b/Resource/ResourceInterface.php index 764d4a367..29558bea4 100644 --- a/Resource/ResourceInterface.php +++ b/Resource/ResourceInterface.php @@ -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 */