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\Controller; |
13
|
|
|
|
14
|
|
|
use CakeDC\Api\Service\ConfigReader; |
15
|
|
|
use CakeDC\Api\Service\ServiceRegistry; |
16
|
|
|
use Cake\Utility\Inflector; |
17
|
|
|
use Exception; |
18
|
|
|
|
19
|
|
|
class ApiController extends AppController |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ServiceRegistry |
24
|
|
|
*/ |
25
|
|
|
public $Services; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Initialize controller. |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function initialize() |
33
|
|
|
{ |
34
|
|
|
parent::initialize(); |
35
|
|
|
if ($this->components()->has('Auth')) { |
36
|
|
|
$this->Auth->allow(['process', 'describe', 'listing']); |
37
|
|
|
} |
38
|
|
|
if ($this->components()->has('RememberMe')) { |
39
|
|
|
$this->components()->unload('RememberMe'); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Process api request |
45
|
|
|
* |
46
|
|
|
* @return \Cake\Http\Client\Response|\Cake\Http\Response|null |
47
|
|
|
*/ |
48
|
|
|
public function process() |
49
|
|
|
{ |
50
|
|
|
return $this->_process(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Process listing api request. |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function listing() |
59
|
|
|
{ |
60
|
|
|
$this->request['service'] = 'listing'; |
61
|
|
|
$options = [ |
62
|
|
|
'className' => 'CakeDC/Api.Listing' |
63
|
|
|
]; |
64
|
|
|
$this->_process($options); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Process describe api request. |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
public function describe() |
73
|
|
|
{ |
74
|
|
|
$this->request['service'] = 'describe'; |
75
|
|
|
$options = [ |
76
|
|
|
'className' => 'CakeDC/Api.Describe' |
77
|
|
|
]; |
78
|
|
|
$this->_process($options); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Process api request |
83
|
|
|
* |
84
|
|
|
* @param array $options Options |
85
|
|
|
* @return \Cake\Http\Client\Response|\Cake\Http\Response|null |
86
|
|
|
*/ |
87
|
|
|
protected function _process($options = []) |
88
|
|
|
{ |
89
|
|
|
$this->autoRender = false; |
90
|
|
|
try { |
91
|
|
|
if (!empty($this->request['service'])) { |
92
|
|
|
$service = $this->request['service']; |
93
|
|
|
$version = null; |
94
|
|
|
if (!empty($this->request['version'])) { |
95
|
|
|
$version = $this->request['version']; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$url = '/' . $service; |
99
|
|
|
if (!empty($this->request->getParam('pass'))) { |
100
|
|
|
$url .= '/' . join('/', $this->request->getParam('pass')); |
101
|
|
|
} |
102
|
|
|
$options += [ |
103
|
|
|
'version' => $version, |
104
|
|
|
'request' => $this->request, |
105
|
|
|
'response' => $this->response, |
106
|
|
|
'baseUrl' => Inflector::underscore($url), |
107
|
|
|
]; |
108
|
|
|
$options += (new ConfigReader())->serviceOptions($service, $version); |
109
|
|
|
$Service = ServiceRegistry::getServiceLocator()->get($service, $options); |
110
|
|
|
$result = $Service->dispatch(); |
111
|
|
|
|
112
|
|
|
return $Service->respond($result); |
113
|
|
|
} |
114
|
|
|
$this->response = $this->response->withStringBody(__('Service not found'))->withStatus(404); |
115
|
|
|
} catch (Exception $e) { |
116
|
|
|
$this->response = $this->response->withStringBody($e->getMessage())->withStatus(400); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->response; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|