Răsfoiți Sursa

Added base of template parser.

Taddes Kroes 14 ani în urmă
părinte
comite
232df72c9b
5 a modificat fișierele cu 119 adăugiri și 17 ștergeri
  1. 8 7
      index.php
  2. 1 1
      pquery.array.php
  3. 14 6
      pquery.php
  4. 3 3
      pquery.sql.php
  5. 93 0
      pquery.template.php

+ 8 - 7
index.php

@@ -2,7 +2,7 @@
 
 include_once '../debug.php';
 include_once 'pquery.php';
-__p::require_plugins('array', 'sql');
+__p::require_plugins('array', 'sql', 'template');
 
 // Array test
 /*$a = _p(range(0, 10));
@@ -11,16 +11,17 @@ while( !$a->is_empty() ) {
 	debug($a->pop(), $a->reverse()->pop());
 }*/
 
-
-
-echo '<br><br>';
-
 // SQL test
-$sql = _sql("select * from posts where slug = '[slug]'")
+/*$sql = _sql("select * from posts where slug = '[slug]'")
 		->set(array('slug' => 'contact'));
 $results = $sql->fetch_all('object');
 $results = _arr($results);
 
-debug($results);
+debug($results);*/
+
+__tpl::set_root('templates');
+$tpl = _tpl('test.tpl');
+
+debug($tpl->content);
 
 ?>

+ 1 - 1
pquery.array.php

@@ -51,7 +51,7 @@ class pQueryArray extends pQuery implements pQueryExtension {
  * @see pQuerySql::__construct
  */
 function _arr($array) {
-	return pQuery::create('pQueryArray', $array);
+	return pQuery::create('array', $array);
 }
 
 pQuery::extend('pQueryArray', 'array');

+ 14 - 6
pquery.php

@@ -113,11 +113,11 @@ class pQuery {
 	 * 
 	 * @param string $error The error message to display.
 	 */
-	static function error($error/*, $arg1, $arg2...*/) {
+	static function error($error /* , $arg1, $arg2... */) {
 		$args = func_get_args();
 		
 		if( DEBUG ) {
-			call_user_func_array('printf', $args);
+			echo nl2br(call_user_func_array('sprintf', $args));
 			//echo debug_backtrace();
 		}
 		
@@ -132,12 +132,20 @@ class pQuery {
 	 */
 	static function create() {
 		$args = func_get_args();
-		$class_name = array_shift($args);
-		$obj = $class_name === null ? new self() : new $class_name();
-		$variable = array_shift($args);
+		$plugin = array_shift($args);
 		
+		if( $plugin === null )
+			$class_name = 'self';
+		elseif( isset(self::$plugins[$plugin]) )
+			$class_name = self::$plugins[$plugin];
+		elseif( in_array($plugin, self::$plugins) )
+			$class_name = $plugin;
+		else
+			return self::error('Plugin "%s" does not exist.', $plugin);
+		
+		$obj = new $class_name();
 		$obj->arguments = $args;
-		$obj->set_variable($variable);
+		$obj->set_variable(array_shift($args));
 		
 		return $obj;
 	}

+ 3 - 3
pquery.sql.php

@@ -236,13 +236,13 @@ class pQuerySql extends pQuery implements pQueryExtension {
 /**
  * Shortcut constructor for {@link pQuerySql}.
  * 
- * @returns pQuerySql A new pQuerySql instance.
- * @see pQuerySql::__construct
+ * @param string $query A MySQL query to evaluate.
+ * @returns pQuerySql A new SQL query instance.
  */
 function _sql($query /* [ , $arg1, ... ] */) {
 	$args = func_get_args();
 	$query = array_shift($args);
-	array_unshift($args, 'pQuerySql', $query);
+	array_unshift($args, 'sql', $query);
 	
 	return call_user_func_array('pQuery::create', $args);
 }

+ 93 - 0
pquery.template.php

@@ -0,0 +1,93 @@
+<?php
+/**
+ * pQuery plugin for parsing templates.
+ * 
+ * @package pQuery
+ */
+
+/**
+ * @todo Documentation
+ * @property $ Alias for {@link pQuery::variable}.
+ */
+class pQueryTemplate extends pQuery implements pQueryExtension {
+	static $accepts = array('string' => 'open_template_file');
+	
+	/**
+	 * Root folders from which template files will be included.
+	 * 
+	 * @var string
+	 */
+	static $include_path = array();
+	
+	/**
+	 * @see pQuery::$variable_alias
+	 * @var string|array
+	 */
+	static $variable_alias = 'template';
+	
+	/**
+	 * Open the given template filename in the current variable.
+	 */
+	function open_template_file() {
+		$found = false;
+		
+		foreach( self::$include_path as $root ) {
+			$path = $root.$this->variable;
+			
+			if( is_file($path) ) {
+				$found = true;
+				break;
+			}
+		}
+		
+		if( !$found ) {
+			return self::error("Could not find template file \"%s\", looked in folders:\n%s",
+				$this->variable, implode("\n", self::$include_path));
+		}
+		
+		$this->content = file_get_contents($path);
+	}
+	
+	/**
+	 * Replace all include paths by a single new one.
+	 * 
+	 * @param str $path The path to set.
+	 * @param bool $relative Indicates whether the path is relative to the document root.
+	 */
+	static function set_root($path, $relative=true) {
+		self::$include_path = array();
+		self::add_root($path);
+	}
+	
+	/**
+	 * Add a new include path.
+	 * 
+	 * @param str $path The path to add.
+	 * @param bool $relative Indicates whether the path is relative to the document root.
+	 */
+	static function add_root($path, $relative=true) {
+		$relative && $path = PQUERY_ROOT.$path;
+		preg_match('%/$%', $path) || $path .= '/';
+		
+		if( !is_dir($path) )
+			return self::error('"%s" is not a directory.', $path);
+		
+		self::$include_path[] = $path;
+	}
+}
+
+/**
+ * Shortcut constructor for {@link pQueryTemplate}.
+ * 
+ * @param string $path The path to a template file.
+ * @returns pQueryTemplate A new template instance.
+ */
+function _tpl($path) {
+	return pQuery::create('tpl', $path);
+}
+
+__p::extend('pQueryTemplate', 'tpl');
+
+__tpl::set_root('');
+
+?>