PageRenderTime 233ms CodeModel.GetById 80ms app.highlight 5ms RepoModel.GetById 106ms app.codeStats 0ms

/heart/reborn/src/Reborn/Cores/Event.php

https://bitbucket.org/yelinaung/reborn
PHP | 185 lines | 97 code | 27 blank | 61 comment | 13 complexity | 2732c611ffb7f20f227eb52d7aba85af MD5 | raw file
  1<?php
  2
  3namespace Reborn\Cores;
  4
  5use Reborn\Cores\Exception\EventException as EventException;
  6
  7/**
  8 * Event Class for Reborn CMS
  9 *
 10 * @package Cores
 11 * @author Reborn CMS Development Team
 12 **/
 13class Event
 14{
 15
 16	/**
 17	 * Variable for events list
 18	 *
 19	 * @var array
 20	 */
 21	protected static $callbackEvents = array();
 22
 23	/**
 24	 * Variable for callback method is not callable message
 25	 *
 26	 * @var string
 27	 */
 28	protected static $msg = 'Not callbable method { %s } for event name { %s } !';
 29
 30	/**
 31	 * Variable for Log object
 32	 *
 33	 * @var object
 34	 */
 35	protected static $log;
 36
 37	/**
 38	 * Initialize method for Event Class
 39	 *
 40	 * @param array $events Events array list
 41	 */
 42	public static function initialize($events = array())
 43	{
 44		static::$log = new Log;
 45
 46		require CORES.'Event'.DS.'register'.EXT;
 47
 48		//require CORES.'Config'.DS.'events'.EXT;
 49
 50		if(! empty($events))
 51		{
 52			foreach($events as $k => $e)
 53			{
 54				static::add($e['name'], $e['callback']);
 55			}
 56		}
 57	}
 58
 59	/**
 60	 * Add(Register) the Event.
 61	 * Don't use (.)dot operator at event name.
 62	 * If callback function is not callable, you can check log file.
 63	 * If you are developement process.
 64	 * You should set true for event.callback_throw in app config file.
 65	 * event.callback_throw is true, throw error msg when callback is not callable.
 66	 *
 67	 * @param string $name Event name (eg: blog_post_create)
 68	 * @param string $callback Callback function name.
 69	 */
 70	public static function add($name, $callback)
 71	{
 72		if(strpos($callback, '::'))
 73		{
 74			list($ns, $method) = explode('::', $callback);
 75
 76			if(! is_callable(array($ns, $method)))
 77			{
 78				$msg = sprintf(static::$msg, $callback, $name);
 79
 80				static::$log->error($msg);
 81
 82				if(Config::get('app.event.callback_throw'))
 83				{
 84					throw new EventException($msg);
 85				}
 86			}
 87		}
 88		else
 89		{
 90			if(! is_callable($callback))
 91			{
 92		    	$msg = sprintf(static::$msg, $callback, $name);
 93
 94				static::$log->error($msg);
 95
 96		    	if(Config::get('app.event.callback_throw'))
 97				{
 98					throw new EventException($msg);
 99				}
100		    }
101		}
102
103	    $eventName = strtolower($name);
104
105	    static::$callbackEvents[$eventName][] = $callback;
106	}
107
108	/**
109	 * Call(Trigger) the event.
110	 *
111	 * @param string $name Name of event
112	 * @param array $data Data array for callback event (optional)
113	 * @return void
114	 */
115	public static function call($name, $data = array())
116	{
117		$eventName = strtolower($name);
118
119		if(isset(static::$callbackEvents[$eventName]))
120		{
121			foreach(static::$callbackEvents[$eventName] as $callback)
122			{
123				call_user_func_array($callback, array($data));
124			}
125		}
126	}
127
128	/**
129	 * Check the given event name is have or not
130	 *
131	 * @param string $name Event name
132	 * @return boolean
133	 */
134	public static function has($name)
135	{
136		$eventName = strtolower($name);
137
138		return isset(static::$callbackEvents[$eventName]);
139	}
140
141	/**
142	 * Remove(UnRegister) the given event name.
143	 * Each event is an array.
144	 * If you give name only, remove the all method from event name.
145	 * If you want to remove event's some method from event name. use(.)
146	 * eg ( eventname.method)
147	 *
148	 * @param string $name Name of the event
149	 * @return void
150	 */
151	public static function remove($name)
152	{
153		$method = '';
154		if(strpos($name, '.'))
155		{
156			list($name, $method) = explode('.', $name);
157		}
158
159		$eventName = strtolower($name);
160
161		if(static::has($name))
162		{
163			if($method == '')
164			{
165				unset(static::$callbackEvents[$eventName]);
166			}
167			else
168			{
169				foreach(static::$callbackEvents[$eventName] as $k => $n)
170				{
171					if($method == $n)
172					{
173						unset(static::$callbackEvents[$eventName][$k]);
174					}
175				}
176			}
177		}
178	}
179
180	public static function getAll()
181	{
182		return static::$callbackEvents;
183	}
184
185} // END class Event