Browse Source

* Get new template engine implemented
* Allow column renaming

Nathaniel van Diepen 7 years ago
parent
commit
f48d37efb3
2 changed files with 94 additions and 7 deletions
  1. 56 7
      Data/template.class.php
  2. 38 0
      PDO/table.class.php

+ 56 - 7
Data/template.class.php

@@ -8,6 +8,16 @@
 	class Template {
 		use Events;
 		private static $templates = [];
+		private static $regex = [
+			'match'=>'/\{([^#\/][^}\n]+?)\}/i',
+			'each'=>'/\{#each ([^}]*)\}([\S\s]*)\{\/each\}/i',
+			'exist'=>'/\{#exist ([^}]*)\}([\S\s]*)\{\/exist\}/i',
+			'existelse'=>'/\{#exist ([^}]*)\}([\S\s]*)\{#else\}([\S\s]*)\{\/exist\}/i',
+			'ignore'=>'/\{#ignore\}([\S\s]*)\{\/ignore\}/i',
+			'ignored'=>'/\{#ignored (\d+?)\}/i',
+			'gettext'=>"/{_([^,}]+)(?:, ?([^},]+))*\}/i",
+			'gettext_string'=>'/^([\'"])(.+)\1$/i'
+		];
 		private $template;
 		private $name;
 		public function __construct(string $name, string $template, bool $is_file = false){
@@ -44,14 +54,53 @@
 			return $template->run($data);
 		}
 		public static function parse(string $template, $data){
-			$output = preg_replace_callback_array([
-				'/\{([^#\/][^}\n]+?)\}/i'=> function($match) use($data){
-					return $data[$match[1]] ?? '';
-				},
-				"/_\(['\"]?([^']+)['\"]?\)/i"=> function($match){
-					return _($match[1]);
+			$ignored = [];
+			// Handle {#ignore code}
+			$output = preg_replace_callback(static::$regex['ignore'], function($matches) use($ignored){
+				$ignored[] = $matches[1];
+				return '{#ignored '.count($ignored).'}';
+			}, $template);
+			// Handle {#each name}{/each}
+			$output = preg_replace_callback(static::$regex['each'], function($matches) use($data){
+				$output = '';
+				if(isset($data[$matches[1]])){
+					foreach($data[$matches[1]] as $item){
+						$output = static::parse($matches[2], $item);
+					}
 				}
-			], $template);
+				return $output;
+			}, $output);
+			// Handle {#exist name}{#else}{/exist}
+			$output = preg_replace_callback(static::$regex['existelse'], function($matches) use($data){
+				if(isset($data[$matches[1]])){
+					$output = static::parse($matches[2], $data);
+				}else{
+					$output = static::parse($matches[3], $data);
+				}
+				return $output;
+			}, $output);
+			// Handle {#exist name}{/exist}
+			$output = preg_replace_callback(static::$regex['exist'], function($matches) use($data){
+				if(isset($data[$matches[1]])){
+					return static::parse($data[$matches[2]], $data);
+				}
+				return '';
+			}, $output);
+			// Handle {gettext}
+			$output = preg_replace_callback(static::$regex['gettext'], function($matches) use($data){
+				$args = array_map(function($item) use($data){
+					if(preg_match(static::$regex['gettext_string'], $item)){
+						return preg_replace(static::$regex['gettext_string'], '\2', $item);
+					}else{
+						return $data[$item] ?? '';
+					}
+				}, array_slice($matches, 1));
+				return _(sprintf(...$args));
+			}, $output);
+			// Handle {name}
+			$output = preg_replace_callback(static::$regex['match'], function($matches) use($data){
+				return $data[$matches[1]] ?? '';
+			}, $output);
 			return $output;
 		}
 	}

+ 38 - 0
PDO/table.class.php

@@ -14,6 +14,7 @@
 		private $columns;
 		private $index;
 		private $foreignKey;
+		private $columns_renamed;
 		private $columns_removed;
 		private $index_removed;
 		private $foreignKey_removed;
@@ -51,6 +52,7 @@
 			$this->primaryKey = [];
 			$this->index = [];
 			$this->foreignKeys = [];
+			$this->columns_renamed = [];
 			$this->columns_removed = [];
 			$this->index_removed = [];
 			$this->foreignKey_removed = [];
@@ -161,6 +163,18 @@
 				unset($sql);
 				$this->exists();
 			}else{
+				if(count($this->columns_renamed) > 0){
+					$sql = "alter table `{$this->name}`";
+					foreach($this->columns_renamed as $new => $old){
+						if(isset($this->columns[$new])){
+							$sql .= " change `{$old}` {$pdo->stringColumn($new, $this->columns[$new])}";
+						}
+					}
+					$sql = rtrim($sql, ',');
+					if($pdo->exec($sql) === false){
+						throw new \Exception("Unable to update table {$this->name}\n{$this->pdo->getError()}");
+					}
+				}
 				$columns = array_filter($this->columns, function($item){
 					return (bool)$item['dirty'];
 				});
@@ -229,6 +243,27 @@
 			$this->exists();
 			$this->describe();
 		}
+		public function rename(string $type, string $old, string $new){
+			switch($type){
+				case self::COLUMN:
+					if(isset($this->columns[$old])){
+						if(!isset($this->columns[$new])){
+							$this->columns[$new] = $this->columns[$old];
+							unset($this->columns[$old]);
+							if(isset($this->columns_renamed[$old])){
+								unset($this->columns_renamed[$old]);
+							}
+							$this->columns_renamed[$new] = $old;
+						}else{
+							throw new \Exception("{$this->name}.{$new} already exists. Unable to rename {$this->name}.{$old}");
+						}
+					}
+				break;
+				default:
+					throw new \Exception("Renaming {$type} is not implemented");
+			}
+			return $this;
+		}
 		public function drop(string $type = null, string $name = null){
 			if(!is_null($type)){
 				switch($type){
@@ -239,6 +274,9 @@
 							}
 							unset($this->columns[$name]);
 						}
+						if(isset($this->columns_renamed[$name])){
+							unset($this->columns_renamed[$name]);
+						}
 					break;
 					case self::INDEX:
 						if(isset($this->index[$name])){