|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the Apix Project. |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
|
8
|
|
|
* |
|
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Apix\Resource; |
|
14
|
|
|
|
|
15
|
|
|
use Apix\Entity, |
|
16
|
|
|
Apix\Server, |
|
17
|
|
|
Apix\Request, |
|
18
|
|
|
Apix\Router, |
|
19
|
|
|
Apix\View\ViewModel; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Help |
|
23
|
|
|
* This resource entity provides in-line referencial of all the API resources. |
|
24
|
|
|
*/ |
|
25
|
|
|
class Help |
|
26
|
|
|
{ |
|
27
|
|
|
// only use in verbose mode. |
|
28
|
|
|
public $private_nodeName = 'verbose'; |
|
29
|
|
|
|
|
30
|
|
|
private $rel_path = '/help'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Help - Provides in-line referencial to this API. |
|
34
|
|
|
* |
|
35
|
|
|
* This resource entity provides documentation and in-line referencial to this API's resources. |
|
36
|
|
|
* By specify a resource and method you can narrow down to specific section. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $resource Optional. A string of characters used to identify |
|
39
|
|
|
* a specific resource entity. |
|
40
|
|
|
* e.g. /help/resource |
|
41
|
|
|
* @param array $filters Optional. Some filters to narrow down the resultset. |
|
42
|
|
|
* @global string $method Default "GET". The resource HTTP method to interact. |
|
43
|
|
|
* @return array An array documentating either all the available resources |
|
44
|
|
|
* or if provided, the specified <b>:resource</b>. |
|
45
|
|
|
* |
|
46
|
|
|
* @example <p><pre>apixs: |
|
47
|
|
|
* help: |
|
48
|
|
|
* title: Help - Provides in-line referencial to this API. |
|
49
|
|
|
* description: This resource entity provides documentation and ... |
|
50
|
|
|
* </pre></p> |
|
51
|
|
|
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.2 |
|
52
|
|
|
*/ |
|
53
|
|
|
public function onRead($resource=null, array $filters=null) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$server = \Apix\Service::get('server'); |
|
56
|
|
|
$resource = preg_replace( |
|
57
|
|
|
'@^(.*?)' . preg_quote($this->rel_path) . '(\.\w+)?@', |
|
58
|
|
|
'', |
|
59
|
|
|
$server->request->getUri() |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
// $resource = rawurldecode($resource); |
|
|
|
|
|
|
63
|
|
|
// if (!$server->resources->has($resource)) { |
|
|
|
|
|
|
64
|
|
|
// return array(); |
|
|
|
|
|
|
65
|
|
|
// } |
|
66
|
|
|
|
|
67
|
|
|
// if ( |
|
68
|
|
|
// !empty($resource) |
|
|
|
|
|
|
69
|
|
|
// && $server->resources->has($resource) |
|
|
|
|
|
|
70
|
|
|
// ) { |
|
71
|
|
|
$server->getRoute()->setName($resource); |
|
72
|
|
|
// } |
|
73
|
|
|
return $this->onHelp($server, $filters); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Outputs info for a resource entity. |
|
78
|
|
|
* |
|
79
|
|
|
* The OPTIONS method represents a request for information about the |
|
80
|
|
|
* communication options available on the request/response chain |
|
81
|
|
|
* identified by the Request-URI. This method allows the client to determine |
|
82
|
|
|
* the options and/or requirements associated with a resource, |
|
83
|
|
|
* or the capabilities of a server, without implying a resource action or |
|
84
|
|
|
* initiating a resource retrieval. |
|
85
|
|
|
* |
|
86
|
|
|
* |
|
87
|
|
|
* @param Server $server The main server object. |
|
88
|
|
|
* @param array $filters An array of filters. |
|
89
|
|
|
* @return array An array of documentation data. |
|
90
|
|
|
* |
|
91
|
|
|
* @api_link OPTIONS /path/to/entity |
|
92
|
|
|
* @api_link OPTIONS /* |
|
93
|
|
|
* @apix_man_toc_hidden |
|
94
|
|
|
*/ |
|
95
|
|
|
public function onHelp(Server $server, array $filters=null) |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
$route = $server->getRoute(); |
|
98
|
|
|
|
|
99
|
|
|
// insures the top node is set to help. |
|
100
|
|
|
$route->setController('help'); |
|
101
|
|
|
|
|
102
|
|
|
$entity = $route->getName() != '/' && $route->getName() != '/*' |
|
103
|
|
|
&& $route->getPath() != '/help' |
|
104
|
|
|
? $server->resources->get($route, false) |
|
105
|
|
|
: null; |
|
106
|
|
|
|
|
107
|
|
|
// TOC of all entities. |
|
108
|
|
|
if (null === $entity) { |
|
109
|
|
|
|
|
110
|
|
|
$docs = array( |
|
111
|
|
|
'items' => self::getResourcesDocs($server) |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
// // set Content-Type (negotiate or default) |
|
115
|
|
|
// if( |
|
116
|
|
|
// $request->hasHeader('CONTENT_LENGTH') |
|
|
|
|
|
|
117
|
|
|
// || $request->hasHeader('TRANSFER_ENCODING') |
|
|
|
|
|
|
118
|
|
|
// ) { |
|
119
|
|
|
// // @expect client request has an entity-body (indicated by Content-Length or Transfer-Encoding) then client's Content-Type must be set. |
|
120
|
|
|
// return array('doc'=>'Todo: return all the resource doc as per CONTENT_LENGTH and/or TRANSFER_ENCODING'); |
|
|
|
|
|
|
121
|
|
|
// } |
|
122
|
|
|
|
|
123
|
|
|
// Manual for the specified entity doc. |
|
124
|
|
|
} else { |
|
125
|
|
|
$docs = self::getEntityDocs( |
|
126
|
|
|
$entity, |
|
127
|
|
|
$server->request->getParam('method') ?: 'GET', |
|
128
|
|
|
$route->getName() |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $docs; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get the documentation of all the resource entities. |
|
137
|
|
|
* |
|
138
|
|
|
* @param Server $server |
|
139
|
|
|
* @return array The documentation for all resource entities. |
|
140
|
|
|
*/ |
|
141
|
|
|
public static function getResourcesDocs(Server $server) |
|
142
|
|
|
{ |
|
143
|
|
|
$docs = array(); |
|
144
|
|
|
$redir = array(); |
|
145
|
|
|
$resources = $server->resources->toArray(); |
|
146
|
|
|
ksort($resources); |
|
147
|
|
|
foreach ($resources as $path => $entity) { |
|
148
|
|
|
if (!$entity->hasRedirect()) { |
|
149
|
|
|
$item = self::getEntityDocs($entity, null, $path); |
|
150
|
|
|
$item['path'] = isset($redir[$path]) |
|
151
|
|
|
? $redir[$path] |
|
152
|
|
|
: $path; |
|
153
|
|
|
$docs[] = $item; |
|
154
|
|
|
} else { |
|
155
|
|
|
$redir[$entity->getRedirect()] = $path; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $docs; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get the documentation for the provided entity and method. |
|
164
|
|
|
* |
|
165
|
|
|
* @param EntityInterface $entity The Entity object to interact with. |
|
166
|
|
|
* @param string|null $method Optional. The Request-method or all. |
|
167
|
|
|
* @param string|null $path Optional. Request-URI for that entity. |
|
168
|
|
|
* @return array The entity array documentation. |
|
169
|
|
|
*/ |
|
170
|
|
|
public static function getEntityDocs(Entity $entity, $method=null, $path=null) |
|
171
|
|
|
{ |
|
172
|
|
|
$docs = $entity->getDocs($method); |
|
173
|
|
|
// $docs = (null !== $method || $entity->hasMethod($method)) |
|
|
|
|
|
|
174
|
|
|
// ? $entity->getDocs($method) // get the specified doc method. |
|
|
|
|
|
|
175
|
|
|
// : $entity->getDocs(); // get all the docs for all methods |
|
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
$docs['path'] = $path; |
|
178
|
|
|
|
|
179
|
|
|
return $docs; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.