Location: PHPKode > projects > Aukyla Platform > base/Signal.php
<?php
/*
     Signal.php, a simple signalling system for PHP
     Copyright (C) 2003-2005 Arend van Beelen, Auton Rijnsburg

     This program is free software; you can redistribute it and/or modify it
     under the terms of the GNU General Public License as published by the Free
     Software Foundation; either version 2 of the License, or (at your option)
     any later version.

     This program is distributed in the hope that it will be useful, but WITHOUT
     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
     more details.

     You should have received a copy of the GNU General Public License along
     with this program; if not, write to the Free Software Foundation, Inc.,
     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

     For any questions, comments or whatever, you may mail me at: hide@address.com
*/

/**
 * @brief Class that mimics Qt-like signal-slot handling.
 *
 * You can connect any number of slots to any number of signals. Every time a
 * signal is emitted, the slots connected to it, get called.
 */
class Signal
{
	private function instance()
	{
		if(self::$instance == null)
		{
			self::$instance = new Signal();
		}

		return self::$instance;
	}

	private function __construct()
	{
		$this->slots = array();
	}

	/**
	 * Connects a signal to a function of a given class.
	 *
	 * @param signal The name of the signal to connect to.
	 * @param slot   Callback function which should be called when the
	 *               signal is emitted.
	 */
	public static function connect($signal, $slot)
	{
		$instance = self::instance();

		// add the signal/slot relation
		$instance->slots[$signal][] = $slot;

		return;
	}

	/**
	 * Emits a signal. All functions connected to this signal are called
	 * with the arguments given after the signal argument.
	 *
	 * @param signal The signal to be emitted.
	 */
	public static function emit($signal)
	{
		$instance = self::instance();

		if(!isset($instance->slots[$signal]))
		{
			return;
		}

		// get the argument list
		$args = func_get_args();
		unset($args[0]); // the first argument was the signal name itself

		// call all slots for this signal
		foreach($instance->slots[$signal] as $slot)
		{
			call_user_func_array($slot, $args);
		}

		return;
	}

	private static $instance = null;
	private $slots;
}

?>
Return current item: Aukyla Platform