|
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
|
39 |
|
* @param Reader $annotationReader annotation reader |
|
35
|
|
|
*/ |
|
36
|
39 |
|
public function __construct(RestActionReader $actionReader, Reader $annotationReader) |
|
37
|
39 |
|
{ |
|
38
|
39 |
|
$this->actionReader = $actionReader; |
|
39
|
|
|
$this->annotationReader = $annotationReader; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns action reader. |
|
44
|
|
|
* |
|
45
|
23 |
|
* @return RestActionReader |
|
46
|
|
|
*/ |
|
47
|
23 |
|
public function getActionReader() |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->actionReader; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Reads controller routes. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \ReflectionClass $reflectionClass |
|
56
|
|
|
* |
|
57
|
|
|
* @throws \InvalidArgumentException |
|
58
|
|
|
* |
|
59
|
24 |
|
* @return RestRouteCollection |
|
60
|
|
|
*/ |
|
61
|
24 |
|
public function read(\ReflectionClass $reflectionClass) |
|
62
|
24 |
|
{ |
|
63
|
|
|
$collection = new RestRouteCollection(); |
|
64
|
|
|
$collection->addResource(new FileResource($reflectionClass->getFileName())); |
|
65
|
24 |
|
|
|
66
|
1 |
|
// read prefix annotation |
|
67
|
1 |
|
if ($annotation = $this->annotationReader->getClassAnnotation($reflectionClass, Annotations\Prefix::class)) { |
|
68
|
|
|
$this->actionReader->setRoutePrefix($annotation->value); |
|
69
|
|
|
} |
|
70
|
24 |
|
|
|
71
|
|
|
// read name-prefix annotation |
|
72
|
|
|
if ($annotation = $this->annotationReader->getClassAnnotation($reflectionClass, Annotations\NamePrefix::class)) { |
|
73
|
|
|
$this->actionReader->setNamePrefix($annotation->value); |
|
74
|
|
|
} |
|
75
|
24 |
|
|
|
76
|
1 |
|
// read version annotation |
|
77
|
1 |
|
if ($annotation = $this->annotationReader->getClassAnnotation($reflectionClass, Annotations\Version::class)) { |
|
78
|
|
|
$this->actionReader->setVersions($annotation->value); |
|
79
|
24 |
|
} |
|
80
|
|
|
|
|
81
|
24 |
|
$resource = []; |
|
82
|
1 |
|
// read route-resource annotation |
|
83
|
1 |
|
if ($annotation = $this->annotationReader->getClassAnnotation($reflectionClass, Annotations\RouteResource::class)) { |
|
84
|
24 |
|
$resource = explode('_', $annotation->resource); |
|
85
|
4 |
|
$this->actionReader->setPluralize($annotation->pluralize); |
|
86
|
4 |
|
} 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
|
|
|
); |
|
90
|
|
|
if (empty($resource)) { |
|
91
|
4 |
|
throw new \InvalidArgumentException("Controller '{$reflectionClass->name}' does not identify a resource"); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
24 |
|
|
|
95
|
2 |
|
// trim '/' at the start of the prefix |
|
96
|
2 |
|
if ('/' === substr($prefix = $this->actionReader->getRoutePrefix(), 0, 1)) { |
|
97
|
|
|
$this->actionReader->setRoutePrefix(substr($prefix, 1)); |
|
98
|
|
|
} |
|
99
|
24 |
|
|
|
100
|
24 |
|
// read action routes into collection |
|
101
|
24 |
|
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
102
|
|
|
$this->actionReader->read($collection, $method, $resource); |
|
103
|
24 |
|
} |
|
104
|
24 |
|
|
|
105
|
24 |
|
$this->actionReader->setRoutePrefix(null); |
|
106
|
24 |
|
$this->actionReader->setNamePrefix(null); |
|
107
|
24 |
|
$this->actionReader->setVersions(null); |
|
108
|
|
|
$this->actionReader->setPluralize(null); |
|
109
|
24 |
|
$this->actionReader->setParents([]); |
|
110
|
|
|
|
|
111
|
|
|
return $collection; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|