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

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