vendor/symfony/src/Symfony/Component/Routing/Loader/AnnotationDirectoryLoader.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\Routing\Loader;
       
    13 
       
    14 use Symfony\Component\Routing\RouteCollection;
       
    15 use Symfony\Component\Config\Resource\DirectoryResource;
       
    16 
       
    17 /**
       
    18  * AnnotationDirectoryLoader loads routing information from annotations set
       
    19  * on PHP classes and methods.
       
    20  *
       
    21  * @author Fabien Potencier <fabien@symfony.com>
       
    22  */
       
    23 class AnnotationDirectoryLoader extends AnnotationFileLoader
       
    24 {
       
    25     /**
       
    26      * Loads from annotations from a directory.
       
    27      *
       
    28      * @param string $path A directory path
       
    29      * @param string $type The resource type
       
    30      *
       
    31      * @return RouteCollection A RouteCollection instance
       
    32      *
       
    33      * @throws \InvalidArgumentException When the directory does not exist or its routes cannot be parsed
       
    34      */
       
    35     public function load($path, $type = null)
       
    36     {
       
    37         $dir = $this->locator->locate($path);
       
    38 
       
    39         $collection = new RouteCollection();
       
    40         $collection->addResource(new DirectoryResource($dir, '/\.php$/'));
       
    41         foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
       
    42             if (!$file->isFile() || '.php' !== substr($file->getFilename(), -4)) {
       
    43                 continue;
       
    44             }
       
    45 
       
    46             if ($class = $this->findClass($file)) {
       
    47                 $collection->addCollection($this->loader->load($class, $type));
       
    48             }
       
    49         }
       
    50 
       
    51         return $collection;
       
    52     }
       
    53 
       
    54     /**
       
    55      * Returns true if this class supports the given resource.
       
    56      *
       
    57      * @param mixed  $resource A resource
       
    58      * @param string $type     The resource type
       
    59      *
       
    60      * @return Boolean True if this class supports the given resource, false otherwise
       
    61      */
       
    62     public function supports($resource, $type = null)
       
    63     {
       
    64         try {
       
    65             $path = $this->locator->locate($resource);
       
    66         } catch (\Exception $e) {
       
    67             return false;
       
    68         }
       
    69 
       
    70         return is_string($resource) && is_dir($path) && (!$type || 'annotation' === $type);
       
    71     }
       
    72 }