diff --git a/controllers/Dispatcher.php b/controllers/Dispatcher.php index 91bc7175..966f7da6 100644 --- a/controllers/Dispatcher.php +++ b/controllers/Dispatcher.php @@ -3,7 +3,7 @@ * Zookeeper Online * * @author Jim Mason - * @copyright Copyright (C) 1997-2019 Jim Mason + * @copyright Copyright (C) 1997-2021 Jim Mason * @link https://zookeeper.ibinx.com/ * @license GPL-3.0 * @@ -34,6 +34,7 @@ require_once __DIR__."/../vendor/autoload.php"; +use ZK\Engine\Config; use ZK\Engine\Engine; class Dispatcher { @@ -42,32 +43,27 @@ class Dispatcher { public function __construct() { // UI configuration file - include __DIR__.'/../config/ui_config.php'; - if(isset($menu) && is_array($menu)) { - $this->menu = $menu; - $customMenu = Engine::param('custom_menu'); - if($customMenu) - $this->menu = array_merge($this->menu, $customMenu); - } + $this->menu = new Config('ui_config', 'menu'); + $customMenu = Engine::param('custom_menu'); + if($customMenu) + $this->menu->merge($customMenu); // Controllers - if(isset($controllers)) { - $this->controllers = $controllers; - $customControllers = Engine::param('custom_controllers'); - if($customControllers) - $this->controllers = array_merge($this->controllers, $customControllers); - } + $this->controllers = new Config('ui_config', 'controllers'); + $customControllers = Engine::param('custom_controllers'); + if($customControllers) + $this->controllers->merge($customControllers); } /** * return menu entry that matches the specified action */ public function match($action) { - foreach($this->menu as $entry) { + return $this->menu->iterate(function($entry) use($action) { if($entry[1] == $action || substr($entry[1], -1) == '%' && substr($entry[1], 0, -1) == substr($action, 0, strlen($entry[1])-1)) return $entry; - } + }); } /** @@ -79,7 +75,7 @@ public function dispatch($action, $subaction, $session) { // If no action was selected or if action is unauthorized, // default to the first one if(!$entry || !$session->isAuth($entry[0])) - $entry = $this->menu[0]; + $entry = $this->menu->default(); $handler = new $entry[3](); if($handler instanceof CommandTarget) @@ -98,8 +94,8 @@ public function isActionAuth($action, $session) { * compose the menu for the specified session */ public function composeMenu($action, $session) { - $result = array(); - foreach ($this->menu as $entry) { + $result = []; + $this->menu->iterate(function($entry) use(&$result, $action, $session) { if($entry[2] && $session->isAuth($entry[0])) { $baseAction = substr($entry[1], -1) == '%'? substr($entry[1], 0, -1):$entry[1]; @@ -111,7 +107,7 @@ public function composeMenu($action, $session) { 'label' => $entry[2], 'selected' => $selected ]; } - } + }); return $result; } @@ -119,13 +115,11 @@ public function composeMenu($action, $session) { * dispatch request to the specified controller */ public function processRequest($controller="") { - if(isset($this->controllers)) { - if(empty($controller) || - !array_key_exists($controller, $this->controllers)) - $controller = array_keys($this->controllers)[0]; - $impl = new $this->controllers[$controller](); - if($impl instanceof IController) - $impl->processRequest($this); - } + $implClass = empty($controller) || + !($p = $this->controllers->getParam($controller)) ? + $this->controllers->default():$p; + $impl = new $implClass(); + if($impl instanceof IController) + $impl->processRequest($this); } } diff --git a/engine/Config.php b/engine/Config.php index e509960e..698d3625 100644 --- a/engine/Config.php +++ b/engine/Config.php @@ -3,7 +3,7 @@ * Zookeeper Online * * @author Jim Mason - * @copyright Copyright (C) 1997-2018 Jim Mason + * @copyright Copyright (C) 1997-2021 Jim Mason * @link https://zookeeper.ibinx.com/ * @license GPL-3.0 * @@ -30,34 +30,73 @@ class Config { private $config; - public function init($file) { - // populate the config property from the given file - include $file; - if(isset($config) && is_array($config)) - $this->config = $config; + /** + * ctor + * + * @param file base filename of configuration file (without extension) + * @param variable variable name in config file (default 'config') + */ + public function __construct($file, $variable = 'config') { + // populate the configuration from the given file and variable + include __DIR__."/../config/${file}.php"; + if(isset($$variable) && is_array($$variable)) + $this->config = $$variable; + else + throw new \Exception("Error parsing configuration: file=${file}.php, variable=${variable}"); } /** - * get a configuration value from the configuration file + * merge an array of entries into this configuration * - * @param key name of param - * @param default value if param is not set (optional) - * @return value or null if not set and no default specified + * @param config array to merge */ - public function getParam($key, $default = null) { - return isset($this->config) && - array_key_exists($key, $this->config)? - $this->config[$key]:$default; + public function merge($config) { + $this->config = array_merge($this->config, $config); + } + + /** + * iterate over the entries in the configuration + * + * calls user-supplied callback for each entry. + * iteration ceases upon first non-null return value from the callback + * + * @param fn callback with signature 'function($entry)' + * @return first value returned by a callback, if any + */ + public function iterate($fn) { + foreach($this->config as $entry) + if(($x = $fn($entry)) !== null) + return $x; } /** - * set a configurate value in the in-memory configuration data + * return the default (first) configuration entry + * + * @return default entry + */ + public function default() { + return $this->config[array_keys($this->config)[0]]; + } + + /** + * determine whether the specified configuration param exists + * + * @param key name of param to test + * @return true if exists, false otherwise + */ + public function hasParam($key) { + return array_key_exists($key, $this->config); + } + + /** + * get a configuration value from the configuration file * * @param key name of param - * @param value value to set + * @param default value if param is not set (optional) + * @return value or null if not set and no default specified */ - public function setParam($key, $value) { - if(isset($this->config)) - $this->config[$key] = $value; + public function getParam($key, $default = null) { + return array_key_exists($key, $this->config)? + $this->config[$key]:$default; } } diff --git a/engine/Engine.php b/engine/Engine.php index 234cbec3..f86aabfd 100644 --- a/engine/Engine.php +++ b/engine/Engine.php @@ -54,12 +54,10 @@ public static function init() { self::customAutoloader(); // application configuration file - self::$config = new Config(); - self::$config->init(__DIR__.'/../config/config.php'); + self::$config = new Config('config'); // engine configuration file - self::$apis = new Config(); - self::$apis->init(__DIR__.'/../config/engine_config.php'); + self::$apis = new Config('engine_config'); self::$session = new Session(); }