Преглед на файлове

[Config] Improve the component

Victor Berchet преди 14 години
родител
ревизия
8588d55c11

+ 1 - 1
src/Symfony/Component/Config/ConfigCache.php

@@ -46,7 +46,7 @@ class ConfigCache
      */
     public function __toString()
     {
-        return $this->cacheDir.'/'.$this->file.'.php';
+        return $this->getCacheFile();
     }
 
     /**

+ 15 - 8
src/Symfony/Component/Config/FileLocator.php

@@ -36,10 +36,11 @@ class FileLocator implements 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
      */
@@ -54,13 +55,19 @@ class FileLocator implements FileLocatorInterface
         }
 
         $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 @@ class FileLocator implements FileLocatorInterface
             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;
     }
 
     /**

+ 4 - 3
src/Symfony/Component/Config/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
      */

+ 3 - 12
src/Symfony/Component/Config/Loader/DelegatingLoader.php

@@ -21,11 +21,6 @@ namespace Symfony\Component\Config\Loader;
  */
 class DelegatingLoader extends Loader
 {
-    /**
-     * @var LoaderResolverInterface
-     */
-    protected $resolver;
-
     /**
      * Constructor.
      *
@@ -41,6 +36,8 @@ class DelegatingLoader extends Loader
      *
      * @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 @@ class DelegatingLoader extends Loader
      */
     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;
     }
 }

+ 5 - 4
src/Symfony/Component/Config/Loader/Loader.php

@@ -63,17 +63,18 @@ abstract class Loader implements LoaderInterface
      */
     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 - 0
src/Symfony/Component/Config/Loader/LoaderInterface.php

@@ -49,4 +49,5 @@ interface LoaderInterface
      * @param LoaderResolver $resolver A LoaderResolver instance
      */
     function setResolver(LoaderResolver $resolver);
+
 }

+ 1 - 1
src/Symfony/Component/Config/Resource/FileResource.php

@@ -53,7 +53,7 @@ class FileResource implements ResourceInterface
     /**
      * 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
      */

+ 1 - 1
src/Symfony/Component/Config/Resource/ResourceInterface.php

@@ -28,7 +28,7 @@ interface ResourceInterface
     /**
      * 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
      */

+ 19 - 3
tests/Symfony/Tests/Component/Config/FileLocatorTest.php

@@ -40,11 +40,27 @@ class FileLocatorTest extends \PHPUnit_Framework_TestCase
 
     public function testLocate()
     {
-        $loader = new FileLocator(array(__DIR__.'/Fixtures'));
+        $loader = new FileLocator(__DIR__.'/Fixtures');
+
+        $this->assertEquals(
+            __DIR__.DIRECTORY_SEPARATOR.'FileLocatorTest.php',
+            $loader->locate('FileLocatorTest.php', __DIR__),
+            '->locate() returns the absolute filename if the file exists in the given path'
+        );
 
-        $this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'FileLocatorTest.php', $loader->locate('FileLocatorTest.php', __DIR__), '->getAbsolutePath() returns an absolute filename if the file exists in the current path');
+        $this->assertEquals(
+            __DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml',
+            $loader->locate('foo.xml', __DIR__),
+            '->locate() returns the absolute filename if the file exists in one of the paths given in the constructor'
+        );
+
+        $loader = new FileLocator(array(__DIR__.'/Fixtures', __DIR__.'/Fixtures/Again'));
 
-        $this->assertEquals(__DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml', $loader->locate('foo.xml', __DIR__), '->getAbsolutePath() returns an absolute filename if the file exists in one of the paths given in the constructor');
+        $this->assertEquals(
+            array(__DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.DIRECTORY_SEPARATOR.'foo.xml'),
+            $loader->locate('foo.xml', __DIR__, false),
+            '->locate() returns an array of absolute filenames'
+        );
     }
 
     /**

+ 0 - 0
tests/Symfony/Tests/Component/Config/Fixtures/Again/foo.xml