Location: PHPKode > scripts > lorenphplib_patternizer > lorenphplib_patternizer/patternizer_commandparser.class.php
<?
	
	$GLOBALS["INCLUDED_LIBS"]["PATTERNIZER_COMMANDPARSER"] = true;
	
	if (!$GLOBALS["INCLUDED_LIBS"]["PATTERNIZER"])
		include LIBDIR."/patternizer/patternizer.class.php";

	/**
	* Command parser for a Patternizer {@link patternizer}
	*
	* Based on an internal list of associated foreign methods of foreign objects, it allows to perform calls to those methods within a tagged command inside a pattern.
	* It also allows to pass parameters to the methods. This way, it provides a full framework to build pattern-based applications.
	* 
	*
	* @author		Llorenç Herrera <hide@address.com>
	* @version		1.0
	* @package		patternizer
	*/
	class patternizer_commandparser extends patternizer
	{
		/**
		* @access private
		*/
		var $separator = ":";
	
		/**
		* @access private
		*/
		var $classes;

		/**
		* Sets up some methods so they can be called inside a pattern
		*
		* Use this method to specify a list of methods of some of your objects, so they can be later be called from inside a pattern.
		* A typical call to this method will look as:
		* $patternizer->addclasses
		* (
		* 	array
		* 	(
		* 		"myclass"	=> array
		* 						(
		* 							"pointer"	=> &$myclass, // Note the & symbol, it points to one of your objects
		* 							"methods"	=> array
		* 											(
		* 												"dosomething"	=> 0, // The method name inside your object, and the number of arguments receives.
		* 												"dosomething2"	=> 1,
		* 												"dosomething3"	=> 2
		* 											)
		* 						),
		* 		"myclass2"	=> array
		* 						(
		* 							"pointer"	=> &$myclass2,
		* 							"methods"	=> array
		* 											(
		* 												"dosomething2"	=> 1
		* 											)
		* 						)
		* 	)
		* );
		* After that, when any matching command was found inside the pattern, the corresponding method will be called with the specified parameters (if any). The return of your method (whatever it is) will be replaced in the pattern, just in the same place where the command were found.
		* An example in-pattern command to call the method "dosomething2" in the example object "$myclass" will be as: <%myclass:dosomething2:parameter1%>
		*
		* @param array $classes An array of methods to be lately interpreted by the parser
		*/
		function addclasses ($classes)
		{
			while (list ($classname, $specs) = each ($classes))
			{
				$this->addclass ($classname, $specs["pointer"]);
				while (list ($methodname, $parameters) = each ($specs["methods"]))
					$this->addmethod ($classname, $methodname, $parameters);
			}
		}
		
		/**
		* @access private
		*/
		function addclass ($classname, &$classpointer)
		{
			$this->classes[$classname]["pointer"] =& $classpointer;
		}

		/**
		* @access private
		*/
		function addmethod ($classname, $method, $parameters)
		{
			$this->classes[$classname]["methods"][$method] = $parameters;
		}
		
		/**
		* Sets the separator character used to divide object, method and parameters in the tagged commands inside the pattern.
		*
		* Its default value is :
		*
		* @param string $separator The character used to divide object, method and parameters in the tagged commands inside the pattern
		*/
		function setseparator ($separator)
		{
			$this->separator = $separator;
		}

		/**
		* @access private
		*/
		function command ($command)
		{
			if ($command == "")
				return $this->error ("No command specified");
			
			$spec = explode ($this->separator, $command);

			$class = $spec[0];
			
			if (!isset ($this->classes[$class]))
				return $this->error ("Class <b>$class</b> not defined");
			
			if (sizeof ($spec) == 1)
				return $this->error ("No method specified for class <b>$class</b>");
			
			$method = $spec[1];
			
			if (!isset ($this->classes[$class]["methods"][$method]))
				return $this->error ("Method <b>$class:$method</b> not defined");
			
			$numparameters = sizeof ($spec) - 2;
			
			if ($numparameters < $this->classes[$class]["methods"][$method])
				return $this->error ("Too less parameters for method <b>$class:$method</b>");
			
			if ($numparameters > $this->classes[$class]["methods"][$method])
				return $this->error ("Too many parameters for method <b>$class:$method</b>");

			switch ($numparameters)
			{
				case 0:		$retr = $this->classes[$class]["pointer"]->$method (); break;
				case 1:		$retr = $this->classes[$class]["pointer"]->$method ($spec[2]); break;
				case 2:		$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4]); break;
				case 3:		$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5]); break;
				case 4:		$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6]); break;
				case 5:		$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7]); break;
				case 6:		$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8]); break;
				case 7:		$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8],$spec[9]); break;
				case 8:		$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8],$spec[9],$spec[10]); break;
				case 9:		$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8],$spec[9],$spec[10],$spec[11]); break;
				case 10:	$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8],$spec[9],$spec[10],$spec[11],$spec[12]); break;
				case 11:	$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8],$spec[9],$spec[10],$spec[11],$spec[12],$spec[13]); break;
				case 12:	$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8],$spec[9],$spec[10],$spec[11],$spec[12],$spec[13],$spec[14]); break;
				case 13:	$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8],$spec[9],$spec[10],$spec[11],$spec[12],$spec[13],$spec[14],$spec[15]); break;
				case 14:	$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8],$spec[9],$spec[10],$spec[11],$spec[12],$spec[13],$spec[14],$spec[15],$spec[16]); break;
				case 15:	$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8],$spec[9],$spec[10],$spec[11],$spec[12],$spec[13],$spec[14],$spec[15],$spec[16],$spec[17]); break;
				case 16:	$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8],$spec[9],$spec[10],$spec[11],$spec[12],$spec[13],$spec[14],$spec[15],$spec[16],$spec[17],$spec[18]); break;
				case 17:	$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8],$spec[9],$spec[10],$spec[11],$spec[12],$spec[13],$spec[14],$spec[15],$spec[16],$spec[17],$spec[18],$spec[19]); break;
				case 18:	$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8],$spec[9],$spec[10],$spec[11],$spec[12],$spec[13],$spec[14],$spec[15],$spec[16],$spec[17],$spec[18],$spec[19],$spec[20]); break;
				case 19:	$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8],$spec[9],$spec[10],$spec[11],$spec[12],$spec[13],$spec[14],$spec[15],$spec[16],$spec[17],$spec[18],$spec[19],$spec[20],$spec[21]); break;
				case 20:	$retr = $this->classes[$class]["pointer"]->$method ($spec[2],$spec[3],$spec[4],$spec[5],$spec[6],$spec[7],$spec[8],$spec[9],$spec[10],$spec[11],$spec[12],$spec[13],$spec[14],$spec[15],$spec[16],$spec[17],$spec[18],$spec[19],$spec[20],$spec[21],$spec[22]); break;
				default:
					return $this->error ("Too many parameters to treat, only 20 allowed");
					break;
			}
			
			return $retr;
		}
		
	}

?>
Return current item: lorenphplib_patternizer