Moving the contants definitions and removing unused code.

This commit is contained in:
Ilya Bakhlin 2022-01-08 22:32:23 +01:00
parent ca311a53cc
commit 9bf852d417
4 changed files with 10 additions and 47 deletions

View file

@ -8,8 +8,9 @@ preferences:
exposeChandler: true exposeChandler: true
extensions: extensions:
path: null
allEnabled: false allEnabled: false
available: "/extensions/available"
enabled: "/extensions/enabled"
database: database:
dsn: "mysql:host=localhost;dbname=db" dsn: "mysql:host=localhost;dbname=db"

View file

@ -51,6 +51,6 @@ class Bootstrap
{ {
$this->igniteExtensions(); $this->igniteExtensions();
header("Referrer-Policy: strict-origin-when-cross-origin"); header("Referrer-Policy: strict-origin-when-cross-origin");
$this->route(function_exists("get_current_url") ? get_current_url() : $_SERVER["REQUEST_URI"]); $this->route($_SERVER["REQUEST_URI"]);
} }
} }

View file

@ -4,21 +4,14 @@ use Chandler\Classes\Singleton;
use Chandler\MVC\Routing\Router; use Chandler\MVC\Routing\Router;
use Nette\Utils\Finder; use Nette\Utils\Finder;
define("CHANDLER_EXTENSIONS", CHANDLER_ROOT_CONF["extensions"]["path"] ?? CHANDLER_ROOT . "/extensions", false); /**
define("CHANDLER_EXTENSIONS_AVAILABLE", CHANDLER_EXTENSIONS . "/available", false); * @package Chandler\Extensions
*/
if(CHANDLER_ROOT_CONF["extensions"]["allEnabled"]) {
define("CHANDLER_EXTENSIONS_ENABLED", CHANDLER_EXTENSIONS_AVAILABLE, false);
} else {
define("CHANDLER_EXTENSIONS_ENABLED", CHANDLER_EXTENSIONS . "/enabled", false);
}
class ExtensionManager extends Singleton class ExtensionManager extends Singleton
{ {
private $extensions = []; private $extensions = [];
private $router = NULL; private $router;
private $rootApp = NULL; private $rootApp;
private $eventLoop = NULL;
protected function __construct() protected function __construct()
{ {
@ -66,11 +59,6 @@ class ExtensionManager extends Singleton
private function init(): void private function init(): void
{ {
foreach($this->getExtensions(true) as $name => $configuration) { foreach($this->getExtensions(true) as $name => $configuration) {
spl_autoload_register(@create_function("\$class", "
if(!substr(\$class, 0, " . iconv_strlen("$name\\") . ") === \"$name\\\\\") return false;
include_once CHANDLER_EXTENSIONS_ENABLED . \"/\" . str_replace(\"\\\\\", \"/\", \$class) . \".php\";
"));
define(str_replace("-", "_", mb_strtoupper($name)) . "_ROOT", CHANDLER_EXTENSIONS_ENABLED . "/$name", false); define(str_replace("-", "_", mb_strtoupper($name)) . "_ROOT", CHANDLER_EXTENSIONS_ENABLED . "/$name", false);
define(str_replace("-", "_", mb_strtoupper($name)) . "_ROOT_CONF", chandler_parse_yaml(CHANDLER_EXTENSIONS_ENABLED . "/$name/$name.yml"), false); define(str_replace("-", "_", mb_strtoupper($name)) . "_ROOT_CONF", chandler_parse_yaml(CHANDLER_EXTENSIONS_ENABLED . "/$name/$name.yml"), false);
@ -81,15 +69,6 @@ class ExtensionManager extends Singleton
$init(); $init();
} }
if(is_dir($hooks = CHANDLER_EXTENSIONS_ENABLED . "/$name/Hooks")) {
foreach(Finder::findFiles("*Hook.php")->in($hooks) as $hookFile) {
$hookClassName = "$name\\Hooks\\" . str_replace(".php", "", end(explode("/", $hookFile)));
$hook = new $hookClassName;
$this->eventLoop->addListener($hook);
}
}
if(is_dir($app = CHANDLER_EXTENSIONS_ENABLED . "/$name/Web")) #"app" means "web app", thus variable is called $app if(is_dir($app = CHANDLER_EXTENSIONS_ENABLED . "/$name/Web")) #"app" means "web app", thus variable is called $app
$this->router->readRoutes("$app/routes.yml", $name, $this->rootApp !== $name); $this->router->readRoutes("$app/routes.yml", $name, $this->rootApp !== $name);
} }
@ -106,23 +85,4 @@ class ExtensionManager extends Singleton
{ {
return @$this->extensions[$name]; return @$this->extensions[$name];
} }
function disableExtension(string $name): void
{
if(!array_key_exists($name, $this->getExtensions(true))) return;
if(!unlink(CHANDLER_EXTENSIONS_ENABLED . "/$name")) throw new \Exception("Could not disable extension");
}
function enableExtension(string $name): void
{
if(CHANDLER_ROOT_CONF["extensions"]["allEnabled"]) return;
if(array_key_exists($name, $this->getExtensions(true))) return;
$path = CHANDLER_EXTENSIONS_AVAILABLE . "/$name";
if(!is_dir($path)) throw new \Exception("Extension doesn't exist");
if(!symlink($path, str_replace("available", "enabled", $path))) throw new \Exception("Could not enable extension");
}
} }

View file

@ -6,6 +6,8 @@ require_once(dirname(__DIR__) . "/vendor/autoload.php");
define("CHANDLER_ROOT", dirname(__DIR__)); define("CHANDLER_ROOT", dirname(__DIR__));
define("CHANDLER_ROOT_CONF", yaml_parse_file(CHANDLER_ROOT . "/chandler.yml")); define("CHANDLER_ROOT_CONF", yaml_parse_file(CHANDLER_ROOT . "/chandler.yml"));
define("CHANDLER_EXTENSIONS_AVAILABLE", CHANDLER_ROOT . CHANDLER_ROOT_CONF["extensions"]["available"]);
define("CHANDLER_EXTENSIONS_ENABLED", CHANDLER_ROOT . CHANDLER_ROOT_CONF["extensions"]["enabled"]);
$bootstrap = new Bootstrap(); $bootstrap = new Bootstrap();