Scripts

Bundel Acties

Ongeveer iets meer dan een jaar ben ik nu redelijk serieus met php bezig en hier geregistreerd. Desalniettemin heb ik nooit wat gepost en log ik bijna nooit in. Gezien ik hier toch veel heb opgestoken en ook wel eens wat kritiek wil ontvangen op mijn classes post ik deze. Ik ben beniewd hoe jullie hem vinden. De class is geschreven vanuit de behoefte dat ik bij het ontwerpen van een cms, modules wilde laden en de functies van die modules wilde kunnen toevoegen aan bepaalde punten in het systeem. --------------------------------------------------------------------- Deze class maakt het mogelijk acties te bundelen. Het is dus een soort geheugen waar je acties (functies, methoden etc) in kunt bewaren zodat ze allemaal tegelijk kunnen worden uitgevoerd op een specifiek moment. Stel dat je modules laadt ergens in de header van je cms die functies bevatten die je later uitgevoerd wilt hebben dan komt dit vast van pas. Ook kun je je shutdown functies hierin bewaren en dit cluster alles laten uitvoeren wat er in het shutdown object bevind. Dit maakt het mogelijk om via een kleine omweg shutdown functies te registeren en te unregisteren, iets dat niet mogelijk is met php's standaard functies. Voor extra flexibiliteit is er de mogelijkheid om acties te bundelen in verschillende sequenties, zodat functies kunnen worden uitgevoerd in de gewenste volgorde. Binnenin de sequenties worden de acties uitgevoerd in de volgorde dat ze zijn toegevoegd aan de class. Neem voor gedetailleerde informatie het script zelf door. -------------- * Update 26-05-07 Heb de kritiek van Jelmer er in verwerkt. Verschillende functies voor het toevoegen aan een sequence en de default sequence.

bundel-acties
<?php
/*
	-------------------------------------------------------
	ACTION CLUSTER
	-------------------------------------------------------

	E.J. Klein Ovink <[email protected]>
	http://www.sparkle6.com
	
	Copyright (c) 2007, All rights reserved.
	
 	-------------------------------------------------------
	- I don't need a link or anything, just keep the stuff above in tact
	
	
	
	// 	DESCRIPTION
	
		This class allows you to create clusters of actions. It is a memory you can store actions in, so they can
		be launched all together at a specific moment.
		If you load modules in the header of a content managament system wich contain functions you want to be launched later or
		on another moment in the header this can be handy.		
		You can also store shutdown functions in a cluster and launch this cluster on shutdown, wich allows you to register and 
		unregister shutdown functions. This is not possible with php's standard functions.
		
		For extra flexibility you can arrange the actions in different sequences to make sure the actions will be launched in the 
		prefered order.
		Within the sequences the actions are launched in the order they are added to the action cluster.
			

		- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
		
		Example for registering a shutdown function:
		
		
			
			function foo($bar) {
				print $bar;
			}
				
			
			$actions	=	new ActionCluster();
			register_shutdown_function(array(&$actions, 'launch'));

			$actions->add('foo' , "<!-- If you check your html source, you see this printed -->");
	

		- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
*/




	
	class ActionCluster {

		var	$actions,	// 	(array)	Actions storage
			$sequences,	// 	(array)	Sequence storage
			$seq,		// 	(int)	Default sequence
			$id;		//	(int)	The id counter

		
		// Constructor
		function ActionCluster() {				
			$this->initialize();
		}

		function initialize() {
			$this->actions		=	array();	
			$this->sequences	=	array();
			$this->seq		=	1;			
			$this->id		=	1;						
		}
		
		
		
		
		/*	Add actions to action storage
		 *	
		 *	This method can be used to add new actions to the current action storage. Everytime an action
		 *	is added the id increases by 1
		 *	An action is added by giving its name as first argument followed by the actions own parameters.
		 *
		 */				
		function add() {			
	
			$parameters	= 	func_get_args();
			
			$this->saveSequence($this->seq);
			$this->saveAction($parameters, $this->seq);
		
		}
	

		/*	Add actions to a specified sequence
		 *	
		 *	Same as add() only this enables you to give a sequence as first parameter, followed by the action
		 *	and its parameters.
		 *
		 */		
		function addTo($seq) {			
	
			$parameters	= 	func_get_args();
			
			if( is_int($parameters[0]) )
				$seq	= 	array_shift($parameters); // The first argument can be used to set sequences
			else
				trigger_error("Given variable <code>\$seq = $seq</code> but should be an integer.");
			
			$this->saveSequence($seq);
			$this->saveAction($parameters, $seq);

		
		}
	
	
		/*	Save sequence
		 *	
		 *	This function saves the sequence
		 *	$seq = integer
		 *
		 */			
		function saveSequence($seq) {
			
			if( !isset($this->sequences[$seq]) )
				$this->sequences[$seq]	=	array();
			
			array_unshift($this->sequences[$seq], $this->id);		
		}
	
	
	
	
		/*	Save sequence
		 *	
		 *	This function saves the action and its parameters.
		 *	$parameters	= array , 0 => name , followed by parameters in the other keys
		 *
		 */		
		function saveAction($parameters, $seq) {
		
			$action	=	array_shift($parameters);	

			if( !is_callable($action) ) {
				trigger_error("<code>$action()</code> is no valid callback action");
				exit;
			} else {
				$this->actions[$this->id]	=	array('action' => $action, 'parameters' => $parameters, 'sequences' => $seq);	
				$this->id++;	
			}		
		}
	
	
	
	
		/*	Delete an action
		 *	
		 *	When a valid action id is passed as argument this function will delete that action
		 *	from the action cluster.
		 *
		 *	$id	= integer
		 *
		 */	
		function delete($id) {		
			$seq	=	$this->actions[$id]['sequences'];
			
			if( in_array($id, $this->sequences[$seq]) ){		
				list( $key )	=	 array_keys( $this->sequences[$seq], $id );		
				unset($this->actions[$id]);
				unset($this->sequences[$seq][$key]);
	
			} else {
				trigger_error("Id <code>$id</code> could not be found in sequence");
				exit;
			}
		}
	
		
		
		
		/*	Launch all actions
		 *
		 *	This method launches all the actions stored in the current action cluster. The sequence with the 
		 *	highest value first, and every action in a sequence in the order they are added.
		 *
		 */	
		function launch() {			
			krsort($this->sequences);
			
			foreach($this->sequences as $seq => $actions){
				
				sort($this->sequences[$seq]);	
				
				foreach ( $this->sequences[$seq] as $seq => $id ) {
					if( isset($this->actions[$id]['action']) )
						call_user_func_array($this->actions[$id]['action'], $this->actions[$id]['parameters']);
					else 
						trigger_error("Action where <code>\$id = $id</code> could not be launched.");
				}
								
			}
		}			



	}

?>

Reacties

0
Nog geen reacties.