From 9bf852d417b3f1c97f9d49e760124d0b5d1dc1dc Mon Sep 17 00:00:00 2001 From: Ilya Bakhlin Date: Sat, 8 Jan 2022 22:32:23 +0100 Subject: [PATCH] Moving the contants definitions and removing unused code. --- chandler-example.yml | 3 +- chandler/Bootstrap.php | 2 +- chandler/Extensions/ExtensionManager.php | 50 +++--------------------- public/index.php | 2 + 4 files changed, 10 insertions(+), 47 deletions(-) diff --git a/chandler-example.yml b/chandler-example.yml index e74dbc1..c386a90 100644 --- a/chandler-example.yml +++ b/chandler-example.yml @@ -8,8 +8,9 @@ preferences: exposeChandler: true extensions: - path: null allEnabled: false + available: "/extensions/available" + enabled: "/extensions/enabled" database: dsn: "mysql:host=localhost;dbname=db" diff --git a/chandler/Bootstrap.php b/chandler/Bootstrap.php index ee978ef..97333f0 100644 --- a/chandler/Bootstrap.php +++ b/chandler/Bootstrap.php @@ -51,6 +51,6 @@ class Bootstrap { $this->igniteExtensions(); 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"]); } } diff --git a/chandler/Extensions/ExtensionManager.php b/chandler/Extensions/ExtensionManager.php index 579b4f7..98f5f40 100644 --- a/chandler/Extensions/ExtensionManager.php +++ b/chandler/Extensions/ExtensionManager.php @@ -4,21 +4,14 @@ use Chandler\Classes\Singleton; use Chandler\MVC\Routing\Router; use Nette\Utils\Finder; -define("CHANDLER_EXTENSIONS", CHANDLER_ROOT_CONF["extensions"]["path"] ?? CHANDLER_ROOT . "/extensions", false); -define("CHANDLER_EXTENSIONS_AVAILABLE", CHANDLER_EXTENSIONS . "/available", false); - -if(CHANDLER_ROOT_CONF["extensions"]["allEnabled"]) { - define("CHANDLER_EXTENSIONS_ENABLED", CHANDLER_EXTENSIONS_AVAILABLE, false); -} else { - define("CHANDLER_EXTENSIONS_ENABLED", CHANDLER_EXTENSIONS . "/enabled", false); -} - +/** + * @package Chandler\Extensions + */ class ExtensionManager extends Singleton { private $extensions = []; - private $router = NULL; - private $rootApp = NULL; - private $eventLoop = NULL; + private $router; + private $rootApp; protected function __construct() { @@ -66,11 +59,6 @@ class ExtensionManager extends Singleton private function init(): void { 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_CONF", chandler_parse_yaml(CHANDLER_EXTENSIONS_ENABLED . "/$name/$name.yml"), false); @@ -81,15 +69,6 @@ class ExtensionManager extends Singleton $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 $this->router->readRoutes("$app/routes.yml", $name, $this->rootApp !== $name); } @@ -106,23 +85,4 @@ class ExtensionManager extends Singleton { 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"); - } } diff --git a/public/index.php b/public/index.php index 282d4a7..2c2c953 100644 --- a/public/index.php +++ b/public/index.php @@ -6,6 +6,8 @@ require_once(dirname(__DIR__) . "/vendor/autoload.php"); define("CHANDLER_ROOT", dirname(__DIR__)); 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();