vendor/friendsofsymfony/rest-bundle/Routing/Loader/Reader/RestControllerReader.php line 105

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\Reader;
  11. use Doctrine\Common\Annotations\Reader;
  12. use FOS\RestBundle\Controller\Annotations;
  13. use FOS\RestBundle\Routing\ClassResourceInterface;
  14. use FOS\RestBundle\Routing\RestRouteCollection;
  15. use Symfony\Component\Config\Resource\FileResource;
  16. /**
  17.  * REST controller reader.
  18.  *
  19.  * @author Konstantin Kudryashov <ever.zet@gmail.com>
  20.  */
  21. class RestControllerReader
  22. {
  23.     private $actionReader;
  24.     private $annotationReader;
  25.     /**
  26.      * Initializes controller reader.
  27.      *
  28.      * @param RestActionReader $actionReader     action reader
  29.      * @param Reader           $annotationReader annotation reader
  30.      */
  31.     public function __construct(RestActionReader $actionReaderReader $annotationReader)
  32.     {
  33.         $this->actionReader $actionReader;
  34.         $this->annotationReader $annotationReader;
  35.     }
  36.     /**
  37.      * Returns action reader.
  38.      *
  39.      * @return RestActionReader
  40.      */
  41.     public function getActionReader()
  42.     {
  43.         return $this->actionReader;
  44.     }
  45.     /**
  46.      * Reads controller routes.
  47.      *
  48.      * @param \ReflectionClass $reflectionClass
  49.      *
  50.      * @throws \InvalidArgumentException
  51.      *
  52.      * @return RestRouteCollection
  53.      */
  54.     public function read(\ReflectionClass $reflectionClass)
  55.     {
  56.         $collection = new RestRouteCollection();
  57.         $collection->addResource(new FileResource($reflectionClass->getFileName()));
  58.         // read prefix annotation
  59.         if ($annotation $this->annotationReader->getClassAnnotation($reflectionClassAnnotations\Prefix::class)) {
  60.             $this->actionReader->setRoutePrefix($annotation->value);
  61.         }
  62.         // read name-prefix annotation
  63.         if ($annotation $this->annotationReader->getClassAnnotation($reflectionClassAnnotations\NamePrefix::class)) {
  64.             $this->actionReader->setNamePrefix($annotation->value);
  65.         }
  66.         // read version annotation
  67.         if ($annotation $this->annotationReader->getClassAnnotation($reflectionClassAnnotations\Version::class)) {
  68.             $this->actionReader->setVersions($annotation->value);
  69.         }
  70.         $resource = [];
  71.         // read route-resource annotation
  72.         if ($annotation $this->annotationReader->getClassAnnotation($reflectionClassAnnotations\RouteResource::class)) {
  73.             $resource explode('_'$annotation->resource);
  74.             $this->actionReader->setPluralize($annotation->pluralize);
  75.         } elseif ($reflectionClass->implementsInterface(ClassResourceInterface::class)) {
  76.             $resource preg_split(
  77.                 '/([A-Z][^A-Z]*)Controller/',
  78.                 $reflectionClass->getShortName(),
  79.                 -1,
  80.                 PREG_SPLIT_NO_EMPTY PREG_SPLIT_DELIM_CAPTURE
  81.             );
  82.             if (empty($resource)) {
  83.                 throw new \InvalidArgumentException("Controller '{$reflectionClass->name}' does not identify a resource");
  84.             }
  85.         }
  86.         // trim '/' at the start of the prefix
  87.         if ('/' === substr($prefix $this->actionReader->getRoutePrefix(), 01)) {
  88.             $this->actionReader->setRoutePrefix(substr($prefix1));
  89.         }
  90.         // read action routes into collection
  91.         foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
  92.             $this->actionReader->read($collection$method$resource);
  93.         }
  94.         $this->actionReader->setRoutePrefix(null);
  95.         $this->actionReader->setNamePrefix(null);
  96.         $this->actionReader->setVersions(null);
  97.         $this->actionReader->setPluralize(null);
  98.         $this->actionReader->setParents([]);
  99.         return $collection;
  100.     }
  101. }