vendor/friendsofsymfony/rest-bundle/Routing/Loader/RestRouteLoader.php line 76

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 FOS\RestBundle\Routing\Loader\Reader\RestControllerReader;
  12. use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
  13. use Symfony\Component\Config\FileLocatorInterface;
  14. use Symfony\Component\Config\Loader\Loader;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpKernel\Kernel;
  18. /**
  19.  * RestRouteLoader REST-enabled controller router loader.
  20.  *
  21.  * @author Konstantin Kudryashov <ever.zet@gmail.com>
  22.  * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
  23.  */
  24. class RestRouteLoader extends Loader
  25. {
  26.     protected $container;
  27.     protected $controllerParser;
  28.     protected $controllerReader;
  29.     protected $defaultFormat;
  30.     protected $locator;
  31.     /**
  32.      * Initializes loader.
  33.      *
  34.      * @param ContainerInterface   $container
  35.      * @param FileLocatorInterface $locator
  36.      * @param ControllerNameParser $controllerParser
  37.      * @param RestControllerReader $controllerReader
  38.      * @param string               $defaultFormat
  39.      */
  40.     public function __construct(
  41.         ContainerInterface $container,
  42.         FileLocatorInterface $locator,
  43.         $controllerParser,
  44.         RestControllerReader $controllerReader,
  45.         $defaultFormat 'html'
  46.     ) {
  47.         $this->container $container;
  48.         $this->locator $locator;
  49.         $this->controllerParser $controllerParser;
  50.         $this->controllerReader $controllerReader;
  51.         $this->defaultFormat $defaultFormat;
  52.     }
  53.     /**
  54.      * Returns controller reader.
  55.      *
  56.      * @return RestControllerReader
  57.      */
  58.     public function getControllerReader()
  59.     {
  60.         return $this->controllerReader;
  61.     }
  62.     /**
  63.      * {@inheritdoc}
  64.      */
  65.     public function load($controller$type null)
  66.     {
  67.         list($prefix$class) = $this->getControllerLocator($controller);
  68.         $collection $this->controllerReader->read(new \ReflectionClass($class));
  69.         $collection->prependRouteControllersWithPrefix($prefix);
  70.         $collection->setDefaultFormat($this->defaultFormat);
  71.         return $collection;
  72.     }
  73.     /**
  74.      * {@inheritdoc}
  75.      */
  76.     public function supports($resource$type null)
  77.     {
  78.         return is_string($resource)
  79.             && 'rest' === $type
  80.             && !in_array(
  81.                 pathinfo($resourcePATHINFO_EXTENSION),
  82.                 ['xml''yml''yaml']
  83.             );
  84.     }
  85.     /**
  86.      * Returns controller locator by it's id.
  87.      *
  88.      * @param string $controller
  89.      *
  90.      * @throws \InvalidArgumentException
  91.      *
  92.      * @return array
  93.      */
  94.     private function getControllerLocator($controller)
  95.     {
  96.         $class null;
  97.         $prefix null;
  98.         if (=== strpos($controller'@')) {
  99.             $file $this->locator->locate($controller);
  100.             $controllerClass ClassUtils::findClassInFile($file);
  101.             if (false === $controllerClass) {
  102.                 throw new \InvalidArgumentException(sprintf('Can\'t find class for controller "%s"'$controller));
  103.             }
  104.             $controller $controllerClass;
  105.         }
  106.         if ($this->container->has($controller)) {
  107.             // service_id
  108.             $prefix $controller.':';
  109.             if (Kernel::VERSION_ID >= 40100) {
  110.                 $prefix .= ':';
  111.             }
  112.             $useScope method_exists($this->container'enterScope') && $this->container->hasScope('request');
  113.             if ($useScope) {
  114.                 $this->container->enterScope('request');
  115.                 $this->container->set('request', new Request());
  116.             }
  117.             $class get_class($this->container->get($controller));
  118.             if ($useScope) {
  119.                 $this->container->leaveScope('request');
  120.             }
  121.         } elseif (class_exists($controller)) {
  122.             // full class name
  123.             $class $controller;
  124.             $prefix $class.'::';
  125.         } elseif ($this->controllerParser && false !== strpos($controller':')) {
  126.             // bundle:controller notation
  127.             try {
  128.                 $notation $this->controllerParser->parse($controller.':method');
  129.                 list($class) = explode('::'$notation);
  130.                 $prefix $class.'::';
  131.             } catch (\Exception $e) {
  132.                 throw new \InvalidArgumentException(sprintf('Can\'t locate "%s" controller.'$controller));
  133.             }
  134.         }
  135.         if (empty($class)) {
  136.             throw new \InvalidArgumentException(sprintf('Class could not be determined for Controller identified by "%s".'$controller));
  137.         }
  138.         return [$prefix$class];
  139.     }
  140. }