I am using this code for a fast JSON API without routes.
Does this code contain any useless parts? Is the class loader efficient enough? Are there any better options? Is ReflectionMethod
slow if used once?
<?php
/*
* Init stuff
*/
session_start();
define('ROOT', realpath( dirname(__FILE__) ) . '/');
set_include_path(ROOT . 'src/');
spl_autoload_extensions('.php');
spl_autoload_register();
ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
ini_set('display_errors', 1); //To change in prod to 0 <<<<<<<<<<<<<<<<<<<<<<<<<
ini_set('log_errors', 1);
ini_set('error_log', ROOT . 'tmp/logs/application.log');
date_default_timezone_set('Europe/Paris');
/*
* Process stuff
*/
$params = explode( '/', trim( key($_GET), '/' ) );
$paramsCount = count($params);
if ($paramsCount >= 2) {
try {
$controllerMethod = new ReflectionMethod( '\app\controller\\' .
$params[1], $params[2]
);
if ($paramsCount - 2 >= $controllerMethod->getNumberOfRequiredParameters()) {
\ens\controller\Request::$lang =& $params[0];
\ens\controller\Request::$data =& $_POST;
$response = $controllerMethod->invokeArgs( null, array_splice($params, 3) );
} else {
$response = array('status' => 400);
}
} catch (ReflectionException $e) {
$response = array('status' => 300);
}
} else {
$response = array('status' => 301);
}
if (!headers_sent()) {
header('Content-Type: application/json');
}
echo json_encode($response);