Umieściłem swoją klasę Dyspozytora na phpclasses.org, oraz na swoim blogu, linki poniżej:
Dispatcher - Dyspozytor na phpclasses.orgDispatcher - Dyspozytor na moim bloguWymagania co do php: PHP >= 5
Szersze omówienie zostało przedstawione na blogu, o co chodzi z tą klasą (w skrócie), klasa ładuje eventy, te eventy możemy sobie przerobić na controlery, i dzięki temu jesteśmy dwa kroki do przodu z ładowaniem klas, sprawdzamy czy dana klasa istnieje, rejestrujemy klasę, wywołujemy z niej metodę w której zwracamy wynik operacji jako np widok.
Kod klasy Dispacher, plik Dispatcher.php:
<?php
/**
* @li gnu/agpl v3 or later
* @code utf8
* @version 0.1
* @author cojack from Aichra.pl
* @date 22.09.09
*
**/
/**
* We require a event handler
*/
require_once('HandlerShow.php');
/**
* @class Dispatcher
* @throw Exceptions
* @access final
*
* Can't extend from it, it's a finall class.
* Class auto call a handled class "name" from event.
*
*/
final class Dispatcher {
private $_handle;
private $_response;
/**
* @method __construct
* @access public
* @param string a event name
* @return void
*
* Method set a event.
*
*/
public function __construct($event) {
$this->_handle = $event;
}
/**
* @method handleEvent
* @access public
* @param void
* @return void
*
* That event handle is a class "name".
* Method check that handle exist.
*
*/
public function handleEvent() {
try {
$name = 'Handler'.ucfirst($this->_handle
); if ( class_exists("$name") ) {
$handObj = new $name($this->_handle);
$this->_response = $handObj->handledEvent();
} else {
throw new Exception ('Can\'t be handled this Event');
}
} catch (Exception $e) {
printf ('Error: %s',$e->getMessage()); }
}
/**
* @method getResponse
* @access public
* @param void
* @return void
*
* Method return a _response.
*
*/
public function getResponse() {
return $this->_response;
}
}
Kod klasy EventHandler, plik EventHandler.php:
<?php
/**
* @li gnu/agpl v3 or later
* @code utf8
* @version 0.1
* @author cojack from Aichra.pl
* @date 22.09.09
*
**/
/**
* We require a Handled interface
*/
require_once('./Handled.php');
/**
* @class EventHandler
* @implements Handled
* @access abstract
*
* We'll extending from this class,
* so for example we can here storage a function with access protected to connect to db
* and we have to have a abstract function implemented from interface Handled a handledEvent()
*
*/
abstract class EventHandler implements Handled {
/**
* @method dbConn
* @access public
* @param void
* @return resource - db handle
*
* Method to connect to db
*
*/
protected function dbConn() {
// some connection to db
$dbHandle = true; // only for example
return $dbHandle;
}
/**
* @method handedEvent()
* @param void
* @access abstract
*
*/
public function handledEvent() {}
}
Kod klasy Handled, plik Handled.php:
<?php
/**
* @li gnu/agpl v3 or later
* @code utf8
* @version 0.1
* @author cojack from Aichra.pl
* @date 22.09.09
*
**/
/**
* @interface Handled
*
*/
interface Handled {
/**
* @method handedEvent()
* @param void
* @access abstract
*
*/
public function handledEvent();
}
Kod klasy HandlerShow, plik HandlerShow.php: (jest to również przykład użycia klasy Dispatcher)
<?php
/**
* @li gnu/agpl v3 or later
* @code utf8
* @version 0.1
* @author cojack from Aichra.pl
* @date 22.09.09
*
**/
/**
* We require a Event Handler abstract class
*/
require_once('EventHandler.php');
/**
* @class HandlerShow
*
* An example of usage a dispatcher, ofcourse it's not good idea to static require a event handler in dispatcher
*
*/
class HandlerShow extends EventHandler {
private $_handle;
public function __construct($event){
$this->_handle = $event;
}
public function handledEvent(){
// so for example we can here get a db handle from extended abstract class, like it:
$dbHandle = parent::dbConn();
// here we can also trows exceptions, and it'll be catched in Dispatcher
// also we can call a method from model to get a some data from db
/*
$articlesObj = new ArticlesModel($dbHandle);
return $articlesObj->getAllArticles();
*/
// or just for example we will print some text
return ('Hello World');
}
}
Plik index.php, wyjaśnione jak to "coś" uruchomić:
<?php
/**
* @li gnu/agpl v3 or later
* @code utf8
* @version 0.1
* @author cojack from Aichra.pl
* @date 22.09.09
*
**/
require_once ('Dispatcher.php');
$disObj = new Dispatcher($_GET['event']);
$disObj->handleEvent();
//tpl etc...
// ...
// ...
// using some templates (Smarty here)
//$tpl->assign('content',$disObj->getResponse());
// or plain text
echo $disObj->getResponse();
// set the ulr to index.php?event=show
// else code
// ...
Kod sprawdzony, działa poprawnie.
Pozdrawiam, mam nadzieję że się komuś przyda.
Ten post edytował cojack 27.09.2009, 09:44:40