Преглед на файлове

Added variable alias fucntionality.

Taddes Kroes преди 14 години
родител
ревизия
2198131dfc
променени са 2 файла, в които са добавени 70 реда и са изтрити 39 реда
  1. 64 23
      pquery.php
  2. 6 16
      pquery.sql.php

+ 64 - 23
pquery.php

@@ -47,6 +47,20 @@ class pQuery {
 	 */
 	static $accepts = array('boolean', 'integer', 'double', 'string', 'array', 'object', 'NULL');
 	
+	/**
+	 * A list of names of plugins that are required to run a plugin.
+	 * 
+	 * @var array
+	 */
+	static $require_plugins = array();
+	
+	/**
+	 * Aliases for the variable setter and getter.
+	 * 
+	 * @var string|array
+	 */
+	static $variable_alias = array();
+	
 	/**
 	 * The current variable.
 	 * 
@@ -128,6 +142,40 @@ class pQuery {
 		return $obj;
 	}
 	
+	/**
+	 * Try to load the file containing the utility class for a specific variable type.
+	 * 
+	 * @param mixed $type the variable type of the class to load.
+	 */
+	static function load_plugin($type) {
+		$path = PQUERY_ROOT.sprintf(self::PLUGIN_FILENAME_PATTERN, $type);
+		
+		if( !file_exists($path) )
+			return false;
+		
+		include_once $path;
+		
+		return true;
+	}
+	
+	/**
+	 * Include the nescessary files for the given plugins.
+	 */
+	static function require_plugins(/* $plugin1 [ , $plugin2, ... ] */) {
+		$plugins = func_get_args();
+		
+		foreach( $plugins as $plugin ) {
+			$path = PQUERY_ROOT.sprintf(self::PLUGIN_FILENAME_PATTERN, $plugin);
+			
+			if( !file_exists($path) ) {
+				return self::error('Required plugin "%s" could not be located (looked in "%s").',
+					$plugin, $path);
+			}
+			
+			include_once $path;
+		}
+	}
+	
 	/**
 	 * Parse the type of the given variable, and convert it if needed.
 	 * 
@@ -158,37 +206,27 @@ class pQuery {
 	}
 	
 	/**
-	 * Try to load the file containing the utility class for a specific variable type.
+	 * Getter for {@link variable}.
 	 * 
-	 * @param mixed $type the variable type of the class to load.
+	 * @see variable_alias
 	 */
-	static function load_plugin($type) {
-		$path = PQUERY_ROOT.sprintf(self::PLUGIN_FILENAME_PATTERN, $type);
-		
-		if( !file_exists($path) )
-			return false;
-		
-		include_once $path;
+	function __get($name) {
+		$class_name = get_class($this);
 		
-		return true;
+		if( in_array($name, (array)$class_name::$variable_alias) )
+			return $this->variable;
 	}
 	
 	/**
-	 * Include the nescessary files for the given plugins.
+	 * Setter for {@link variable}.
+	 * 
+	 * @see variable_alias
 	 */
-	static function require_plugins(/* $plugin1 [ , $plugin2, ... ] */) {
-		$plugins = func_get_args();
+	function __set($name, $value) {
+		$class_name = get_class($this);
 		
-		foreach( $plugins as $plugin ) {
-			$path = PQUERY_ROOT.sprintf(self::PLUGIN_FILENAME_PATTERN, $plugin);
-			
-			if( !file_exists($path) ) {
-				return self::error('Required plugin "%s" could not be located (looked in "%s").',
-					$plugin, $path);
-			}
-			
-			include_once $path;
-		}
+		if( in_array($name, (array)$class_name::$variable_alias) )
+			$this->variable = $value;
 	}
 }
 
@@ -231,6 +269,9 @@ function _p($variable, $plugin=null) {
 	return new $class_name($variable);
 }
 
+/**
+ * Set an alias for the bas class consistent with plugin aliases.
+ */
 class_alias('pQuery', '__p');
  
 ?>

+ 6 - 16
pquery.sql.php

@@ -21,6 +21,12 @@ class pQuerySql extends pQuery implements pQueryExtension {
 	 */
 	static $link;
 	
+	/**
+	 * @see pQuery::$variable_alias
+	 * @var string|array
+	 */
+	static $variable_alias = 'query';
+	
 	/**
 	 * The result of the current query.
 	 * 
@@ -158,22 +164,6 @@ class pQuerySql extends pQuery implements pQueryExtension {
 		return $func($this->result);
 	}
 	
-	/**
-	 * Getter for property 'query'.
-	 */
-	function __get($name) {
-		if( $name == 'query' )
-			return $this->variable;
-	}
-	
-	/**
-	 * Setter for property 'query'.
-	 */
-	function __set($name, $value) {
-		if( $name == 'query' )
-			$this->variable = $value;
-	}
-	
 	/**
 	 * Assert that the current query has been executed.
 	 */