mirror of
https://github.com/openvk/chandler.git
synced 2025-03-31 21:43:59 +03:00
Reformatting the Boostrap class.
This commit is contained in:
parent
ff46d1f1e5
commit
545be21dbf
1 changed files with 88 additions and 92 deletions
|
@ -1,137 +1,134 @@
|
|||
<?php declare(strict_types=1);
|
||||
require_once(dirname(__FILE__) . "/../vendor/autoload.php");
|
||||
<?php
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
use Tracy\Debugger;
|
||||
|
||||
define("CHANDLER_VER", "0.0.1", false);
|
||||
define("CHANDLER_ROOT", dirname(__FILE__) . "/..", false);
|
||||
define("CHANDLER_VER", "0.0.1", false); // IMPROVE: Remove this.
|
||||
define("CHANDLER_ROOT", dirname(__FILE__) . "/..", false); // IMPROVE: Remove this.
|
||||
|
||||
/**
|
||||
* Bootstrap class, that is called during framework starting phase.
|
||||
* Initializes everything.
|
||||
*
|
||||
*
|
||||
* @author kurotsun <celestine@vriska.ru>
|
||||
* @internal
|
||||
*/
|
||||
class Bootstrap
|
||||
{
|
||||
/**
|
||||
* Starts Tracy debugger session and installs panels.
|
||||
*
|
||||
* @internal
|
||||
* Defines constant CONNECTING_IP, that stores end user's IP address.
|
||||
* Uses X-Forwarded-For if present.
|
||||
*
|
||||
* @return void
|
||||
* @internal
|
||||
*/
|
||||
private function defineIP(): void
|
||||
{
|
||||
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
|
||||
$path = explode(", ", $_SERVER["HTTP_X_FORWARDED_FOR"]);
|
||||
$ip = $path[0];
|
||||
} else {
|
||||
$ip = $_SERVER["REMOTE_ADDR"];
|
||||
}
|
||||
define("CONNECTING_IP", $ip, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstraps extensions.
|
||||
*
|
||||
* @return void
|
||||
* @internal
|
||||
*/
|
||||
private function igniteExtensions(): void
|
||||
{
|
||||
Chandler\Extensions\ExtensionManager::i();
|
||||
}
|
||||
|
||||
private function loadConfig(): void
|
||||
{
|
||||
if (!file_exists($conf = CHANDLER_ROOT . "/chandler.yml"))
|
||||
if (!file_exists($conf = CHANDLER_ROOT . "/../chandler.yml"))
|
||||
if (!file_exists($conf = "/etc/chandler.d/chandler.yml"))
|
||||
exit("Configuration file not found... Have you forgotten to rename it?");
|
||||
define("CHANDLER_ROOT_CONF", chandler_parse_yaml($conf)["chandler"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set ups autoloaders.
|
||||
*
|
||||
* @return void
|
||||
* @internal
|
||||
*/
|
||||
private function registerAutoloaders(): void
|
||||
{
|
||||
spl_autoload_register(function ($class): void {
|
||||
if (strpos($class, "Chandler\\") !== 0) return;
|
||||
require_once(str_replace("\\", "/", str_replace("Chandler\\", CHANDLER_ROOT . "/chandler/", $class)) . ".php");
|
||||
}, true, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts Tracy debugger session and installs panels.
|
||||
*
|
||||
* @return void
|
||||
* @internal
|
||||
*/
|
||||
private function registerDebugger(): void
|
||||
{
|
||||
Debugger::enable((CHANDLER_ROOT_CONF["debug"] ? Debugger::DEVELOPMENT : Debugger::PRODUCTION), __DIR__ . "/../logs");
|
||||
Debugger::getBar()->addPanel(new Chandler\Debug\DatabasePanel);
|
||||
}
|
||||
|
||||
private function loadConfig(): void
|
||||
{
|
||||
if(!file_exists($conf = CHANDLER_ROOT . "/chandler.yml"))
|
||||
if(!file_exists($conf = CHANDLER_ROOT . "/../chandler.yml"))
|
||||
if(!file_exists($conf = "/etc/chandler.d/chandler.yml"))
|
||||
exit("Configuration file not found... Have you forgotten to rename it?");
|
||||
|
||||
define("CHANDLER_ROOT_CONF", chandler_parse_yaml($conf)["chandler"]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Loads procedural APIs.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @return void
|
||||
* @internal
|
||||
*/
|
||||
private function registerFunctions(): void
|
||||
{
|
||||
foreach(glob(CHANDLER_ROOT . "/chandler/procedural/*.php") as $procDef)
|
||||
foreach (glob(CHANDLER_ROOT . "/chandler/procedural/*.php") as $procDef)
|
||||
require $procDef;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set ups autoloaders.
|
||||
*
|
||||
* @internal
|
||||
* Starts router and serves request.
|
||||
*
|
||||
* @param string $url Request URL
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function registerAutoloaders(): void
|
||||
{
|
||||
spl_autoload_register(function($class): void
|
||||
{
|
||||
if(strpos($class, "Chandler\\") !== 0) return;
|
||||
|
||||
require_once(str_replace("\\", "/", str_replace("Chandler\\", CHANDLER_ROOT . "/chandler/", $class)) . ".php");
|
||||
}, true, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines constant CONNECTING_IP, that stores end user's IP address.
|
||||
* Uses X-Forwarded-For if present.
|
||||
*
|
||||
* @internal
|
||||
* @return void
|
||||
*/
|
||||
private function defineIP(): void
|
||||
private function route(string $url): void
|
||||
{
|
||||
if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
|
||||
$path = explode(", ", $_SERVER["HTTP_X_FORWARDED_FOR"]);
|
||||
$ip = $path[0];
|
||||
} else {
|
||||
$ip = $_SERVER["REMOTE_ADDR"];
|
||||
}
|
||||
|
||||
define("CONNECTING_IP", $ip, false);
|
||||
ob_start();
|
||||
$router = Chandler\MVC\Routing\Router::i();
|
||||
if (($output = $router->execute($url, null)) !== null)
|
||||
echo $output;
|
||||
else
|
||||
chandler_http_panic(404, "Not Found", "No routes for $url.");
|
||||
ob_flush();
|
||||
ob_end_flush();
|
||||
flush();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initializes GeoIP, sets DB directory.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @return void
|
||||
* @internal
|
||||
*/
|
||||
private function setupGeoIP(): void
|
||||
{
|
||||
geoip_setup_custom_directory(CHANDLER_ROOT . "/3rdparty/maxmind/");
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstraps extensions.
|
||||
*
|
||||
* @internal
|
||||
* @return void
|
||||
*/
|
||||
private function igniteExtensions(): void
|
||||
{
|
||||
Chandler\Extensions\ExtensionManager::i();
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts router and serves request.
|
||||
*
|
||||
* @internal
|
||||
* @param string $url Request URL
|
||||
* @return void
|
||||
*/
|
||||
private function route(string $url): void
|
||||
{
|
||||
ob_start();
|
||||
|
||||
$router = Chandler\MVC\Routing\Router::i();
|
||||
if(($output = $router->execute($url, NULL)) !== null)
|
||||
echo $output;
|
||||
else
|
||||
chandler_http_panic(404, "Not Found", "No routes for $url.");
|
||||
|
||||
ob_flush();
|
||||
ob_end_flush();
|
||||
flush();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Starts framework.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @return void
|
||||
* @internal
|
||||
*/
|
||||
function ignite(bool $headless = false): void
|
||||
{
|
||||
|
@ -140,8 +137,7 @@ class Bootstrap
|
|||
$this->loadConfig();
|
||||
$this->registerDebugger();
|
||||
$this->igniteExtensions();
|
||||
|
||||
if(!$headless) {
|
||||
if (!$headless) {
|
||||
header("Referrer-Policy: strict-origin-when-cross-origin");
|
||||
$this->defineIP();
|
||||
$this->route(function_exists("get_current_url") ? get_current_url() : $_SERVER["REQUEST_URI"]);
|
||||
|
|
Loading…
Reference in a new issue