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

Language PHP Lines 186
MD5 Hash 2732c611ffb7f20f227eb52d7aba85af Estimated Cost $2,307 (why?)
Repository https://bitbucket.org/yelinaung/reborn.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php

namespace Reborn\Cores;

use Reborn\Cores\Exception\EventException as EventException;

/**
 * Event Class for Reborn CMS
 *
 * @package Cores
 * @author Reborn CMS Development Team
 **/
class Event
{

	/**
	 * Variable for events list
	 *
	 * @var array
	 */
	protected static $callbackEvents = array();

	/**
	 * Variable for callback method is not callable message
	 *
	 * @var string
	 */
	protected static $msg = 'Not callbable method { %s } for event name { %s } !';

	/**
	 * Variable for Log object
	 *
	 * @var object
	 */
	protected static $log;

	/**
	 * Initialize method for Event Class
	 *
	 * @param array $events Events array list
	 */
	public static function initialize($events = array())
	{
		static::$log = new Log;

		require CORES.'Event'.DS.'register'.EXT;

		//require CORES.'Config'.DS.'events'.EXT;

		if(! empty($events))
		{
			foreach($events as $k => $e)
			{
				static::add($e['name'], $e['callback']);
			}
		}
	}

	/**
	 * Add(Register) the Event.
	 * Don't use (.)dot operator at event name.
	 * If callback function is not callable, you can check log file.
	 * If you are developement process.
	 * You should set true for event.callback_throw in app config file.
	 * event.callback_throw is true, throw error msg when callback is not callable.
	 *
	 * @param string $name Event name (eg: blog_post_create)
	 * @param string $callback Callback function name.
	 */
	public static function add($name, $callback)
	{
		if(strpos($callback, '::'))
		{
			list($ns, $method) = explode('::', $callback);

			if(! is_callable(array($ns, $method)))
			{
				$msg = sprintf(static::$msg, $callback, $name);

				static::$log->error($msg);

				if(Config::get('app.event.callback_throw'))
				{
					throw new EventException($msg);
				}
			}
		}
		else
		{
			if(! is_callable($callback))
			{
		    	$msg = sprintf(static::$msg, $callback, $name);

				static::$log->error($msg);

		    	if(Config::get('app.event.callback_throw'))
				{
					throw new EventException($msg);
				}
		    }
		}

	    $eventName = strtolower($name);

	    static::$callbackEvents[$eventName][] = $callback;
	}

	/**
	 * Call(Trigger) the event.
	 *
	 * @param string $name Name of event
	 * @param array $data Data array for callback event (optional)
	 * @return void
	 */
	public static function call($name, $data = array())
	{
		$eventName = strtolower($name);

		if(isset(static::$callbackEvents[$eventName]))
		{
			foreach(static::$callbackEvents[$eventName] as $callback)
			{
				call_user_func_array($callback, array($data));
			}
		}
	}

	/**
	 * Check the given event name is have or not
	 *
	 * @param string $name Event name
	 * @return boolean
	 */
	public static function has($name)
	{
		$eventName = strtolower($name);

		return isset(static::$callbackEvents[$eventName]);
	}

	/**
	 * Remove(UnRegister) the given event name.
	 * Each event is an array.
	 * If you give name only, remove the all method from event name.
	 * If you want to remove event's some method from event name. use(.)
	 * eg ( eventname.method)
	 *
	 * @param string $name Name of the event
	 * @return void
	 */
	public static function remove($name)
	{
		$method = '';
		if(strpos($name, '.'))
		{
			list($name, $method) = explode('.', $name);
		}

		$eventName = strtolower($name);

		if(static::has($name))
		{
			if($method == '')
			{
				unset(static::$callbackEvents[$eventName]);
			}
			else
			{
				foreach(static::$callbackEvents[$eventName] as $k => $n)
				{
					if($method == $n)
					{
						unset(static::$callbackEvents[$eventName][$k]);
					}
				}
			}
		}
	}

	public static function getAll()
	{
		return static::$callbackEvents;
	}

} // END class Event
Back to Top