1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Service; |
13
|
|
|
|
14
|
|
|
use Cake\Utility\Inflector; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class CrudService |
18
|
|
|
* |
19
|
|
|
* @package CakeDC\Api\Service |
20
|
|
|
*/ |
21
|
|
|
abstract class CrudService extends Service |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Actions classes map. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $_actionsClassMap = [ |
30
|
|
|
'describe' => '\CakeDC\Api\Service\Action\CrudDescribeAction', |
31
|
|
|
'index' => '\CakeDC\Api\Service\Action\CrudIndexAction', |
32
|
|
|
'view' => '\CakeDC\Api\Service\Action\CrudViewAction', |
33
|
|
|
'add' => '\CakeDC\Api\Service\Action\CrudAddAction', |
34
|
|
|
'edit' => '\CakeDC\Api\Service\Action\CrudEditAction', |
35
|
|
|
'delete' => '\CakeDC\Api\Service\Action\CrudDeleteAction', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Table name. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $_table = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Id param name. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $_idName = 'id'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* CrudService constructor. |
54
|
|
|
* |
55
|
|
|
* @param array $config Service configuration. |
56
|
|
|
*/ |
57
|
130 |
|
public function __construct(array $config = []) |
58
|
|
|
{ |
59
|
130 |
|
parent::__construct($config); |
60
|
130 |
|
if (isset($config['table'])) { |
61
|
|
|
$this->setTable($config['table']); |
62
|
|
|
} else { |
63
|
130 |
|
$this->setTable(Inflector::camelize($this->getName())); |
64
|
|
|
} |
65
|
130 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets a Table name. |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
113 |
|
public function getTable() |
73
|
|
|
{ |
74
|
113 |
|
return $this->_table; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Sets the table instance. |
79
|
|
|
* |
80
|
|
|
* @param string $table A Table name. |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
130 |
|
public function setTable($table) |
84
|
|
|
{ |
85
|
130 |
|
$this->_table = $table; |
86
|
|
|
|
87
|
130 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Api method for table. |
92
|
|
|
* |
93
|
|
|
* @param string $table A Table name. |
94
|
|
|
* @deprecated 3.4.0 Use setTable()/getTable() instead. |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function table($table = null) |
98
|
|
|
{ |
99
|
|
|
deprecationWarning( |
100
|
|
|
'Service::table() is deprecated. ' . |
101
|
|
|
'Use Service::setTable()/getTable() instead.' |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
if ($table !== null) { |
105
|
|
|
return $this->setTable($table); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $this->getTable(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Action constructor options. |
113
|
|
|
* |
114
|
|
|
* @param array $route Activated route. |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
55 |
|
protected function _actionOptions($route) |
118
|
|
|
{ |
119
|
55 |
|
$id = null; |
120
|
55 |
|
if (isset($route[$this->_idName])) { |
121
|
17 |
|
$id = $route[$this->_idName]; |
122
|
17 |
|
} |
123
|
|
|
|
124
|
55 |
|
return parent::_actionOptions($route) + [ |
125
|
55 |
|
'id' => $id, |
126
|
55 |
|
'idName' => $this->_idName, |
127
|
55 |
|
]; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|