Skip to content

Commit 45791b5

Browse files
author
Daniel Khalil
committed
Added generic API controller
1 parent 90c1732 commit 45791b5

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed

lib/Bacon/Controllers/Api.php

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
3+
/**
4+
Copyright 2012-2013 Brainsware
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
*/
19+
20+
namespace Bacon\Controllers;
21+
22+
class Api extends \Bacon\Controller
23+
{
24+
use \Bacon\Traits\Pagination;
25+
26+
protected $model = '';
27+
protected $per_page = 100;
28+
29+
protected $allowed_fields = [
30+
];
31+
32+
protected $sortable = null;
33+
34+
public function init ()
35+
{
36+
/* Check whether given request is from within /backend.
37+
* Deny (send 404) for all non-GET requests outside of /backend. */
38+
if ($this->environment->request_method !== 'GET' &&
39+
!$this->check_key()) {
40+
41+
return $this->http_status(403);
42+
}
43+
44+
if (!empty($this->belongs_to)) {
45+
try {
46+
$model = $this->belongs_to['model'];
47+
$this->parent_model = $model::find($this->params[$this->belongs_to['param']]);
48+
49+
} catch (\PDOException $e) {
50+
return $this->http_status(404);
51+
}
52+
}
53+
54+
return parent::init();
55+
}
56+
57+
protected function check_key ()
58+
{
59+
return true;
60+
}
61+
62+
protected function referer_uri ()
63+
{
64+
$referer = S($this->environment->http_referer);
65+
66+
$referer->replaceF('http://', '');
67+
$referer->replaceF('https://', '');
68+
$referer->replaceF($this->environment->http_host, '');
69+
70+
return $referer;
71+
}
72+
73+
public function index ()
74+
{
75+
$options = A([
76+
'order_by' => (empty($this->sortable)) ? 'id' : $this->sortable,
77+
'order' => 'desc',
78+
'per_page' => $this->per_page,
79+
'page' => 0
80+
])->mergeF($this->params);
81+
82+
$this->paginate($options->per_page);
83+
84+
$where = [];
85+
86+
if (!empty($this->params->status)) {
87+
$where['status'] = $this->params->status;
88+
}
89+
90+
if (!empty($this->belongs_to)) {
91+
$where[$this->belongs_to['key']] = $this->parent_model->id;
92+
}
93+
94+
$model = $this->model;
95+
96+
return $this->json(
97+
$model::where($where)
98+
->order($options->order_by, $options->order)
99+
->page($options->page, $options->per_page)->all()
100+
);
101+
}
102+
103+
public function show ()
104+
{
105+
$model = $this->model;
106+
107+
try {
108+
$data = $model::find($this->params->id);
109+
110+
return $this->json($data);
111+
112+
} catch (\PDOException $e) {
113+
$this->log->warning($e->getMessage());
114+
115+
return $this->http_status(400);
116+
}
117+
}
118+
119+
public function create ()
120+
{
121+
$model = $this->model;
122+
123+
try {
124+
$data = new $model();
125+
126+
foreach ($this->allowed_fields as $field) {
127+
if ($this->params->has_key($field)) {
128+
$data->$field = $this->params[$field];
129+
}
130+
}
131+
132+
if (!empty($this->belongs_to)) {
133+
$key = $this->belongs_to['key'];
134+
$data->$key = $this->parent_model->id;
135+
}
136+
137+
$data->save();
138+
139+
$data = $model::find($data->id);
140+
} catch (\PDOException $e) {
141+
$this->log->warning($e->getMessage());
142+
143+
return $this->http_status(400);
144+
}
145+
146+
return $this->json($data);
147+
}
148+
149+
public function update ()
150+
{
151+
$model = $this->model;
152+
153+
try {
154+
$this->data = $model::find($this->params->id);
155+
} catch (\PDOException $e) {
156+
$this->log->warning($e->getMessage());
157+
158+
return $this->http_status(400);
159+
}
160+
161+
foreach ($this->allowed_fields as $field) {
162+
if ($this->params->has_key($field)) {
163+
if (!empty($this->sortable) && $field == $this->sortable) {
164+
$this->data->move($this->params[$field]);
165+
} else {
166+
$this->data->$field = $this->params[$field];
167+
}
168+
}
169+
}
170+
171+
try {
172+
$this->data->save();
173+
} catch (\PDOException $e) {
174+
$this->log->warning($e->getMessage());
175+
176+
return $this->http_status(400);
177+
}
178+
179+
return $this->json($this->data);
180+
}
181+
182+
public function destroy ()
183+
{
184+
$model = $this->model;
185+
186+
try {
187+
$this->data = $model::find($this->params->id);
188+
189+
$this->data->delete();
190+
} catch (\PDOException $e) {
191+
$this->log->warning($e->getMessage());
192+
193+
return $this->http_status(400);
194+
}
195+
196+
return $this->json([]);
197+
}
198+
}
199+
200+
?>

0 commit comments

Comments
 (0)