Skip to content

Commit

Permalink
refactored controller configuration to controller_config; refactored …
Browse files Browse the repository at this point in the history
…UI configuration to UIDispatcher (UI/Main); changed signature of IController::processRequest
  • Loading branch information
RocketMan committed Aug 29, 2021
1 parent d731559 commit f6b25d4
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 135 deletions.
14 changes: 8 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,24 @@ The following is an overview of the source code directory structure:
settings for the database, SSO setup (if any),
e-mail, hyperlinks, branding, etc.
controller_config.php
Controller configuration. Here we map request targets
onto controllers.
engine_config.php
Model configuration. This maps the model interfaces
onto the concrete implementations.
onto concrete implementations.
ui_config.php
Controller and navigation configuration. Controller
configuration maps request targets onto controllers;
navigation configuration defines menu items, access
controls, and implementations.
User interface configuration. This defines menu items,
access controls, and implementations.
controllers/
Controllers are responsible for processing requests
that are received by the application. Controllers are
instantiated and invoked by the Dispatcher, whose
operation is specified via metadata in
config/ui_config.php.
config/controller_config.php.
css/
CSS assets. These files are automatically whitespace
Expand Down
19 changes: 19 additions & 0 deletions config/controller_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Target to controller mappings
*
* The first item is the default if no target is specified.
*/
$controllers = [
'main' => ZK\UI\UIController::class,
'addexp' => ZK\Controllers\ExportAdd::class,
'export' => ZK\Controllers\ExportPlaylist::class,
'afile' => ZK\Controllers\ExportAfile::class,
'opensearch' => ZK\Controllers\OpenSearch::class,
'daily' => ZK\Controllers\RunDaily::class,
'print' => ZK\Controllers\PrintTags::class,
'rss' => ZK\Controllers\RSS::class,
'api' => ZK\Controllers\API::class,
'sso' => ZK\Controllers\SSOLogin::class,
'push' => ZK\Controllers\PushServer::class,
];
31 changes: 5 additions & 26 deletions config/ui_config.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php
/**
* User Interface configuration
*
* Navigation and command processing array
*
* Each array entry is itself an array which consists of four elements
* as follows: (names are for convenience; array is numeric/keyless)
* Each entry consists of four elements as follows: (names are for
* convenience; array is numeric/keyless)
*
* * access - specifies the required access level to use this
* function: "a" is all, "u" is authenticated users,
Expand All @@ -21,16 +20,15 @@
* it will be selected if the action string is any of these:
* "someThing", "someThingElse", "someThingMore", etc.)
*
* * menu - text displayed in the navigation menu for this command;
* * label - text displayed in the navigation menu for this command;
* this should be 0 for commands which have no corresponding
* menu item
*
* * class - MenuItem class to which command is dispatched
* * implementation - MenuItem class to which action is dispatched
*
* The first item in the list is the default if no action is specified.
*/

//NOTE: server must be restarted for changes to take effect.
$menu = [
// access, action, menu label, implementation class
[ 'a', 'home', 0, ZK\UI\Home::class ],
Expand All @@ -54,22 +52,3 @@
[ 'x', 'adminUsers', 'Administer Users', ZK\UI\UserAdmin::class ],
[ 'a', 'viewChart', 'Airplay Charts', ZK\UI\Charts::class ],
];

/**
* IController implementations
*
* The first item in the list is the default if no target is specified.
*/
$controllers = [
'main' => ZK\UI\Main::class,
'addexp' => ZK\Controllers\ExportAdd::class,
'export' => ZK\Controllers\ExportPlaylist::class,
'afile' => ZK\Controllers\ExportAfile::class,
'opensearch' => ZK\Controllers\OpenSearch::class,
'daily' => ZK\Controllers\RunDaily::class,
'print' => ZK\Controllers\PrintTags::class,
'rss' => ZK\Controllers\RSS::class,
'api' => ZK\Controllers\API::class,
'sso' => ZK\Controllers\SSOLogin::class,
'push' => ZK\Controllers\PushServer::class,
];
2 changes: 1 addition & 1 deletion controllers/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ class API extends CommandTarget implements IController {
private $limit;
private $serializer;

public function processRequest($dispatcher) {
public function processRequest() {
$wantXml = $_REQUEST["xml"] ||
substr($_SERVER["HTTP_ACCEPT"], 0, 8) == "text/xml";
$this->serializer = $wantXml?new XMLSerializer():new JSONSerializer();
Expand Down
67 changes: 2 additions & 65 deletions controllers/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,79 +38,16 @@
use ZK\Engine\Engine;

class Dispatcher {
private $menu;
private $controllers;

public function __construct() {
// UI configuration file
$this->menu = new Config('ui_config', 'menu');
$customMenu = Engine::param('custom_menu');
if($customMenu)
$this->menu->merge($customMenu);

// Controllers
$this->controllers = new Config('ui_config', 'controllers');
$this->controllers = new Config('controller_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) {
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;
});
}

/**
* dispatch action to the appropriate menu item/command target
*/
public function dispatch($action, $subaction, $session) {
$entry = $this->match($action);

// If no action was selected or if action is unauthorized,
// default to the first one
if(!$entry || !$session->isAuth($entry[0]))
$entry = $this->menu->default();

$handler = new $entry[3]();
if($handler instanceof CommandTarget)
$handler->process($action, $subaction, $session);
}

/**
* indicate whether the specified action is authorized for the session
*/
public function isActionAuth($action, $session) {
$entry = $this->match($action);
return !$entry || $session->isAuth($entry[0]);
}

/**
* compose the menu for the specified session
*/
public function composeMenu($action, $session) {
$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];
$selected = $entry[1] == $action ||
substr($entry[1], -1) == '%' &&
substr($entry[1], 0, -1) ==
substr($action, 0, strlen($entry[1]) - 1);
$result[] = [ 'action' => $baseAction,
'label' => $entry[2],
'selected' => $selected ];
}
});
return $result;
}

