Umieściłem swoją klasę Dyspozytora na phpclasses.org, oraz na swoim blogu, linki poniżej:

Dispatcher - Dyspozytor na phpclasses.org
Dispatcher - Dyspozytor na moim blogu


Wymagania 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:
  1. <?php
  2. /**
  3.  * @li gnu/agpl v3 or later
  4.  * @code utf8
  5.  * @version 0.1
  6.  * @author cojack from Aichra.pl
  7.  * @date 22.09.09
  8.  *
  9.  **/
  10.  
  11. /**
  12.  * We require a event handler
  13.  */
  14. require_once('HandlerShow.php');
  15.  
  16.  
  17. /**
  18.  * @class Dispatcher
  19.  * @throw Exceptions
  20.  * @access final
  21.  *
  22.  * Can't extend from it, it's a finall class.
  23.  * Class auto call a handled class "name" from event.
  24.  *
  25.  */
  26.  
  27. final class Dispatcher {
  28.  
  29. private $_handle;
  30. private $_response;
  31.  
  32. /**
  33.   * @method __construct
  34.   * @access public
  35.   * @param string a event name
  36.   * @return void
  37.   *
  38.   * Method set a event.
  39.   *
  40.   */
  41. public function __construct($event) {
  42. $this->_handle = $event;
  43. }
  44.  
  45. /**
  46.   * @method handleEvent
  47.   * @access public
  48.   * @param void
  49.   * @return void
  50.   *
  51.   * That event handle is a class "name".
  52.   * Method check that handle exist.
  53.   *
  54.   */
  55. public function handleEvent() {
  56. try {
  57. $name = 'Handler'.ucfirst($this->_handle);
  58. if ( class_exists("$name") ) {
  59. $handObj = new $name($this->_handle);
  60. $this->_response = $handObj->handledEvent();
  61. } else {
  62. throw new Exception ('Can\'t be handled this Event');
  63. }
  64. } catch (Exception $e) {
  65. printf ('Error: %s',$e->getMessage());
  66. }
  67. }
  68.  
  69. /**
  70.   * @method getResponse
  71.   * @access public
  72.   * @param void
  73.   * @return void
  74.   *
  75.   * Method return a _response.
  76.   *
  77.   */
  78. public function getResponse() {
  79. return $this->_response;
  80. }
  81.  
  82. }



Kod klasy EventHandler, plik EventHandler.php:
  1. <?php
  2. /**
  3.  * @li gnu/agpl v3 or later
  4.  * @code utf8
  5.  * @version 0.1
  6.  * @author cojack from Aichra.pl
  7.  * @date 22.09.09
  8.  *
  9.  **/
  10.  
  11. /**
  12.  * We require a Handled interface
  13.  */
  14. require_once('./Handled.php');
  15.  
  16. /**
  17.  * @class EventHandler
  18.  * @implements Handled
  19.  * @access abstract
  20.  *
  21.  * We'll extending from this class,
  22.  * so for example we can here storage a function with access protected to connect to db
  23.  * and we have to have a abstract function implemented from interface Handled a handledEvent()
  24.  *
  25.  */
  26.  
  27. abstract class EventHandler implements Handled {
  28.  
  29. /**
  30.   * @method dbConn
  31.   * @access public
  32.   * @param void
  33.   * @return resource - db handle
  34.   *
  35.   * Method to connect to db
  36.   *
  37.   */
  38. protected function dbConn() {
  39. // some connection to db
  40. $dbHandle = true; // only for example
  41. return $dbHandle;
  42. }
  43.  
  44. /**
  45.   * @method handedEvent()
  46.   * @param void
  47.   * @access abstract
  48.   *
  49.   */
  50. public function handledEvent() {}
  51. }


Kod klasy Handled, plik Handled.php:
  1. <?php
  2. /**
  3.  * @li gnu/agpl v3 or later
  4.  * @code utf8
  5.  * @version 0.1
  6.  * @author cojack from Aichra.pl
  7.  * @date 22.09.09
  8.  *
  9.  **/
  10.  
  11.  
  12. /**
  13.  * @interface Handled
  14.  *
  15.  */
  16.  
  17. interface Handled {
  18.  
  19. /**
  20.   * @method handedEvent()
  21.   * @param void
  22.   * @access abstract
  23.   *
  24.   */
  25. public function handledEvent();
  26. }


Kod klasy HandlerShow, plik HandlerShow.php: (jest to również przykład użycia klasy Dispatcher)
  1. <?php
  2. /**
  3.  * @li gnu/agpl v3 or later
  4.  * @code utf8
  5.  * @version 0.1
  6.  * @author cojack from Aichra.pl
  7.  * @date 22.09.09
  8.  *
  9.  **/
  10.  
  11. /**
  12.  * We require a Event Handler abstract class
  13.  */
  14. require_once('EventHandler.php');
  15.  
  16. /**
  17.  * @class HandlerShow
  18.  *
  19.  * An example of usage a dispatcher, ofcourse it's not good idea to static require a event handler in dispatcher
  20.  *
  21.  */
  22.  
  23. class HandlerShow extends EventHandler {
  24.  
  25. private $_handle;
  26.  
  27. public function __construct($event){
  28. $this->_handle = $event;
  29. }
  30.  
  31. public function handledEvent(){
  32. // so for example we can here get a db handle from extended abstract class, like it:
  33. $dbHandle = parent::dbConn();
  34.  
  35. // here we can also trows exceptions, and it'll be catched in Dispatcher
  36.  
  37. // also we can call a method from model to get a some data from db
  38. /*
  39.   $articlesObj = new ArticlesModel($dbHandle);
  40.   return $articlesObj->getAllArticles();
  41.   */
  42.  
  43. // or just for example we will print some text
  44. return ('Hello World');
  45. }
  46.  
  47. }


Plik index.php, wyjaśnione jak to "coś" uruchomić:
  1. <?php
  2. /**
  3.  * @li gnu/agpl v3 or later
  4.  * @code utf8
  5.  * @version 0.1
  6.  * @author cojack from Aichra.pl
  7.  * @date 22.09.09
  8.  *
  9.  **/
  10.  
  11. require_once ('Dispatcher.php');
  12.  
  13. $disObj = new Dispatcher($_GET['event']);
  14. $disObj->handleEvent();
  15.  
  16. //tpl etc...
  17. // ...
  18. // ...
  19.  
  20. // using some templates (Smarty here)
  21. //$tpl->assign('content',$disObj->getResponse());
  22.  
  23. // or plain text
  24. echo $disObj->getResponse();
  25.  
  26. // set the ulr to index.php?event=show
  27.  
  28. // else code
  29. // ...


Kod sprawdzony, działa poprawnie.

Pozdrawiam, mam nadzieję że się komuś przyda.