-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.php
More file actions
62 lines (50 loc) · 1.95 KB
/
Copy pathsettings.php
File metadata and controls
62 lines (50 loc) · 1.95 KB
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
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$env = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : 'local';
// batch
// APP_ENV=local php ./webroot/index.php 'commandName'
if (isset($GLOBALS['argv'][1])) {
$commandName = $GLOBALS['argv'][1];
$configs[$env]['environment'] = \Slim\Http\Environment::mock(
[
'REQUEST_URI' => '/' . $commandName,
]
);
}
$app = new \Slim\App($configs[$env]);
$container = $app->getContainer();
// 404 Not Found
$container['notFoundHandler'] = function ($c) {
return function (Request $request, Response $response) use ($c) : Response {
return $c['response']
->withJson(['code' => 404, 'message' => 'Not Found'], 404);
};
};
// 405 Not Allowed
$container['notAllowedHandler'] = function ($c) {
return function (Request $request, Response $response, array $methods) use ($c) : Response {
$c->get('logger')->error('ERROR(405): requested method was not in ' . implode(', ', $methods));
return $c['response']
->withJson(['code' => 405, 'message' => 'Not Allowed'], 405);
};
};
// 500 Internal Server Error
$container['errorHandler'] = function ($c) {
return function (Request $request, Response $response, \Exception $exception) use ($c) : Response {
$c->get('logger')->error('ERROR(500): ' . $exception->getMessage());
return $c['response']
->withJson(['code' => 500, 'message' => 'Internal Server Error'], 500);
};
};
// monolog
$container['logger'] = function ($c) {
$settings = $c->get('settings')['logger'];
$logger = new Monolog\Logger($settings['name']);
$handler = new Monolog\Handler\StreamHandler($settings['path'], $settings['level']);
$handler->setFormatter(new Monolog\Formatter\JsonFormatter());
$logger->pushHandler($handler);
return $logger;
};
// idiorm
ORM::configure($container->get('settings')['database']);