diff --git a/chandler/Extensions/ExtensionManager.php b/chandler/Extensions/ExtensionManager.php index 98f5f40..e3ffc4b 100644 --- a/chandler/Extensions/ExtensionManager.php +++ b/chandler/Extensions/ExtensionManager.php @@ -1,5 +1,7 @@ -in(CHANDLER_EXTENSIONS_AVAILABLE) as $directory) { - $extensionName = $directory->getFilename(); - $directory = $directory->getRealPath(); - $config = "$directory/manifest.yml"; - - if(!file_exists($config)) { - trigger_error("Skipping $extensionName for not having a valid configuration file ($config is not found)", E_USER_WARNING); - continue; - } - - $this->extensions[$extensionName] = (object) chandler_parse_yaml($config); - $this->extensions[$extensionName]->id = $extensionName; - $this->extensions[$extensionName]->rawName = $directory; - $this->extensions[$extensionName]->enabled = CHANDLER_ROOT_CONF["extensions"]["allEnabled"]; - } - - 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; - } - } - - if(!array_key_exists(CHANDLER_ROOT_CONF["rootApp"], $this->extensions) || !$this->extensions[CHANDLER_ROOT_CONF["rootApp"]]->enabled) { - trigger_error("Selected root app is not available", E_USER_ERROR); - } - - $this->rootApp = CHANDLER_ROOT_CONF["rootApp"]; - $this->router = Router::getInstance(); - - $this->init(); - } + private $router; private function init(): void { - foreach($this->getExtensions(true) as $name => $configuration) { - + foreach ($this->getExtensions(true) as $name => $configuration) { 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); - - if(isset($configuration->init)) { + if (isset($configuration->init)) { $init = require(CHANDLER_EXTENSIONS_ENABLED . "/$name/" . $configuration->init); - if(is_callable($init)) + if (is_callable($init)) $init(); } - - 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); } } - function getExtensions(bool $onlyEnabled = false): array - { - return $onlyEnabled - ? array_filter($this->extensions, function($e) { return $e->enabled; }) - : $this->extensions; - } - function getExtension(string $name): ?object { return @$this->extensions[$name]; } + + function getExtensions(bool $onlyEnabled = false): array + { + return $onlyEnabled + ? array_filter($this->extensions, function ($e) { + return $e->enabled; + }) + : $this->extensions; + } + + protected function __construct() + { + foreach (Finder::findDirectories("*")->in(CHANDLER_EXTENSIONS_AVAILABLE) as $directory) { + $extensionName = $directory->getFilename(); + $directory = $directory->getRealPath(); + $config = "$directory/manifest.yml"; + if (!file_exists($config)) { + trigger_error("Skipping $extensionName for not having a valid configuration file ($config is not found)", E_USER_WARNING); + continue; + } + $this->extensions[$extensionName] = (object)chandler_parse_yaml($config); + $this->extensions[$extensionName]->id = $extensionName; + $this->extensions[$extensionName]->rawName = $directory; + $this->extensions[$extensionName]->enabled = CHANDLER_ROOT_CONF["extensions"]["allEnabled"]; + } + 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; + } + } + if (!array_key_exists(CHANDLER_ROOT_CONF["rootApp"], $this->extensions) || !$this->extensions[CHANDLER_ROOT_CONF["rootApp"]]->enabled) { + trigger_error("Selected root app is not available", E_USER_ERROR); + } + $this->rootApp = CHANDLER_ROOT_CONF["rootApp"]; + $this->router = Router::getInstance(); + $this->init(); + } }