-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRestful.php
162 lines (145 loc) · 5.01 KB
/
Restful.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
<?php
/**
* Copyright (c) 2017. Alexandr Kosarev, @kosarev.by
*/
/**
* Created by PhpStorm.
* User: Alexandr
* Date: 13.12.2017
* Time: 15:30
*/
namespace Joomplace\X\Helper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomplace\X\Model;
use Joomplace\X\View;
trait Restful
{
use Injector;
public function execute($task){
$method = strtoupper($this->injectArg('httpxmethod',$_SERVER['REQUEST_METHOD']));
$methodMap = [
'store'=>'POST',
'update'=>'PUT',
'destroy'=>'DELETE',
'show' => 'GET',
'index' => 'GET',
];
if(!$task){
switch ($method){
case 'GET':
if($this->injectArg('id')){
$task = 'show';
}else{
$task = 'index';
}
break;
default:
$task = array_search($method,$methodMap);
}
}else{
if(array_key_exists($task,$methodMap) && $methodMap[$task]!=$method){
throw new \Exception("Incorrect method. Expecting '$methodMap[$task]', and got '$method'.", 500);
}
}
if(!$this->methodDefined($task)){
if(array_key_exists($task,$methodMap) || in_array($task,array('create','edit'))){
$this->taskMap[$task] = 'trait'.ucfirst($task);
}
}
return parent::execute($task);
}
public function traitIndex(){
/** @var Model $modelClass */
$modelClass = $this->getModel();
$view = $this->getView();
$view->items = $modelClass::accessible()->paginate($this->input->get('limit',Factory::getConfig()->get('list_limit',
20)));
return $view->render();
}
public function traitShow($id){
/** @var Model $modelClass */
$modelClass = $this->getModel();
$view = $this->getView();
/** @var Model $item */
$item = $modelClass::findOrFail($id);
if($modelClass::can('view',$item)){
$view->item = $item;
return $view->render();
}else{
throw new \Exception(Text::_('JOOMPLACE_X_NOT_ALLOWED'), 403);
}
}
public function traitCreate(){
/** @var View $view */
$view = $this->getView();
$view->setLayout('create');
/** @var Model $modelClass */
$modelClass = $this->getModel();
if($modelClass::can('create')){
return $view->render();
}else{
throw new \Exception(Text::_('JOOMPLACE_X_NOT_ALLOWED'), 403);
}
}
public function traitEdit($id){
/** @var Model $modelClass */
$modelClass = $this->getModel();
$view = $this->getView();
$view->setLayout('edit');
/** @var Model $item */
$item = $modelClass::findOrFail($id);
if($modelClass::can('edit',$item)){
$view->item = $item;
return $view->render();
}else{
throw new \Exception(Text::_('JOOMPLACE_X_NOT_ALLOWED'), 403);
}
}
public function traitStore(){
/** @var Model $modelClass */
$modelClass = $this->getModel();
/** @var Model $item */
if($modelClass::can('create',new $modelClass)){
/** @var Model $item */
$item = $modelClass::create($modelClass::getFillFromInput($this->getInput()));
$option = $this->injectArg('option');
$this->app->enqueueMessage(Text::sprintf(strtoupper($option).'_CREATED',$item->id));
$view = $this->getView();
return $view->stored($item);
}else{
throw new \Exception(Text::_('JOOMPLACE_X_NOT_ALLOWED'), 403);
}
}
public function traitUpdate($id){
/** @var Model $modelClass */
$modelClass = $this->getModel();
/** @var Model $item */
$item = $modelClass::findOrFail($id);
if($modelClass::can('update',$item)){
$item->fill($modelClass::getFillFromInput($this->getInput()));
$item->saveOrFail();
$option = $this->injectArg('option');
$this->app->enqueueMessage(Text::sprintf(strtoupper($option).'_UPDATED',$item->id));
$view = $this->getView();
return $view->updated($item);
}else{
throw new \Exception(Text::_('JOOMPLACE_X_NOT_ALLOWED'), 403);
}
}
public function traitDestroy($id){
/** @var Model $modelClass */
$modelClass = $this->getModel();
/** @var Model $item */
$item = $modelClass::findOrFail($id);
if($modelClass::can('delete',$item)){
$item->delete();
$option = $this->injectArg('option');
$this->app->enqueueMessage(Text::sprintf(strtoupper($option).'_DELETED',$item->id));
$view = $this->getView();
return $view->destroyed($item);
}else{
throw new \Exception(Text::_('JOOMPLACE_X_NOT_ALLOWED'), 403);
}
}
}