Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.57 KB | None | 0 0
  1. <?php
  2. require_once 'Autoload.php';
  3.  
  4. class Event{
  5.  
  6.     static $event_action = array();
  7.     static $subscribers = array();
  8.     static $type = NULL;
  9.    
  10.     function __construct($event_type, $properties){
  11.         self::$type = $event_type;
  12.         if(!empty($properties) && gettype($properties)=='array'){
  13.             foreach($properties as $property_name => $property_value){
  14.                 $this->{$property_name} = $property_value;
  15.             }
  16.         }
  17.     }
  18.  
  19.     public function dispatch(){
  20.         static::get_subscribers();
  21.         static::notify();
  22.     }
  23.  
  24.     static function subscribe($event_type, EventObserver $observer){
  25.         if(empty(static::$event_action[$event_type])){
  26.             static::$event_action[$event_type] = array();
  27.         }
  28.  
  29.         static::$event_action[$event_type][] =  $observer;
  30.     }
  31.  
  32.    
  33.  
  34.     private static function get_subscribers(){
  35.         static::$subscribers = static::$event_action[self::$type];
  36.     }
  37.  
  38.     private static function notify(){
  39.         if(!empty(static::$subscribers)){
  40.             foreach(static::$subscribers as $subscriber){
  41.                 self::exec($subscriber);
  42.             }
  43.         }
  44.     }
  45.  
  46.     static function exec($subscriber){
  47.         var_dump($subscriber);
  48.         $subscriber->action->__invoke();
  49.     }
  50.  
  51. }
  52.  
  53.     class EventObserver{
  54.         public function __construct($action){
  55.             if(!empty($action)){
  56.                 $this->action = $action;
  57.             }
  58.         }
  59.  
  60.         public function notify(){
  61.             $this->action();
  62.         }
  63.     }      
  64.  
  65. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement