1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2016 - 2017, 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 - 2017, 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
|
|
|
if ($table !== null) { |
100
|
|
|
return $this->setTable($table); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->getTable(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Action constructor options. |
108
|
|
|
* |
109
|
|
|
* @param array $route Activated route. |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
55 |
|
protected function _actionOptions($route) |
113
|
|
|
{ |
114
|
55 |
|
$id = null; |
115
|
55 |
|
if (isset($route[$this->_idName])) { |
116
|
17 |
|
$id = $route[$this->_idName]; |
117
|
17 |
|
} |
118
|
|
|
|
119
|
55 |
|
return parent::_actionOptions($route) + [ |
120
|
55 |
|
'id' => $id, |
121
|
55 |
|
'idName' => $this->_idName, |
122
|
55 |
|
]; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.