|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
require_once __DIR__ . '/vendor/autoload.php'; |
|
4
|
|
|
|
|
5
|
|
|
use Battis\SharedLogs\Database\Bindings\DevicesBinding; |
|
6
|
|
|
use Battis\SharedLogs\Database\Bindings\EntriesBinding; |
|
7
|
|
|
use Battis\SharedLogs\Database\Bindings\LogsBinding; |
|
8
|
|
|
use Battis\SharedLogs\Database\Bindings\UsersBinding; |
|
9
|
|
|
use Battis\SharedLogs\Objects\User; |
|
10
|
|
|
use Slim\App; |
|
11
|
|
|
use Slim\Handlers\Strategies\RequestResponseArgs; |
|
12
|
|
|
use Slim\Http\Request; |
|
13
|
|
|
use Slim\Http\Response; |
|
14
|
|
|
|
|
15
|
|
|
define('id_PATTERN', '/{id:[0-9]+}'); |
|
16
|
|
|
|
|
17
|
|
|
$config = json_decode(file_get_contents('config.json'), true); |
|
18
|
|
|
$app = new App(['settings' => $config]); |
|
19
|
|
|
|
|
20
|
|
|
/* register dependencies */ |
|
21
|
|
|
$container = $app->getContainer(); |
|
22
|
|
|
|
|
23
|
|
|
/* |
|
24
|
|
|
* show errors |
|
25
|
|
|
* TODO Handle database errors more transparently |
|
26
|
|
|
* FIXME disable in production! |
|
27
|
|
|
*/ |
|
28
|
|
|
$container['settings']['displayErrorDetails'] = true; |
|
29
|
|
|
|
|
30
|
|
|
/* database with PDO */ |
|
31
|
|
|
$container['pdo'] = function ($c) { |
|
32
|
|
|
$settings = $c['settings']['database']; |
|
33
|
|
|
$pdo = new PDO($settings['dsn'], $settings['user'], $settings['password']); |
|
34
|
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
35
|
|
|
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); |
|
36
|
|
|
return $pdo; |
|
37
|
|
|
}; |
|
38
|
|
|
|
|
39
|
|
|
/* placeholders as separate arguments */ |
|
40
|
|
|
$container['foundHandler'] = function () { |
|
41
|
|
|
return new RequestResponseArgs(); |
|
42
|
|
|
}; |
|
43
|
|
|
|
|
44
|
|
|
/* prepare bindings */ |
|
45
|
|
|
$container['devices'] = function ($c) { |
|
46
|
|
|
return new DevicesBinding($c->pdo); |
|
47
|
|
|
}; |
|
48
|
|
|
$container['logs'] = function ($c) { |
|
49
|
|
|
return new LogsBinding($c->pdo); |
|
50
|
|
|
}; |
|
51
|
|
|
$container['entries'] = function ($c) { |
|
52
|
|
|
return new EntriesBinding($c->pdo); |
|
53
|
|
|
}; |
|
54
|
|
|
$container['users'] = function ($c) { |
|
55
|
|
|
return new UsersBinding($c->pdo); |
|
56
|
|
|
}; |
|
57
|
|
|
|
|
58
|
|
|
$container['cors'] = function ($c) { |
|
59
|
|
|
return [ |
|
60
|
|
|
'allow-origin' => (empty($c['settings']['cors']['allow-origin']) |
|
61
|
|
|
? ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['SERVER_NAME'] |
|
62
|
|
|
: $c['settings']['cors']['allow-origin'] |
|
63
|
|
|
), |
|
64
|
|
|
'allow-headers' => (empty($c['settings']['cors']['allow-headers']) |
|
65
|
|
|
? 'X-Requested-With, Content-Type, Accept, Origin, Authorization' |
|
66
|
|
|
: $c['settings']['cors']['allow-headers'] |
|
67
|
|
|
), |
|
68
|
|
|
'allow-methods' => (empty($c['settings']['cors']['allow-methods']) |
|
69
|
|
|
? 'GET, POST, PUT, DELETE, OPTIONS' |
|
70
|
|
|
: $c['settings']['cors']['allow-headers'] |
|
71
|
|
|
) |
|
72
|
|
|
]; |
|
73
|
|
|
}; |
|
74
|
|
|
|
|
75
|
|
|
$apiPrefix = $container['settings']['api']['prefix']; |
|
76
|
|
|
|
|
77
|
|
|
/* "lazy CORS" */ |
|
78
|
|
|
$app->options($apiPrefix . '/{routes:.+}', function ($request, $response, $args) { |
|
|
|
|
|
|
79
|
|
|
return $response; |
|
80
|
|
|
}); |
|
81
|
|
|
|
|
82
|
|
|
$app->add(function (Request $req, Response $res, callable $next) { |
|
83
|
|
|
$response = $next($req, $res); |
|
84
|
|
|
return $response |
|
85
|
|
|
->withHeader('Access-Control-Allow-Origin', $this->cors['allow-origin']) |
|
|
|
|
|
|
86
|
|
|
->withHeader('Access-Control-Allow-Headers', $this->cors['allow-headers']) |
|
87
|
|
|
->withHeader('Access-Control-Allow-Methods', $this->cors['allow-methods']); |
|
88
|
|
|
}); |
|
89
|
|
|
|
|
90
|
|
|
function callWithNonEmptyParams(callable $method, ...$params) |
|
91
|
|
|
{ |
|
92
|
|
|
return $method(...array_filter($params, function ($param) { |
|
93
|
|
|
return !empty($param); |
|
94
|
|
|
})); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/* |
|
98
|
|
|
* define routes |
|
99
|
|
|
*/ |
|
100
|
|
|
$app->group($apiPrefix . '/devices', function () { |
|
101
|
|
|
$this->post('', function (Request $request, Response $response) { |
|
|
|
|
|
|
102
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->devices, 'create'], $request->getParsedBody(), $request->getParams())); |
|
|
|
|
|
|
103
|
|
|
}); |
|
104
|
|
|
$this->get('', function (Request $request, Response $response) { |
|
105
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->devices, 'all'], $request->getParams())); |
|
|
|
|
|
|
106
|
|
|
}); |
|
107
|
|
|
$this->get(id_PATTERN, function (Request $request, Response $response, $id) { |
|
108
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->devices, 'get'], $id, $request->getParams())); |
|
|
|
|
|
|
109
|
|
|
}); |
|
110
|
|
|
$this->put(id_PATTERN, function (Request $request, Response $response, $id) { |
|
111
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->devices, 'update'], $id, $request->getParams())); |
|
|
|
|
|
|
112
|
|
|
}); |
|
113
|
|
|
$this->delete(id_PATTERN, function (Request $request, Response $response, $id) { |
|
114
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->devices, 'delete'], $id, $request->getParams())); |
|
|
|
|
|
|
115
|
|
|
}); |
|
116
|
|
|
$this->get(id_PATTERN . '/logs', function (Request $request, Response $response, $id) { |
|
117
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->logs, 'listByDevice'], $id, $request->getParams())); |
|
|
|
|
|
|
118
|
|
|
}); |
|
119
|
|
|
}); |
|
120
|
|
|
$app->group($apiPrefix . '/logs', function () { |
|
121
|
|
|
$this->post('', function (Request $request, Response $response) { |
|
|
|
|
|
|
122
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->logs, 'create'], $request->getParsedBody(), $request->getParams())); |
|
|
|
|
|
|
123
|
|
|
}); |
|
124
|
|
|
$this->get('', function (Request $request, Response $response) { |
|
125
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->logs, 'all'], $request->getParams())); |
|
|
|
|
|
|
126
|
|
|
}); |
|
127
|
|
|
$this->get(id_PATTERN, function (Request $request, Response $response, $id) { |
|
128
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->logs, 'get'], $id, $request->getParams())); |
|
|
|
|
|
|
129
|
|
|
}); |
|
130
|
|
|
$this->put(id_PATTERN, function (Request $request, Response $response, $id) { |
|
131
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->logs, 'update'], $id, $request->getParams())); |
|
|
|
|
|
|
132
|
|
|
}); |
|
133
|
|
|
$this->delete(id_PATTERN, function (Request $request, Response $response, $id) { |
|
134
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->logs, 'delete'], $id, $request->getParams())); |
|
|
|
|
|
|
135
|
|
|
}); |
|
136
|
|
|
$this->get(id_PATTERN . '/entries', function (Request $request, Response $response, $id) { |
|
137
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->entries, 'listByLog'], $id, $request->getParams())); |
|
|
|
|
|
|
138
|
|
|
}); |
|
139
|
|
|
}); |
|
140
|
|
|
$app->group($apiPrefix . '/entries', function () { |
|
141
|
|
|
$this->post('', function (Request $request, Response $response) { |
|
|
|
|
|
|
142
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->entries, 'create'], $request->getParsedBody(), $request->getParams())); |
|
|
|
|
|
|
143
|
|
|
}); |
|
144
|
|
|
$this->get(id_PATTERN, function (Request $request, Response $response, $id) { |
|
145
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->entries, 'get'], $id, $request->getParams())); |
|
|
|
|
|
|
146
|
|
|
}); |
|
147
|
|
|
$this->put(id_PATTERN, function (Request $request, Response $response, $id) { |
|
148
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->entries, 'update'], $id, $request->getParams())); |
|
|
|
|
|
|
149
|
|
|
}); |
|
150
|
|
|
$this->delete(id_PATTERN, function (Request $request, Response $response, $id) { |
|
151
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->entries, 'delete'], $id, $request->getParams())); |
|
|
|
|
|
|
152
|
|
|
}); |
|
153
|
|
|
}); |
|
154
|
|
|
$app->group($apiPrefix . '/users', function () { |
|
155
|
|
|
$this->post('', function (Request $request, Response $response) { |
|
|
|
|
|
|
156
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->users, 'create'], $request->getParsedBody(), $request->getParams())); |
|
|
|
|
|
|
157
|
|
|
}); |
|
158
|
|
|
$this->get('', function (Request $request, Response $response) { |
|
159
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->users, 'all'], $request->getParams())); |
|
|
|
|
|
|
160
|
|
|
}); |
|
161
|
|
|
$this->get(id_PATTERN, function (Request $request, Response $response, $id) { |
|
162
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->users, 'get'], $id, $request->getParams())); |
|
|
|
|
|
|
163
|
|
|
}); |
|
164
|
|
|
$this->get('/{screen_name:\w{' . User::SCREEN_NAME_MINIMUM_LENGTH . ',}}', function (Request $request, Response $response, $screen_name) { |
|
165
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->users, 'lookupByScreenName'], $screen_name, $request->getParams())); |
|
|
|
|
|
|
166
|
|
|
}); |
|
167
|
|
|
$this->put(id_PATTERN, function (Request $request, Response $response, $id) { |
|
168
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->users, 'update'], $id, $request->getParams())); |
|
|
|
|
|
|
169
|
|
|
}); |
|
170
|
|
|
$this->delete(id_PATTERN, function (Request $request, Response $response, $id) { |
|
171
|
|
|
return $response->withJson(callWithNonEmptyParams([$this->users, 'delete'], $id, $request->getParams())); |
|
|
|
|
|
|
172
|
|
|
}); |
|
173
|
|
|
}); |
|
174
|
|
|
|
|
175
|
|
|
/* finish lazy CORS */ |
|
176
|
|
|
$app->map(['GET', 'POST', 'PUT', 'DELETE'], $apiPrefix . '/{routes:.+}', function ($req, $res) { |
|
177
|
|
|
$handler = $this->notFoundHandler; |
|
|
|
|
|
|
178
|
|
|
return $handler($req, $res); |
|
179
|
|
|
}); |
|
180
|
|
|
|
|
181
|
|
|
$app->run(); |
|
182
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.