vendor/friendsofsymfony/rest-bundle/Routing/Loader/DirectoryRouteLoader.php line 54

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSRestBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\RestBundle\Routing\Loader;
  11. use Symfony\Component\Config\FileLocatorInterface;
  12. use Symfony\Component\Config\Loader\Loader;
  13. use Symfony\Component\Finder\Finder;
  14. use Symfony\Component\Routing\RouteCollection;
  15. /**
  16.  * Parse annotated controller classes from all files of a directory.
  17.  *
  18.  * @author Christian Flothmann <christian.flothmann@xabbuh.de>
  19.  */
  20. class DirectoryRouteLoader extends Loader
  21. {
  22.     private $fileLocator;
  23.     private $processor;
  24.     public function __construct(FileLocatorInterface $fileLocatorRestRouteProcessor $processor)
  25.     {
  26.         $this->fileLocator $fileLocator;
  27.         $this->processor $processor;
  28.     }
  29.     /**
  30.      * {@inheritdoc}
  31.      */
  32.     public function load($resource$type null)
  33.     {
  34.         if (isset($resource[0]) && in_array($resource[0], ['@''.'], true)) {
  35.             $resource $this->fileLocator->locate($resource);
  36.         }
  37.         if (!is_dir($resource)) {
  38.             throw new \InvalidArgumentException(sprintf('Given resource of type "%s" is no directory.'$resource));
  39.         }
  40.         $collection = new RouteCollection();
  41.         $finder = new Finder();
  42.         foreach ($finder->in($resource)->name('*.php')->sortByName()->files() as $file) {
  43.             if ($class ClassUtils::findClassInFile($file)) {
  44.                 $imported $this->processor->importResource($this$class, array(), nullnull'rest');
  45.                 $collection->addCollection($imported);
  46.             }
  47.         }
  48.         return $collection;
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      */
  53.     public function supports($resource$type null)
  54.     {
  55.         if ('rest' !== $type || !is_string($resource)) {
  56.             return false;
  57.         }
  58.         if (isset($resource[0]) && in_array($resource[0], ['@''.'], true)) {
  59.             $resource $this->fileLocator->locate($resource);
  60.         }
  61.         return is_dir($resource);
  62.     }
  63. }