-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
revised configuration file management
- Loading branch information
Showing
3 changed files
with
82 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
* Zookeeper Online | ||
* | ||
* @author Jim Mason <[email protected]> | ||
* @copyright Copyright (C) 1997-2019 Jim Mason <[email protected]> | ||
* @copyright Copyright (C) 1997-2021 Jim Mason <[email protected]> | ||
* @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,21 +107,19 @@ public function composeMenu($action, $session) { | |
'label' => $entry[2], | ||
'selected' => $selected ]; | ||
} | ||
} | ||
}); | ||
return $result; | ||
} | ||
|
||
/** | ||
* 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
* Zookeeper Online | ||
* | ||
* @author Jim Mason <[email protected]> | ||
* @copyright Copyright (C) 1997-2018 Jim Mason <[email protected]> | ||
* @copyright Copyright (C) 1997-2021 Jim Mason <[email protected]> | ||
* @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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters