forked from Ahmard/utiweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
69 lines (56 loc) · 1.86 KB
/
index.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
<?php
use App\Core\Helpers\Classes\RequestHelper;
use App\Core\Http\RequestHandler;
use App\Core\Http\Response\InternalServerErrorResponse;
use App\Core\ResponseGenerator;
use Dotenv\Dotenv;
use Laminas\Diactoros\ServerRequestFactory;
//From line 10-14 should be removed when used in apache
$uri = substr($_SERVER['REQUEST_URI'], 1);
if ('/' !== $uri && file_exists($uri)) {
return false;
}
$start = microtime(true);
require 'vendor/autoload.php';
//Create request instance
$request = ServerRequestFactory::fromGlobals();
/**
* @param Throwable $exception
* @param bool $willTerminate
*/
function handleApplicationException(Throwable $exception, bool $willTerminate = true): void
{
global $request;
//Save error log
$filename = __DIR__ . '/storage/logs/error/' . date('d_m_Y-H_i_s') . '.json';
file_put_contents($filename, json_encode([
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $exception->getMessage(),
'uri' => $request->getUri()->getPath(),
'trace' => $exception->getTraceAsString(),
], JSON_PRETTY_PRINT));
if ($willTerminate) {
//Send server error response to client
InternalServerErrorResponse::create($exception)->send($request);
}
}
//Handle all exceptions thrown
set_exception_handler('handleApplicationException');
//Load environment variables
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
if ('production' === $_ENV['APP_ENVIRONMENT']) {
error_reporting(E_ERROR);
}
try {
//Http request helper
RequestHelper::setRequest($request);
//Helper functions
require('app/Core/Helpers/helperFunctions.php');
$response = RequestHandler::handle($request);
//Send response to browser
ResponseGenerator::generate($response)->send($request);
} catch (Throwable $exception) {
handleApplicationException($exception);
}