/**
* dispatch request to the specified controller
*/
Expand All @@ -120,6 +57,6 @@ public function processRequest($controller="") {
$this->controllers->default():$p;
$impl = new $implClass();
if($impl instanceof IController)
$impl->processRequest($this);
$impl->processRequest();
}
}
2 changes: 1 addition & 1 deletion controllers/ExportAdd.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use ZK\Engine\IChart;

class ExportAdd implements IController {
public function processRequest($dispatcher) {
public function processRequest() {
// Ensure there's a date
$date = $_REQUEST["date"];
if(strlen($date) != 10 ||
Expand Down
4 changes: 2 additions & 2 deletions controllers/ExportAfile.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 All @@ -29,7 +29,7 @@
use ZK\UI\AddManager;

class ExportAfile implements IController {
public function processRequest($dispatcher) {
public function processRequest() {
$userAgent = $_SERVER["HTTP_USER_AGENT"];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Expand Down
4 changes: 2 additions & 2 deletions controllers/ExportPlaylist.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 Down Expand Up @@ -48,7 +48,7 @@ class ExportPlaylist extends CommandTarget implements IController {
private $time;
private $records;

public function processRequest($dispatcher) {
public function processRequest() {
// Ensure user has selected a playlist
$playlist = intval($_REQUEST["playlist"]);
if($playlist == 0) {
Expand Down
4 changes: 2 additions & 2 deletions controllers/IController.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 @@ -25,5 +25,5 @@
namespace ZK\Controllers;

interface IController {
public function processRequest($dispatcher);
public function processRequest();
}
4 changes: 2 additions & 2 deletions controllers/OpenSearch.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 @@ -29,7 +29,7 @@
use ZK\UI\UICommon as UI;

class OpenSearch implements IController {
public function processRequest($dispatcher) {
public function processRequest() {
$baseURL = UI::getBaseURL();
$favicon = Engine::param('favicon', 'favicon.ico');
$banner = Engine::param("station")." ".Engine::param("application");
Expand Down
4 changes: 2 additions & 2 deletions controllers/PrintTags.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 Down Expand Up @@ -48,7 +48,7 @@ class PrintTags implements IController {
],
];

public function processRequest($dispatcher) {
public function processRequest() {
header("Content-type: application/pdf");

$form = empty($_REQUEST["form"])?self::LABEL_FORM:$_REQUEST["form"];
Expand Down
2 changes: 1 addition & 1 deletion controllers/PushServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public static function sendAsyncNotification($show = null, $spin = null) {
socket_close($socket);
}

public function processRequest($dispatcher) {
public function processRequest() {
if(php_sapi_name() != "cli") {
http_response_code(400);
return;
Expand Down
2 changes: 1 addition & 1 deletion controllers/RSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static function htmlnumericentities($str) {
// $str);
}

public function processRequest($dispatcher) {
public function processRequest() {
$this->session = Engine::session();

header("Content-type: text/xml");
Expand Down
4 changes: 2 additions & 2 deletions controllers/RunDaily.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 Down Expand Up @@ -34,7 +34,7 @@
class RunDaily implements IController {
private $catCodes;

public function processRequest($dispatcher) {
public function processRequest() {
if(php_sapi_name() != "cli") {
http_response_code(400);
return;
Expand Down
4 changes: 2 additions & 2 deletions controllers/SSOLogin.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-2020 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 @@ -33,7 +33,7 @@ class SSOLogin implements IController {
private $action;
private $ssoOptions;

public function processRequest($dispatcher) {
public function processRequest() {
$params = SSOCommon::zkQSParams();
$state = $params["state"];
if($state) {
Expand Down
6 changes: 3 additions & 3 deletions custom/KzsuUIController.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-2020 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 @@ -36,8 +36,8 @@
* The file is provided as an example of a custom UI controller;
* it is not part of the basic functionality of Zookeeper Online.
*/
class KzsuUIController extends Main {
protected function emitBodyHeader($dispatcher) {
class KzsuUIController extends UIController {
protected function emitBodyHeader() {
$urls = Engine::param('urls');
$station_full = Engine::param('station_full');
?>
Expand Down
Loading

0 comments on commit f6b25d4

Please sign in to comment.