Преглед изворни кода

[Routing] refactored resources

Fabien Potencier пре 15 година
родитељ
комит
87ae06c8cb

+ 1 - 1
src/Symfony/Components/Routing/Loader/XmlFileLoader.php

@@ -4,7 +4,7 @@ namespace Symfony\Components\Routing\Loader;
 
 use Symfony\Components\Routing\RouteCollection;
 use Symfony\Components\Routing\Route;
-use Symfony\Components\Routing\FileResource;
+use Symfony\Components\Routing\Resource\FileResource;
 
 /*
  * This file is part of the Symfony framework.

+ 1 - 1
src/Symfony/Components/Routing/Loader/YamlFileLoader.php

@@ -4,7 +4,7 @@ namespace Symfony\Components\Routing\Loader;
 
 use Symfony\Components\Routing\RouteCollection;
 use Symfony\Components\Routing\Route;
-use Symfony\Components\Routing\FileResource;
+use Symfony\Components\Routing\Resource\FileResource;
 use Symfony\Components\Yaml\Yaml;
 
 /*

+ 12 - 2
src/Symfony/Components/Routing/FileResource.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace Symfony\Components\Routing;
+namespace Symfony\Components\Routing\Resource;
 
 /*
  * This file is part of the Symfony framework.
@@ -29,7 +29,17 @@ class FileResource implements ResourceInterface
      */
     public function __construct($resource)
     {
-        $this->resource = $resource;
+        $this->resource = realpath($resource);
+    }
+
+    /**
+     * Returns a string representation of the Resource.
+     *
+     * @return string A string representation of the Resource
+     */
+    public function __toString()
+    {
+        return $this->resource;
     }
 
     /**

+ 8 - 1
src/Symfony/Components/Routing/ResourceInterface.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace Symfony\Components\Routing;
+namespace Symfony\Components\Routing\Resource;
 
 /*
  * This file is part of the Symfony framework.
@@ -20,6 +20,13 @@ namespace Symfony\Components\Routing;
  */
 interface ResourceInterface
 {
+    /**
+     * Returns a string representation of the Resource.
+     *
+     * @return string A string representation of the Resource
+     */
+    function __toString();
+
     /**
      * Returns true if the resource has not been updated since the given timestamp.
      *

+ 3 - 1
src/Symfony/Components/Routing/RouteCollection.php

@@ -2,6 +2,8 @@
 
 namespace Symfony\Components\Routing;
 
+use Symfony\Components\Routing\Resource\ResourceInterface;
+
 /*
  * This file is part of the Symfony framework.
  *
@@ -111,7 +113,7 @@ class RouteCollection
      */
     public function getResources()
     {
-        return $this->resources;
+        return array_unique($this->resources);
     }
 
     /**

+ 0 - 0
tests/Symfony/Tests/Components/Routing/Fixtures/foo.xml


+ 0 - 0
tests/Symfony/Tests/Components/Routing/Fixtures/foo1.xml


+ 3 - 3
tests/Symfony/Tests/Components/Routing/FileResourceTest.php

@@ -9,9 +9,9 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Tests\Components\Routing;
+namespace Symfony\Tests\Components\Routing\Resource;
 
-use Symfony\Components\Routing\FileResource;
+use Symfony\Components\Routing\Resource\FileResource;
 
 class FileResourceTest extends \PHPUnit_Framework_TestCase
 {
@@ -20,7 +20,7 @@ class FileResourceTest extends \PHPUnit_Framework_TestCase
         $file = sys_get_temp_dir().'/tmp.xml';
         touch($file);
         $resource = new FileResource($file);
-        $this->assertEquals($file, $resource->getResource(), '->getResource() returns the path to the resource');
+        $this->assertEquals(realpath($file), $resource->getResource(), '->getResource() returns the path to the resource');
         unlink($file);
     }
 

+ 4 - 4
tests/Symfony/Tests/Components/Routing/RouteCollectionTest.php

@@ -13,7 +13,7 @@ namespace Symfony\Tests\Components\Routing;
 
 use Symfony\Components\Routing\RouteCollection;
 use Symfony\Components\Routing\Route;
-use Symfony\Components\Routing\FileResource;
+use Symfony\Components\Routing\Resource\FileResource;
 
 class RouteCollectionTest extends \PHPUnit_Framework_TestCase
 {
@@ -45,9 +45,9 @@ class RouteCollectionTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('/foo/foo1', $collection->getRoute('foo')->getPattern(), '->addCollection() can add a prefix to all merged routes');
 
         $collection = new RouteCollection();
-        $collection->addResource($foo = new FileResource('foo'));
+        $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
         $collection1 = new RouteCollection();
-        $collection1->addResource($foo1 = new FileResource('foo1'));
+        $collection1->addResource($foo1 = new FileResource(__DIR__.'/Fixtures/foo1.xml'));
         $collection->addCollection($collection1);
         $this->assertEquals(array($foo, $foo1), $collection->getResources(), '->addCollection() merges resources');
     }
@@ -65,7 +65,7 @@ class RouteCollectionTest extends \PHPUnit_Framework_TestCase
     public function testResource()
     {
         $collection = new RouteCollection();
-        $collection->addResource($foo = new FileResource('foo'));
+        $collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
         $this->assertEquals(array($foo), $collection->getResources(), '->addResources() adds a resource');
     }
 }