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