From 8aa46425b5869769f906e51f97383b42f4e42044 Mon Sep 17 00:00:00 2001 From: Alma Armas Date: Wed, 27 Jan 2021 18:52:19 +0000 Subject: [PATCH] Add option to change extensions location and to force enable 'em all --- chandler-example.yml | 4 +++ chandler/Extensions/ExtensionManager.php | 37 ++++++++++++++---------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/chandler-example.yml b/chandler-example.yml index 75f3c76..f5d7362 100644 --- a/chandler-example.yml +++ b/chandler-example.yml @@ -8,6 +8,10 @@ chandler: adminUrl: "/chandlerd" exposeChandler: true + extensions: + path: null + allEnabled: false + database: dsn: "mysql:host=localhost;dbname=db" user: "root" diff --git a/chandler/Extensions/ExtensionManager.php b/chandler/Extensions/ExtensionManager.php index 875ebff..a6f6a5f 100644 --- a/chandler/Extensions/ExtensionManager.php +++ b/chandler/Extensions/ExtensionManager.php @@ -5,9 +5,14 @@ use Chandler\Patterns\TSimpleSingleton; use Chandler\MVC\Routing\Router; use Nette\Utils\Finder; -define("CHANDLER_EXTENSIONS", CHANDLER_ROOT . "/extensions", false); +define("CHANDLER_EXTENSIONS", CHANDLER_ROOT_CONF["extensions"]["path"] ?? CHANDLER_ROOT . "/extensions", false); define("CHANDLER_EXTENSIONS_AVAILABLE", CHANDLER_EXTENSIONS . "/available", false); -define("CHANDLER_EXTENSIONS_ENABLED", CHANDLER_EXTENSIONS . "/enabled", false); + +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 { @@ -31,20 +36,22 @@ class ExtensionManager $this->extensions[$extensionName] = (object) chandler_parse_yaml($config); $this->extensions[$extensionName]->id = $extensionName; $this->extensions[$extensionName]->rawName = $directory; - $this->extensions[$extensionName]->enabled = false; + $this->extensions[$extensionName]->enabled = CHANDLER_ROOT_CONF["extensions"]["allEnabled"]; } - foreach(Finder::find("*")->in(CHANDLER_EXTENSIONS_ENABLED) as $directory) { #findDirectories doesn't work with symlinks - if(!is_dir($directory->getRealPath())) continue; - - $extension = $directory->getFilename(); - - if(!array_key_exists($extension, $this->extensions)) { - trigger_error("Extension $extension is enabled, but not available, skipping", E_USER_WARNING); - continue; + if(!CHANDLER_ROOT_CONF["extensions"]["allEnabled"]) { + foreach(Finder::find("*")->in(CHANDLER_EXTENSIONS_ENABLED) as $directory) { #findDirectories doesn't work with symlinks + if(!is_dir($directory->getRealPath())) continue; + + $extension = $directory->getFilename(); + + if(!array_key_exists($extension, $this->extensions)) { + trigger_error("Extension $extension is enabled, but not available, skipping", E_USER_WARNING); + continue; + } + + $this->extensions[$extension]->enabled = true; } - - $this->extensions[$extension]->enabled = true; } if(!array_key_exists(CHANDLER_ROOT_CONF["rootApp"], $this->extensions) || !$this->extensions[CHANDLER_ROOT_CONF["rootApp"]]->enabled) { @@ -64,14 +71,14 @@ class ExtensionManager spl_autoload_register(@create_function("\$class", " if(!substr(\$class, 0, " . iconv_strlen("$name\\") . ") === \"$name\\\\\") return false; - include_once CHANDLER_ROOT . \"/extensions/enabled/\" . str_replace(\"\\\\\", \"/\", \$class) . \".php\"; + include_once CHANDLER_EXTENSIONS_ENABLED . \"/\" . str_replace(\"\\\\\", \"/\", \$class) . \".php\"; ")); define(str_replace("-", "_", mb_strtoupper($name)) . "_ROOT", CHANDLER_ROOT . "/extensions/enabled/$name", false); define(str_replace("-", "_", mb_strtoupper($name)) . "_ROOT_CONF", chandler_parse_yaml(CHANDLER_ROOT . "/extensions/enabled/$name/$name.yml"), false); if(isset($configuration->init)) { - $init = require(CHANDLER_ROOT . "/extensions/enabled/$name/" . $configuration->init); + $init = require(CHANDLER_EXTENSIONS_ENABLED . "/$name/" . $configuration->init); if(is_callable($init)) $init(); }