55 lines
1.1 KiB
PHP
55 lines
1.1 KiB
PHP
<?php
|
|
namespace phprs;
|
|
|
|
use phprs\util\IoCFactory;
|
|
use phprs\util\exceptions\NotFound;
|
|
use phprs\util\exceptions\BadRequest;
|
|
use phprs\util\exceptions\Forbidden;
|
|
|
|
class Bootstrap
|
|
{
|
|
static public function run($conf_file)
|
|
{
|
|
$err = null;
|
|
$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
|
|
//执行请求
|
|
try
|
|
{
|
|
require_once __DIR__ . '/AutoLoad.php';
|
|
$factory = new IoCFactory($conf_file);
|
|
$router = $factory->create('phprs\\RouterWithCache');
|
|
//var_dump($router);
|
|
$router();
|
|
}
|
|
catch (NotFound $e)
|
|
{
|
|
header($protocol . ' 404 Not Found');
|
|
$err = $e;
|
|
}
|
|
catch (BadRequest $e)
|
|
{
|
|
header($protocol . ' 400 Bad Request');
|
|
$err = $e;
|
|
}
|
|
catch (Forbidden $e)
|
|
{
|
|
header($protocol . ' 403 Forbidden');
|
|
$err = $e;
|
|
}
|
|
catch (\Exception $e)
|
|
{
|
|
header($protocol . ' 500 Internal Server Error');
|
|
$err = $e;
|
|
}
|
|
if ($err)
|
|
{
|
|
header("Content-Type: application/json; charset=UTF-8");
|
|
$estr = array(
|
|
'error' => get_class($err),
|
|
'message' => $err->getMessage(),
|
|
);
|
|
echo json_encode($estr, JSON_UNESCAPED_UNICODE);
|
|
}
|
|
}
|
|
}
|