Skip to content
This repository was archived by the owner on Oct 16, 2018. It is now read-only.

Commit 091d90b

Browse files
committed
Merge branch 'release/1.0'
2 parents a0038bd + 6062006 commit 091d90b

25 files changed

Lines changed: 1629 additions & 265 deletions

src/Fabs/Rest/APIBase.php

Lines changed: 75 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212
use Fabs\Rest\Constants\HttpHeaders;
1313
use Fabs\Rest\Constants\HttpMethods;
14+
use Fabs\Rest\Constants\PatchOperations;
1415
use Fabs\Rest\Models\MapModel;
16+
use Fabs\Rest\Models\QueryElement;
17+
use Fabs\Rest\Models\SearchQueryModel;
18+
use Fabs\Rest\Services\PatchHandler;
1519
use Fabs\Rest\Services\ServiceBase;
1620
use Phalcon\Mvc\Micro\Collection as MicroCollection;
1721

@@ -30,16 +34,25 @@ abstract class APIBase extends ServiceBase
3034
*/
3135
protected $mapped_functions = [];
3236

37+
/**
38+
* @var PatchHandler
39+
*/
40+
protected $patch_handler = null;
41+
3342
public function __construct()
3443
{
44+
$this->patch_handler = new PatchHandler();
45+
$this->patch_handler->addAllowedOperation(PatchOperations::ADD);
46+
$this->patch_handler->addAllowedOperation(PatchOperations::REMOVE);
47+
$this->patch_handler->addAllowedOperation(PatchOperations::REPLACE);
48+
3549
$this->addAllowedMethod(HttpMethods::GET);
3650
$this->addAllowedMethod(HttpMethods::POST);
3751
$this->addAllowedMethod(HttpMethods::HEAD);
3852
$this->addAllowedMethod(HttpMethods::PUT);
3953
$this->addAllowedMethod(HttpMethods::PATCH);
4054
$this->addAllowedMethod(HttpMethods::DELETE);
4155

42-
4356
$this->map(HttpMethods::GET, '/', 'get');
4457
$this->map(HttpMethods::POST, '/', 'post');
4558

@@ -51,27 +64,12 @@ public function __construct()
5164
$this->collection = new MicroCollection();
5265
$this->collection->setHandler($this);
5366
$this->collection->setPrefix($this->getPrefix());
54-
55-
$this->application->before(function () {
56-
$pattern = $this->router->getMatchedRoute()->getPattern();
57-
if (strpos($pattern, $this->getPrefix()) === 0) {
58-
return $this->before();
59-
}
60-
return true;
61-
});
62-
63-
$this->application->after(function () {
64-
$pattern = $this->router->getMatchedRoute()->getPattern();
65-
if (strpos($pattern, $this->getPrefix()) === 0) {
66-
$this->after();
67-
}
68-
});
6967
}
7068

7169
/**
7270
* @return string
7371
*/
74-
protected abstract function getPrefix();
72+
public abstract function getPrefix();
7573

7674
public function get()
7775
{
@@ -103,67 +101,86 @@ public function head($id)
103101
$this->status_code_handler->methodNotAllowed();
104102
}
105103

106-
protected function map($method, $url, $function_name)
104+
/**
105+
* @param string $method
106+
* @param string $uri
107+
* @param string $function_name
108+
* @return MapModel
109+
*/
110+
protected function map($method, $uri, $function_name)
107111
{
108-
$map = new MapModel();
109-
$map->method_name = $method;
110-
$map->url = $url;
111-
$map->function_name = $function_name;
112+
$map = new MapModel($method, $uri, $function_name);
113+
114+
foreach ($this->mapped_functions as $key => $map_model) {
115+
if ($map_model->getMethodName() == $method && $map_model->getURI() == $uri) {
116+
$this->mapped_functions[$key] = $map;
117+
return $map;
118+
}
119+
}
112120

113121
$this->mapped_functions[] = $map;
122+
return $map;
114123
}
115124

116125
public function mount()
117126
{
118127
foreach ($this->mapped_functions as $map) {
119-
if (method_exists($this->collection, strtolower($map->method_name))) {
120-
if (in_array($map->method_name, $this->allowed_methods, true)) {
121-
call_user_func_array([$this->collection, strtolower($map->method_name)],
122-
[$map->url, $map->function_name]);
128+
if (method_exists($this->collection, $map->getMethodName())) {
129+
if (in_array(strtoupper($map->getMethodName()), $this->allowed_methods, true)) {
130+
call_user_func_array(
131+
[
132+
$this->collection,
133+
$map->getMethodName()
134+
],
135+
[
136+
$map->getURI(),
137+
$map->getFunctionName(),
138+
$this->getPrefix() . $map->getURI()
139+
]
140+
);
123141
} else {
124-
call_user_func_array([$this->collection, strtolower($map->method_name)],
125-
[$map->url, 'methodNotAllowed']);
142+
call_user_func_array(
143+
[
144+
$this->collection,
145+
$map->getMethodName()
146+
],
147+
[
148+
$map->getURI(),
149+
'methodNotAllowed'
150+
]
151+
);
126152
}
127153
}
128154
}
129155

130156
$this->application->mount($this->collection);
131157
}
132158

133-
protected function before()
159+
/**
160+
* @return Models\MapModel[]
161+
*/
162+
public function getMappedFunctions()
134163
{
135-
$this->application->response->setHeader(HttpHeaders::ACCESS_CONTROL_ALLOW_METHODS,
136-
strtoupper(implode(', ', $this->allowed_methods)));
137-
return true;
164+
return $this->mapped_functions;
138165
}
139166

140-
protected function after()
167+
public function awake()
141168
{
142169

143170
}
144171

145-
public function getETag()
172+
public function before()
146173
{
147-
return $this->request->getHeader(HttpHeaders::IF_NONE_MATCH);
174+
$this->application->response->setHeader(
175+
HttpHeaders::ACCESS_CONTROL_ALLOW_METHODS,
176+
strtoupper(implode(', ', $this->allowed_methods))
177+
);
178+
return true;
148179
}
149180

150-
protected function getPage()
181+
public function after()
151182
{
152-
return $this->request->get('page', 'int', 0);
153-
}
154183

155-
protected function getPerPage()
156-
{
157-
return $per_page = $this->request->get('per_page', 'int', 0);
158-
}
159-
160-
/**
161-
* @param $cache_key string
162-
* @return string
163-
*/
164-
protected function makeCacheKey($cache_key)
165-
{
166-
return $cache_key . '_' . $this->getPage() . '_' . $this->getPerPage();
167184
}
168185

169186
/**
@@ -191,4 +208,12 @@ public function methodNotAllowed()
191208
$this->status_code_handler->methodNotAllowed();
192209
}
193210

211+
/**
212+
* @return SearchQueryModel|null
213+
*/
214+
public function getSearchQuery()
215+
{
216+
$search_query = $this->dispatcher->getParam('search_query');
217+
return $search_query;
218+
}
194219
}

0 commit comments

Comments
 (0)