2019-01-13 13:06:32 +00:00
|
|
|
<?php
|
2019-01-14 22:18:30 +00:00
|
|
|
$start_time = microtime(true);
|
2019-01-13 13:06:32 +00:00
|
|
|
|
|
|
|
define("ROOT_DIR", dirname(__FILE__) . "/");
|
|
|
|
|
|
|
|
|
2019-01-15 19:26:14 +00:00
|
|
|
// 1: Autoloaders
|
2019-01-13 13:06:32 +00:00
|
|
|
require("vendor/autoload.php");
|
|
|
|
|
|
|
|
$autoloader = new \Aura\Autoload\Loader();
|
|
|
|
$autoloader->addPrefix("AirQuality", "logic");
|
|
|
|
$autoloader->addPrefix("SBRL", "lib/SBRL");
|
|
|
|
$autoloader->register();
|
|
|
|
|
2019-06-20 23:02:26 +00:00
|
|
|
// 1.5: Performance counter
|
|
|
|
$perfcounter = new \SBRL\PerformanceCounter();
|
|
|
|
$perfcounter->start("total");
|
2019-01-15 19:26:14 +00:00
|
|
|
|
2019-01-15 19:19:17 +00:00
|
|
|
// 2: Settings
|
2019-06-20 23:02:26 +00:00
|
|
|
$perfcounter->start("settings");
|
2019-01-15 19:19:17 +00:00
|
|
|
$settings = new \SBRL\TomlConfig(
|
|
|
|
"data/settings.toml",
|
|
|
|
"settings.default.toml"
|
|
|
|
);
|
2019-06-20 23:02:26 +00:00
|
|
|
$perfcounter->end("settings");
|
2019-01-15 19:19:17 +00:00
|
|
|
|
2019-01-13 13:06:32 +00:00
|
|
|
|
2019-01-15 19:26:14 +00:00
|
|
|
// 3: Dependency injection
|
2019-01-13 13:06:32 +00:00
|
|
|
|
2019-06-20 23:02:26 +00:00
|
|
|
$perfcounter->start("di");
|
|
|
|
|
2019-01-13 13:06:32 +00:00
|
|
|
$di_builder = new DI\ContainerBuilder();
|
|
|
|
$di_builder->addDefinitions("di_config.php");
|
2019-01-15 19:19:17 +00:00
|
|
|
if($settings->get("env.mode") == "production") {
|
|
|
|
// http://php-di.org/doc/container-configuration.html
|
|
|
|
if(!file_exists(ROOT_DIR."data/cache/php_di"))
|
2019-01-15 19:22:14 +00:00
|
|
|
mkdir(ROOT_DIR."data/cache/php_di", 0700, true);
|
2019-01-15 19:24:09 +00:00
|
|
|
$di_builder->enableCompilation(ROOT_DIR."data/cache/php_di");
|
2019-01-15 19:19:17 +00:00
|
|
|
|
|
|
|
if(!file_exists(ROOT_DIR."data/cache/php_di_proxies"))
|
2019-01-15 19:22:14 +00:00
|
|
|
mkdir(ROOT_DIR."data/cache/php_di_proxies", 0700, true);
|
2019-01-15 19:24:09 +00:00
|
|
|
$di_builder->writeProxiesToFile(true, ROOT_DIR."data/cache/php_di_proxies");
|
2019-01-15 19:19:17 +00:00
|
|
|
}
|
2019-01-13 13:06:32 +00:00
|
|
|
|
|
|
|
$di_container = $di_builder->build();
|
|
|
|
|
2019-06-20 23:02:26 +00:00
|
|
|
$perfcounter->end("di");
|
2019-01-13 13:06:32 +00:00
|
|
|
|
|
|
|
// 4: Database
|
|
|
|
|
2019-01-15 17:02:24 +00:00
|
|
|
// Done automagically by PHP-DI.
|
|
|
|
// PHP-DI autowires it, and doesn't create more than 1 instance of it either
|
2019-01-13 13:06:32 +00:00
|
|
|
|
2019-01-15 19:26:14 +00:00
|
|
|
|
2019-01-13 13:06:32 +00:00
|
|
|
// 5: Action
|
|
|
|
|
|
|
|
// Figure out the action name
|
|
|
|
$action = str_replace(
|
|
|
|
" ", "",
|
|
|
|
ucwords(str_replace(
|
|
|
|
"-", " ",
|
|
|
|
preg_replace("/[^a-z-]/i", "", $_GET["action"] ?? $settings->get("routing.default-action"))
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
|
|
|
$handler_name = "AirQuality\\Actions\\$action";
|
|
|
|
|
|
|
|
// if(!class_exists($handler_name))
|
|
|
|
// send_error(404, "Unknown action $handler_name.");
|
|
|
|
|
|
|
|
// Fetch it from the dependency injector and execute it, but only if it exists
|
|
|
|
|
|
|
|
if(!class_exists($handler_name)) {
|
|
|
|
http_response_code(404);
|
|
|
|
header("content-type: text/plain");
|
|
|
|
exit("Error: No action with the name '$action' could be found.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$handler = $di_container->get($handler_name);
|
2019-06-20 23:02:26 +00:00
|
|
|
|
|
|
|
$perfcounter->start("handle");
|
|
|
|
// FUTURE: A PSR-7 request/response system would be rather nice here.
|
2019-01-13 13:06:32 +00:00
|
|
|
$handler->handle();
|