-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAction.php
189 lines (164 loc) · 4.17 KB
/
Action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace In\Blueprint;
use RuntimeException;
use ReflectionMethod;
use Illuminate\Support\Collection;
class Action extends Section
{
/**
* Action reflector instance.
*
* @var \ReflectionMethod
*/
protected $reflector;
/**
* Annotations belonging to the action.
*
* @var \Illuminate\Support\Collection
*/
protected $annotations;
/**
* Parent resource of the action.
*
* @var \In\Blueprint\Resource
*/
protected $resource;
/**
* Create a new action instance.
*
* @param \ReflectionMethod $reflector
* @param \Illuminate\Support\Collection $annotations
*
* @return void
*/
public function __construct(ReflectionMethod $reflector, Collection $annotations)
{
$this->reflector = $reflector;
$this->annotations = $annotations;
}
/**
* Get the actions definition.
*
* @return string
*/
public function getDefinition()
{
$definition = $this->getMethod();
if ($identifier = $this->getIdentifier()) {
if ($uri = $this->getUri()) {
$definition = $definition.' '.$uri;
}
$definition = $identifier.' ['.$definition.']';
}
return '## '.$definition;
}
/**
* Get the actions version annotation.
*
* @return \In\Blueprint\Annotation\Versions|null
*/
public function getVersions()
{
if ($annotation = $this->getAnnotationByType('Versions')) {
return $annotation;
}
}
/**
* Get the actions response annotation.
*
* @return \In\Blueprint\Annotation\Response|null
*/
public function getResponse()
{
if ($annotation = $this->getAnnotationByType('Response')) {
return $annotation;
}
}
/**
* Get the actions request annotation.
*
* @return \In\Blueprint\Annotation\Request|null
*/
public function getRequest()
{
if ($annotation = $this->getAnnotationByType('Request')) {
return $annotation;
}
}
/**
* Get the actions transaction annotation.
*
* @return \In\Blueprint\Annotation\Transaction|null
*/
public function getTransaction()
{
if ($annotation = $this->getAnnotationByType('Transaction')) {
return $annotation;
}
}
/**
* Get the actions identifier.
*
* @return string|null
*/
public function getIdentifier()
{
$factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
$docblock = $factory->create($this->reflector);
return $docblock->getSummary();
}
/**
* Get the actions description.
*
* @return string|null
*/
public function getDescription()
{
$factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance();
$docblock = $factory->create($this->reflector);
return $docblock->getDescription();
}
/**
* Get the actions URI.
*
* @return string
*/
public function getUri()
{
$uri = '/';
if (($annotation = $this->getAnnotationByType('Method\Method')) && isset($annotation->uri)) {
$uri = trim($annotation->uri, '/');
} else {
return;
}
if (! starts_with($uri, '{?')) {
$uri = '/'.$uri;
}
return '/'.trim(trim($this->resource->getUri(), '/').rtrim($uri, '/'), '/');
}
/**
* Get the actions method.
*
* @throws \RuntimeException
*
* @return string
*/
public function getMethod()
{
if ($annotation = $this->getAnnotationByType('Method\Method')) {
return strtoupper(substr(get_class($annotation), strrpos(get_class($annotation), '\\') + 1));
}
throw new RuntimeException('No HTTP method given, invalid API blueprint.');
}
/**
* Set the parent resource on the action.
*
* @param \In\Blueprint\Resource $resource
*
* @return void
*/
public function setResource(Resource $resource)
{
$this->resource = $resource;
}
}