Skip to content

Commit

Permalink
revised configuration file management
Browse files Browse the repository at this point in the history
  • Loading branch information
RocketMan committed Aug 27, 2021
1 parent 03067f5 commit d731559
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 51 deletions.
50 changes: 22 additions & 28 deletions controllers/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -34,6 +34,7 @@

require_once __DIR__."/../vendor/autoload.php";

use ZK\Engine\Config;
use ZK\Engine\Engine;

class Dispatcher {
Expand All @@ -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;
}
});
}

/**
Expand All @@ -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)
Expand All @@ -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];
Expand All @@ -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);
}
}
77 changes: 58 additions & 19 deletions engine/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
}
}
6 changes: 2 additions & 4 deletions engine/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit d731559

Please sign in to comment.