-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
193 lines (164 loc) · 6.39 KB
/
api.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/* --------------------------------------------------------------
api.php 2016-01-20
Gambio GmbH
http://www.gambio.de
Copyright (c) 2016 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
/**
* Gambio GX2 - API (implemented with Slim Framework)
*
* @link http://www.slimframework.com
*
* Hit this file directly with new requests and it will route them to their corresponding API
* controllers. Controller files reside inside the "GXEngine/Controllers/Api" directory and are
* separated by version. This separation enables the addition of newer API versions in the future.
*
* Since v2 the shop API is RESTful and that means that it supports a variety of HTTP methods
* in order to implement a semantic interface for client developers. You can use one of the GET,
* POST, PUT, DELETE, PATCH, HEAD, OPTIONS methods in your controller classes. Check the
* "HttpApiV2Controller" class for more information on how to create your own controller.
*
* @link http://en.wikipedia.org/wiki/Representational_state_transfer
* @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
*
* It is important that each API version is able to route the controllers differently because
* the codebase will be more flexible and easy to maintain. Expand the current file with new
* controller-routing rules for future versions.
*
* You can generate detailed API documentation through ApiDoc. It is a NodeJS command line tool
* that parses specific DocBlock comments and creates rich content output. It's always preferable
* that API methods are well-documented so that is easier for external developers to use them.
*
* @link http://apidocjs.com
*
* Version 2.0.0 of the API uses HTTP Basic Authentication and that means that authorization
* credentials are transferred over the wire. Always use HTTPS when accessing the API.
*
* http://en.wikipedia.org/wiki/Basic_access_authentication
*/
// ----------------------------------------------------------------------------
// INITIALIZE API - SLIM FRAMEWORK
// ----------------------------------------------------------------------------
/**
* API Version
*
* The current API version will be included within every response in the "X-API-Version" header so that
* clients know which exact version they are using.
*
* @var string
*/
$version = '2.2.0';
/**
* API Environment
*
* If the ".dev-environment" file is present it will override the API_V2_ENVIRONMENT value and
* it will set the environment back to testing ('development' is only suitable for complete error display).
*
* @var string
*/
$environment = file_exists(__DIR__ . '/.dev-environment') ? 'test' : 'production';
switch($environment)
{
case 'development': // Complete verbose (HTML) output when errors occur.
$config = array(
'mode' => 'development',
'debug' => true
);
break;
case 'test': // Includes PHP errors in the response body (stack trace).
$config = array(
'mode' => 'test',
'debug' => false
);
break;
case 'production': // Will display error info in JSON format but hide extra information.
$config = array(
'mode' => 'production',
'debug' => false
);
break;
default:
throw new Exception('Invalid APIv2 environment selected: ' . $environment);
}
$config['version'] = $version;
require __DIR__ . '/includes/application_top.php';
$api = new \Slim\Slim($config);
// ----------------------------------------------------------------------------
// CONTROLLER ROUTING FOR V2
// ----------------------------------------------------------------------------
$api->map('/v2(/:uri+)', function ($uri = array()) use ($api)
{
$resourceName = explode('_', ucfirst($uri[0]));
foreach($resourceName as &$section)
{
$section = ucfirst($section);
}
$resourceName = implode('', $resourceName);
$controllerName = (!empty($uri)) ? $resourceName . 'ApiV2Controller' : HttpApiV2Controller::DEFAULT_CONTROLLER_NAME;
// Check if the resource exists (there is no such method in MainFactory so we use the autoloader function).
if(!class_exists($controllerName))
{
throw new HttpApiV2Exception('Resource not found.', 404);
}
$controller = MainFactory::create($controllerName, $api, $uri);
$method = strtolower($api->request->getMethod());
$resource = array($controller, $method);
if(!is_callable($resource))
{
throw new HttpApiV2Exception('The requested resource is not supported by the API v2.', 405);
}
call_user_func($resource);
})->via('GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'); // Supported request methods
// ----------------------------------------------------------------------------
// API ERROR HANDLING
// ----------------------------------------------------------------------------
$api->error(function (\Exception $ex) use ($api)
{
$responseErrorCode = 500; // The default value for exceptions on server.
if(is_a($ex, 'HttpApiV2Exception')) // An HttpApiException will contain a specific HTTP status code.
{
$responseErrorCode = $ex->getCode();
}
$api->response->setStatus($responseErrorCode);
$api->response->headers->set('Content-Type', 'application/json');
$response = array(
'code' => $ex->getCode(),
'status' => 'error',
'message' => $ex->getMessage(),
'request' => array(
'method' => $api->request->getMethod(),
'url' => $api->request->getUrl(),
'path' => $api->request->getPath(),
'uri' => array(
'root' => $api->request->getRootUri(),
'resource' => $api->request->getResourceUri()
)
)
);
// Provide error stack only in 'test' mode.
if($api->config('mode') === 'test')
{
$response['error'] = array(
'file' => $ex->getFile(),
'line' => $ex->getLine(),
'stack' => $ex->getTrace()
);
}
if(defined(JSON_PRETTY_PRINT) && defined(JSON_UNESCAPED_SLASHES))
{
$responseJsonString = json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
else
{
$responseJsonString = json_encode($response); // PHP v5.3
}
$api->response->write($responseJsonString);
});
// ----------------------------------------------------------------------------
// API EXECUTION
// ----------------------------------------------------------------------------
$api->run();