Permalink
Browse files

Model 2.3: introduced CuTeML {break:var} syntax in loops.

Storage 1.1: (almost) full documented; more structured and robust code.
  • Loading branch information...
diegolamonica committed Aug 8, 2012
1 parent 92cd60f commit 9e22cca8099d68c94e04e0fa55f6dec0874fea4d
Showing with 438 additions and 204 deletions.
  1. +93 −71 classes/Model.php
  2. +158 −133 classes/Storage.php
  3. +119 −0 classes/Storage/CookieStorage.php
  4. +58 −0 classes/Storage/SessionStorage.php
  5. +10 −0 classes/interfaces/iStorage.php
View
@@ -1,7 +1,7 @@
<?php
/**
* @author Diego La Monica
* @version 2.2
* @version 2.3
* @name Model
* @package ALPHA
* @uses Debugger
@@ -36,8 +36,12 @@ class Model extends Debugger {
* - set the scope (public / private) for the class methods
* - Improved documentation
*
* V 2.3
* - Introduced the new cuteml element {break:var}
* - removed an undefined variable from replaceVar() method
*
*/
const VERSION = '2.2';
const VERSION = '2.3';
const KEYWORD_PHP_BLOCK_START = 'php';
const KEYWORD_PHP_BLOCK_END = 'phpend';
@@ -65,6 +69,9 @@ class Model extends Debugger {
*/
const KEYWORD_LOOP_END = 'loop';
const KEYWORD_LOOP_BREAK = 'break';
/**
* Keyword to identify a variable in the template
* @var String
@@ -143,7 +150,9 @@ class Model extends Debugger {
private $buffer = '';
private $storedFromCache = false;
private $_doNotSendHeader = false;
private $currentIterationBlock = '';
private $loopBreakFor = '';
/**
* Contain all the variables expoesed to the template
* @var array
@@ -373,7 +382,7 @@ private function replaceVar($variableName, $variableValue, &$variableContainer){
/*
* Going deep further
*/
self::createVar($variableNames[1], $variableValue, $variableContainer[$variableName], $method);
self::createVar($variableNames[1], $variableValue, $variableContainer[$variableName]);
}
}
@@ -832,14 +841,16 @@ public function getBuffer(){
private function storeCurrentStatus(){
return array(
'_doNotSendHeader' => $this->_doNotSendHeader,
'buffer' => $this->buffer,
'ignoreCache' => $this->ignoreCache,
'inLoop' => $this->inLoop,
'isPlugin' => $this->isPlugin,
'storedFromCache' => $this->storedFromCache,
'viewFileName' => $this->viewFileName,
'viewName' => $this->viewName
'_doNotSendHeader' => $this->_doNotSendHeader,
'buffer' => $this->buffer,
'ignoreCache' => $this->ignoreCache,
'inLoop' => $this->inLoop,
'isPlugin' => $this->isPlugin,
'storedFromCache' => $this->storedFromCache,
'viewFileName' => $this->viewFileName,
'viewName' => $this->viewName,
'currentIterationBlock' => $this->currentIterationBlock,
'loopBreakFor' => $this->loopBreakFor
);
}
@@ -999,26 +1010,26 @@ public function render($bufferedOutput=false){
if(is_null($tempResult)){
# I don't like this way to get data here, because it has a lot of memory usage
# Lambda function never destruct... is it a PHP bug or am I unable to do it???
# Look for a solution because it's a thing useful out of there too!
#$lambdaFunction = create_function("", "return $blockName;");
#$temporaryObject = $lambdaFunction();
# And I don't like this way too... it's weird and insecure.
#eval( '$temporaryObject='. $blockName . ';');
# I don't like this way to get data here, because it has a lot of memory usage
# Lambda function never destruct... is it a PHP bug or am I unable to do it???
# Look for a solution because it's a thing useful out of there too!
#$lambdaFunction = create_function("", "return $blockName;");
#$temporaryObject = $lambdaFunction();
# And I don't like this way too... it's weird and insecure.
#eval( '$temporaryObject='. $blockName . ';');
# This way is cleaner than both lambda and eval.
$availableObjects = array();
isset($_GET) && $availableObjects['$_GET'] = $_GET;
isset($_POST) && $availableObjects['$_POST'] = $_POST;
isset($_COOKIE) && $availableObjects['$_COOKIE'] = $_COOKIE;
isset($_ENV) && $availableObjects['$_ENV'] = $_ENV;
isset($_FILES) && $availableObjects['$_FILES'] = $_FILES;
isset($_REQUEST) && $availableObjects['$_REQUEST'] = $_REQUEST;
isset($_SERVER) && $availableObjects['$_SERVER'] = $_SERVER;
isset($_SESSION) && $availableObjects['$_SESSION'] = $_SESSION;
$availableObjects = array();
isset($_GET) && $availableObjects['$_GET'] = $_GET;
isset($_POST) && $availableObjects['$_POST'] = $_POST;
isset($_COOKIE) && $availableObjects['$_COOKIE'] = $_COOKIE;
isset($_ENV) && $availableObjects['$_ENV'] = $_ENV;
isset($_FILES) && $availableObjects['$_FILES'] = $_FILES;
isset($_REQUEST) && $availableObjects['$_REQUEST'] = $_REQUEST;
isset($_SERVER) && $availableObjects['$_SERVER'] = $_SERVER;
isset($_SESSION) && $availableObjects['$_SESSION'] = $_SESSION;
if(isset($availableObjects[$blockName])){
/*
@@ -1050,11 +1061,12 @@ public function render($bufferedOutput=false){
* maybe in the will we need to discuss about fragmented cache.
*
*/
$this->inLoop = true;
$this->_doNotSendHeader = true;
$this->ignoreCache = true;
$this->storedFromCache = false;
$this->inLoop = true;
$this->_doNotSendHeader = true;
$this->ignoreCache = true;
$this->storedFromCache = false;
$this->currentIterationBlock = $blockName;
$i = 0;
/*
* Storing the current iterator (useful for nested var).
@@ -1063,6 +1075,7 @@ public function render($bufferedOutput=false){
$totalResults = count($tempResult);
$tmpBufferArray = array();
foreach($tempResult as $key => &$value){
$this->buffer = $loopBlock;
@@ -1085,48 +1098,48 @@ public function render($bufferedOutput=false){
/**
* @since 2.2
*/
#$this->setVar('iterator.prev', $lastVariables);
self::$variables['iterator']['count'][0] = $totalResults;
#$this->setVar('iterator.prev', $lastVariables);
self::$variables['iterator']['count'][0] = $totalResults;
#$this->setVar('iterator.last', ($i == $totalResults));
self::$variables['iterator']['last'] = ($i == $totalResults);
#$this->setVar('iterator.prev', $lastVariables);
self::$variables['iterator']['prev'] = $lastVariables;
/*
* Creating the object structure for the current iterator item.
* Here an hypotetic structure like:
*
* buildings: [
* 0 => {
* 'type' => 'home',
* 'place' => 'Italy'
* },
* 1 => {
* 'type' => 'factory',
* 'place' => 'New York'
* }
* ]
*
* adds to the building structure the direct keys 'type' and 'place' for each
* iteration. The above structure would become (in example for the first iteration)
*
* buildings: [
* 0 => {
* 'type' => ['home'],
* 'place' => ['Italy']
* },
* 1 => {
* 'type' => ['factory'],
* 'place' => ['New York']
* },
* type: ['home'],
* place: ['Italy']
* ]
*
*
*/
/*
* Creating the object structure for the current iterator item.
* Here an hypotetic structure like:
*
* buildings: [
* 0 => {
* 'type' => 'home',
* 'place' => 'Italy'
* },
* 1 => {
* 'type' => 'factory',
* 'place' => 'New York'
* }
* ]
*
* adds to the building structure the direct keys 'type' and 'place' for each
* iteration. The above structure would become (in example for the first iteration)
*
* buildings: [
* 0 => {
* 'type' => ['home'],
* 'place' => ['Italy']
* },
* 1 => {
* 'type' => ['factory'],
* 'place' => ['New York']
* },
* type: ['home'],
* place: ['Italy']
* ]
*
*
*/
$this->setVar($blockName, $value);
// Processing and rendering the structure for each iteration
@@ -1141,6 +1154,11 @@ public function render($bufferedOutput=false){
// Same fate for the iterator object.
$this->clearVar('iterator');
if(substr($blockName.".", 0, strlen($this->loopBreakFor.".")) == $this->loopBreakFor."."){
if($blockName == $this->loopBreakFor) $this->loopBreakFor = '';
break;
}
}
@@ -1160,7 +1178,11 @@ public function render($bufferedOutput=false){
$buffer = str_replace($items[0].$loopBlock.'{' . self::KEYWORD_LOOP_END.'}', $tmpBuffer, $buffer);
break;
case self::KEYWORD_LOOP_BREAK:
$replacement = $items[0];
$buffer = str_replace($replacement, '', $buffer);
$this->loopBreakFor = $value;
break;
case self::KEYWORD_IF_START:
list($ifBlock, $elseBlock) = $this->ifBlockSearch($buffer, $items[0], self::KEYWORD_IF_START, self::KEYWORD_IF_END, self::KEYWORD_IF_ELSE);
$replacement = $items[0];
Oops, something went wrong.

0 comments on commit 9e22cca

Please sign in to comment.