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