PHP Classes

File: index.php

Recommend this page to a friend!
  Classes of Ahmad Mustapha   Utility Web PHP API   index.php   Download  
File: index.php
Role: Application script
Content type: text/plain
Description: Application script
Class: Utility Web PHP API
API to retrieve movie details and other resources
Author: By
Last change:
Date: 3 years ago
Size: 1,726 bytes
 

Contents

Class file image Download
<?php

use App\Core\Helpers\Classes\RequestHelper;
use
App\Core\Http\Response\InternalServerErrorResponse;
use
App\Core\Http\Response\ResponseInterface;
use
App\Core\Http\Router\Dispatcher;
use
App\Core\Http\Router\Router;
use
App\Core\ResponseGenerator;
use
Dotenv\Dotenv;
use
Sunrise\Http\ServerRequest\ServerRequestFactory;

$uri = $_SERVER['REQUEST_URI'];
if (
'/' !== $uri && file_exists($uri)) {
    return
false;
}

require
'vendor/autoload.php';

//Create request instance
$request = ServerRequestFactory::fromGlobals();


/**
 * @param Throwable $exception
 */
function handleApplicationException(Throwable $exception)
{
    global
$request;
   
//Save error log
   
$filename = __DIR__ . '/storage/logs/error-' . date('d_m_Y-H_i_s') . '.log';
   
file_put_contents($filename, $exception);

   
//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();

//Http request helper
RequestHelper::setRequest($request);

//Helper functions
require('app/Core/Helpers/generalHelperFunctions.php');
require(
'app/Core/Helpers/httpHelperFunctions.php');

$response = null;
try {
   
//Router dispatch result
   
$dispatchResult = Dispatcher::dispatch($request);

   
//Execution response
   
$response = Router::route($request, $dispatchResult);

    if(
$response instanceof Throwable){
       
handleApplicationException($response);
    }
} catch (
Throwable $throwable) {
   
handleApplicationException($throwable);
}

//Send response to browser
ResponseGenerator::generate($response)->send($request);