Commit 7c2e53a7 by seldaek

BC Break: Dwoo::isArray had to be fixed and it has been split up in 3 methods,…

BC Break: Dwoo::isArray had to be fixed and it has been split up in 3 methods, isArray (for array access), isTraversable (for foreach) and count (just a helper that counts anything). It won't affect you unless you built some plugin depending on isArray, in which case you should check all works fine still fixes #37 git-svn-id: svn://dwoo.org/dwoo/trunk@284 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent 9b4b8364
[2009--] 1.2.0
! BC Break: Dwoo::isArray had to be fixed and it has been split up in 3
methods, isArray (for array access), isTraversable (for foreach) and
count (just a helper that counts anything). It won't affect you unless
you built some plugin depending on isArray, in which case you should
check all works fine still
[2009--] 1.1.1 [2009--] 1.1.1
+ Added Dwoo::setTemplate() for testing purposes mostly + Added Dwoo::setTemplate() for testing purposes mostly
* Fixed a security issue, if you didn't use a custom compiler factory but * Fixed a security issue, if you didn't use a custom compiler factory but
......
...@@ -354,7 +354,6 @@ class Dwoo ...@@ -354,7 +354,6 @@ class Dwoo
} }
} else { } else {
// no cache present // no cache present
if ($doCache === true) { if ($doCache === true) {
$dynamicId = uniqid(); $dynamicId = uniqid();
} }
...@@ -875,43 +874,77 @@ class Dwoo ...@@ -875,43 +874,77 @@ class Dwoo
} }
/** /**
* [util function] checks if the input is an array or an iterator object, optionally it can also check if it's empty * [util function] checks if the input is an array or arrayaccess object, optionally it can also check if it's empty
* *
* @param mixed $value the variable to check * @param mixed $value the variable to check
* @param bool $checkIsEmpty if true, the function will also check if the array is empty, * @param bool $checkIsEmpty if true, the function will also check if the array|arrayaccess is empty,
* and return true only if it's not empty * and return true only if it's not empty
* @return bool true if it's an array (and not empty) or false if it's not an array (or if it's empty) * @return int|bool true if it's an array|arrayaccess (or the item count if $checkIsEmpty is true) or false if it's not an array|arrayaccess (or 0 if $checkIsEmpty is true)
*/ */
public function isArray($value, $checkIsEmpty=false) public function isArray($value, $checkIsEmpty=false)
{ {
if (is_array($value) === true) { if (is_array($value) === true || $value instanceof ArrayAccess) {
if ($checkIsEmpty === false) { if ($checkIsEmpty === false) {
return true; return true;
} else { } else {
return count($value) > 0; return $this->count($value);
} }
} elseif ($value instanceof Iterator) { }
}
/**
* [util function] checks if the input is an array or a traversable object, optionally it can also check if it's empty
*
* @param mixed $value the variable to check
* @param bool $checkIsEmpty if true, the function will also check if the array|traversable is empty,
* and return true only if it's not empty
* @return int|bool true if it's an array|traversable (or the item count if $checkIsEmpty is true) or false if it's not an array|traversable (or 0 if $checkIsEmpty is true)
*/
public function isTraversable($value, $checkIsEmpty=false)
{
if (is_array($value) === true) {
if ($checkIsEmpty === false) { if ($checkIsEmpty === false) {
return true; return true;
} elseif ($value instanceof Countable) {
return count($value) > 0;
} else { } else {
$value->rewind(); return count($value) > 0;
return $value->valid();
} }
} elseif ($value instanceof ArrayAccess) { } elseif ($value instanceof Traversable) {
if ($checkIsEmpty === false) { if ($checkIsEmpty === false) {
return true; return true;
} elseif ($value instanceof Countable) {
return count($value) > 0;
} else { } else {
return $value->offsetExists(0); return $this->count($value);
} }
} }
return false; return false;
} }
/** /**
* [util function] counts an array or arrayaccess/traversable object
* @param mixed $value
* @return int|bool the count for arrays and objects that implement countable, true for other objects that don't, and 0 for empty elements
*/
public function count($value)
{
if (is_array($value) === true || $value instanceof Countable) {
return count($value);
} elseif ($value instanceof ArrayAccess) {
if ($value->offsetExists(0)) {
return true;
}
} elseif ($value instanceof Iterator) {
$value->rewind();
if ($value->valid()) {
return true;
}
} elseif ($value instanceof Traversable) {
foreach ($value as $dummy) {
return true;
}
}
return 0;
}
/**
* [util function] triggers a dwoo error * [util function] triggers a dwoo error
* *
* @param string $message the error message * @param string $message the error message
......
...@@ -71,7 +71,7 @@ class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Bloc ...@@ -71,7 +71,7 @@ class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Bloc
"\n".'$_for'.$cnt.'_step = abs('.$step.');'. "\n".'$_for'.$cnt.'_step = abs('.$step.');'.
"\n".'if (is_numeric($_for'.$cnt.'_from) && !is_numeric($_for'.$cnt.'_to)) { $this->triggerError(\'For requires the <em>to</em> parameter when using a numerical <em>from</em>\'); }'. "\n".'if (is_numeric($_for'.$cnt.'_from) && !is_numeric($_for'.$cnt.'_to)) { $this->triggerError(\'For requires the <em>to</em> parameter when using a numerical <em>from</em>\'); }'.
"\n".'$tmp_shows = $this->isArray($_for'.$cnt.'_from, true) || (is_numeric($_for'.$cnt.'_from) && (abs(($_for'.$cnt.'_from - $_for'.$cnt.'_to)/$_for'.$cnt.'_step) !== 0 || $_for'.$cnt.'_from == $_for'.$cnt.'_to));'; "\n".'$tmp_shows = $this->isArray($_for'.$cnt.'_from, true) || (is_numeric($_for'.$cnt.'_from) && (abs(($_for'.$cnt.'_from - $_for'.$cnt.'_to)/$_for'.$cnt.'_step) !== 0 || $_for'.$cnt.'_from == $_for'.$cnt.'_to));';
// adds foreach properties // adds for properties
if ($usesAny) { if ($usesAny) {
$out .= "\n".'$this->globals["for"]['.$name.'] = array'."\n("; $out .= "\n".'$this->globals["for"]['.$name.'] = array'."\n(";
if ($usesIndex) $out .="\n\t".'"index" => 0,'; if ($usesIndex) $out .="\n\t".'"index" => 0,';
...@@ -79,14 +79,14 @@ class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Bloc ...@@ -79,14 +79,14 @@ class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Bloc
if ($usesFirst) $out .="\n\t".'"first" => null,'; if ($usesFirst) $out .="\n\t".'"first" => null,';
if ($usesLast) $out .="\n\t".'"last" => null,'; if ($usesLast) $out .="\n\t".'"last" => null,';
if ($usesShow) $out .="\n\t".'"show" => $tmp_shows,'; if ($usesShow) $out .="\n\t".'"show" => $tmp_shows,';
if ($usesTotal) $out .="\n\t".'"total" => $this->isArray($_for'.$cnt.'_from) ? floor(count($_for'.$cnt.'_from) / $_for'.$cnt.'_step) : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),'; if ($usesTotal) $out .="\n\t".'"total" => $this->isArray($_for'.$cnt.'_from) ? floor($this->count($_for'.$cnt.'_from) / $_for'.$cnt.'_step) : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),';
$out.="\n);\n".'$_for'.$cnt.'_glob =& $this->globals["for"]['.$name.'];'; $out.="\n);\n".'$_for'.$cnt.'_glob =& $this->globals["for"]['.$name.'];';
} }
// checks if for must be looped // checks if for must be looped
$out .= "\n".'if ($tmp_shows)'."\n{"; $out .= "\n".'if ($tmp_shows)'."\n{";
// set from/to to correct values if an array was given // set from/to to correct values if an array was given
$out .= "\n\t".'if ($this->isArray($_for'.$cnt.'_from, true)) { $out .= "\n\t".'if ($this->isArray($_for'.$cnt.'_from'.(isset($params['hasElse']) ? ', true' : '').') == true) {
$_for'.$cnt.'_to = is_numeric($_for'.$cnt.'_to) ? $_for'.$cnt.'_to - $_for'.$cnt.'_step : count($_for'.$cnt.'_from) - 1; $_for'.$cnt.'_to = is_numeric($_for'.$cnt.'_to) ? $_for'.$cnt.'_to - $_for'.$cnt.'_step : $this->count($_for'.$cnt.'_from) - 1;
$_for'.$cnt.'_from = 0; $_for'.$cnt.'_from = 0;
}'; }';
...@@ -114,8 +114,7 @@ class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Bloc ...@@ -114,8 +114,7 @@ class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Bloc
}'; }';
} }
$out .= ' $out .= "\n\t".'for ($this->scope['.$name.'] = $_for'.$cnt.'_from; $this->scope['.$name.'] '.$condition.' $_for'.$cnt.'_to; $this->scope['.$name.'] '.$incrementer.'= $_for'.$cnt.'_step)'."\n\t{";
for ($this->scope['.$name.'] = $_for'.$cnt.'_from; $this->scope['.$name.'] '.$condition.' $_for'.$cnt.'_to; $this->scope['.$name.'] '.$incrementer.'= $_for'.$cnt.'_step)'."\n\t{";
// updates properties // updates properties
if ($usesIndex) { if ($usesIndex) {
$out .="\n\t\t".'$_for'.$cnt.'_glob["index"] = $this->scope['.$name.'];'; $out .="\n\t\t".'$_for'.$cnt.'_glob["index"] = $this->scope['.$name.'];';
......
...@@ -110,11 +110,11 @@ class Dwoo_Plugin_foreach extends Dwoo_Block_Plugin implements Dwoo_ICompilable_ ...@@ -110,11 +110,11 @@ class Dwoo_Plugin_foreach extends Dwoo_Block_Plugin implements Dwoo_ICompilable_
if ($usesFirst) $pre .="\n\t".'"first" => null,'; if ($usesFirst) $pre .="\n\t".'"first" => null,';
if ($usesLast) $pre .="\n\t".'"last" => null,'; if ($usesLast) $pre .="\n\t".'"last" => null,';
if ($usesShow) $pre .="\n\t".'"show" => $this->isArray($_fh'.$cnt.'_data, true),'; if ($usesShow) $pre .="\n\t".'"show" => $this->isArray($_fh'.$cnt.'_data, true),';
if ($usesTotal) $pre .="\n\t".'"total" => $this->isArray($_fh'.$cnt.'_data) ? count($_fh'.$cnt.'_data) : 0,'; if ($usesTotal) $pre .="\n\t".'"total" => $this->count($_fh'.$cnt.'_data),';
$pre.="\n);\n".'$_fh'.$cnt.'_glob =& $this->globals["foreach"]['.$name.'];'; $pre.="\n);\n".'$_fh'.$cnt.'_glob =& $this->globals["foreach"]['.$name.'];';
} }
// checks if foreach must be looped // checks if foreach must be looped
$pre .= "\n".'if ($this->isArray($_fh'.$cnt.'_data'.(isset($params['hasElse']) ? ', true' : '').') === true)'."\n{"; $pre .= "\n".'if ($this->isTraversable($_fh'.$cnt.'_data'.(isset($params['hasElse']) ? ', true' : '').') == true)'."\n{";
// iterates over keys // iterates over keys
$pre .= "\n\t".'foreach ($_fh'.$cnt.'_data as '.(isset($key)?'$this->scope['.$key.']=>':'').'$this->scope['.$val.'])'."\n\t{"; $pre .= "\n\t".'foreach ($_fh'.$cnt.'_data as '.(isset($key)?'$this->scope['.$key.']=>':'').'$this->scope['.$val.'])'."\n\t{";
// updates properties // updates properties
......
...@@ -91,12 +91,12 @@ class Dwoo_Plugin_loop extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Blo ...@@ -91,12 +91,12 @@ class Dwoo_Plugin_loop extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Blo
if ($usesIteration) $pre .="\n\t".'"iteration" => 1,'; if ($usesIteration) $pre .="\n\t".'"iteration" => 1,';
if ($usesFirst) $pre .="\n\t".'"first" => null,'; if ($usesFirst) $pre .="\n\t".'"first" => null,';
if ($usesLast) $pre .="\n\t".'"last" => null,'; if ($usesLast) $pre .="\n\t".'"last" => null,';
if ($usesShow) $pre .="\n\t".'"show" => $this->isArray($_loop'.$cnt.'_data, true),'; if ($usesShow) $pre .="\n\t".'"show" => $this->isTraversable($_loop'.$cnt.'_data, true),';
if ($usesTotal) $pre .="\n\t".'"total" => $this->isArray($_loop'.$cnt.'_data) ? count($_loop'.$cnt.'_data) : 0,'; if ($usesTotal) $pre .="\n\t".'"total" => $this->count($_loop'.$cnt.'_data),';
$pre.="\n);\n".'$_loop'.$cnt.'_glob =& $this->globals["loop"]['.$name.'];'; $pre.="\n);\n".'$_loop'.$cnt.'_glob =& $this->globals["loop"]['.$name.'];';
} }
// checks if the loop must be looped // checks if the loop must be looped
$pre .= "\n".'if ($this->isArray($_loop'.$cnt.'_data'.(isset($params['hasElse']) ? ', true' : '').') === true)'."\n{"; $pre .= "\n".'if ($this->isTraversable($_loop'.$cnt.'_data'.(isset($params['hasElse']) ? ', true' : '').') == true)'."\n{";
// iterates over keys // iterates over keys
$pre .= "\n\t".'foreach ($_loop'.$cnt.'_data as $tmp_key => $this->scope["-loop-"])'."\n\t{"; $pre .= "\n\t".'foreach ($_loop'.$cnt.'_data as $tmp_key => $this->scope["-loop-"])'."\n\t{";
// updates properties // updates properties
......
...@@ -92,7 +92,7 @@ class Dwoo_Plugin_section extends Dwoo_Block_Plugin implements Dwoo_ICompilable_ ...@@ -92,7 +92,7 @@ class Dwoo_Plugin_section extends Dwoo_Block_Plugin implements Dwoo_ICompilable_
if ($usesFirst) $output .="\n\t".'"first" => null,'; if ($usesFirst) $output .="\n\t".'"first" => null,';
if ($usesLast) $output .="\n\t".'"last" => null,'; if ($usesLast) $output .="\n\t".'"last" => null,';
if ($usesShow) $output .="\n\t".'"show" => ($this->isArray($_for'.$cnt.'_from, true)) || (is_numeric($_for'.$cnt.'_from) && $_for'.$cnt.'_from != $_for'.$cnt.'_to),'; if ($usesShow) $output .="\n\t".'"show" => ($this->isArray($_for'.$cnt.'_from, true)) || (is_numeric($_for'.$cnt.'_from) && $_for'.$cnt.'_from != $_for'.$cnt.'_to),';
if ($usesTotal) $output .="\n\t".'"total" => $this->isArray($_for'.$cnt.'_from) ? count($_for'.$cnt.'_from) - $_for'.$cnt.'_skip : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),'; if ($usesTotal) $output .="\n\t".'"total" => $this->isArray($_for'.$cnt.'_from) ? $this->count($_for'.$cnt.'_from) - $_for'.$cnt.'_skip : (is_numeric($_for'.$cnt.'_from) ? abs(($_for'.$cnt.'_to + 1 - $_for'.$cnt.'_from)/$_for'.$cnt.'_step) : 0),';
$out.="\n);\n".'$_section'.$cnt.'[\'glob\'] =& $this->globals["section"]['.$name.'];'."\n\n"; $out.="\n);\n".'$_section'.$cnt.'[\'glob\'] =& $this->globals["section"]['.$name.'];'."\n\n";
} }
*/ */
......
...@@ -39,9 +39,6 @@ function Dwoo_Plugin_include(Dwoo $dwoo, $file, $cache_time = null, $cache_id = ...@@ -39,9 +39,6 @@ function Dwoo_Plugin_include(Dwoo $dwoo, $file, $cache_time = null, $cache_id =
} }
try { try {
if (!is_numeric($cache_time)) {
$cache_time = null;
}
$include = $dwoo->templateFactory($resource, $identifier, $cache_time, $cache_id, $compile_id); $include = $dwoo->templateFactory($resource, $identifier, $cache_time, $cache_id, $compile_id);
} catch (Dwoo_Security_Exception $e) { } catch (Dwoo_Security_Exception $e) {
return $dwoo->triggerError('Include : Security restriction : '.$e->getMessage(), E_USER_WARNING); return $dwoo->triggerError('Include : Security restriction : '.$e->getMessage(), E_USER_WARNING);
...@@ -55,12 +52,10 @@ function Dwoo_Plugin_include(Dwoo $dwoo, $file, $cache_time = null, $cache_id = ...@@ -55,12 +52,10 @@ function Dwoo_Plugin_include(Dwoo $dwoo, $file, $cache_time = null, $cache_id =
return $dwoo->triggerError('Include : Resource "'.$resource.'" does not support includes.', E_USER_WARNING); return $dwoo->triggerError('Include : Resource "'.$resource.'" does not support includes.', E_USER_WARNING);
} }
if ($dwoo->isArray($data)) { if (is_string($data)) {
$vars = $data;
} elseif ($dwoo->isArray($cache_time)) {
$vars = $cache_time;
} else {
$vars = $dwoo->readVar($data); $vars = $dwoo->readVar($data);
} else {
$vars = $data;
} }
if (count($rest)) { if (count($rest)) {
......
...@@ -304,54 +304,54 @@ class CoreTests extends PHPUnit_Framework_TestCase ...@@ -304,54 +304,54 @@ class CoreTests extends PHPUnit_Framework_TestCase
$dwoo = new Dwoo(); $dwoo = new Dwoo();
$data = array(); $data = array();
$this->assertEquals(true, $dwoo->isArray($data)); $this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(false, $dwoo->isArray($data, true)); $this->assertEquals(0, $dwoo->isArray($data, true));
$data = array('moo'); $data = array('moo');
$this->assertEquals(true, $dwoo->isArray($data)); $this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(true, $dwoo->isArray($data, true)); $this->assertEquals(1, $dwoo->isArray($data, true));
} }
public function testIsArrayIterator() public function testIsArrayArrayAccess()
{ {
$dwoo = new Dwoo(); $dwoo = new Dwoo();
$data = new TestIterator(array()); $data = new TestArrayAccess(array());
$this->assertEquals(true, $dwoo->isArray($data)); $this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(false, $dwoo->isArray($data, true)); $this->assertEquals(0, $dwoo->isArray($data, true));
$data = new TestIterator(array('moo')); $data = new TestArrayAccess(array('moo'));
$this->assertEquals(true, $dwoo->isArray($data)); $this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(true, $dwoo->isArray($data, true)); $this->assertEquals(true, $dwoo->isArray($data, true));
} }
public function testIsArrayCountableIterator() public function testIsArrayCountableArrayAccess()
{ {
$dwoo = new Dwoo(); $dwoo = new Dwoo();
$data = new TestCountableIterator(array()); $data = new TestCountableArrayAccess(array());
$this->assertEquals(true, $dwoo->isArray($data)); $this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(false, $dwoo->isArray($data, true)); $this->assertEquals(0, $dwoo->isArray($data, true));
$data = new TestCountableIterator(array('moo')); $data = new TestCountableArrayAccess(array('moo'));
$this->assertEquals(true, $dwoo->isArray($data)); $this->assertEquals(true, $dwoo->isArray($data));
$this->assertEquals(true, $dwoo->isArray($data, true)); $this->assertEquals(1, $dwoo->isArray($data, true));
} }
public function testIsArrayArrayAccess() public function testIsTraversableIterator()
{ {
$dwoo = new Dwoo(); $dwoo = new Dwoo();
$data = new TestArrayAccess(array()); $data = new TestIterator(array());
$this->assertEquals(true, $dwoo->isArray($data)); $this->assertEquals(true, $dwoo->isTraversable($data));
$this->assertEquals(false, $dwoo->isArray($data, true)); $this->assertEquals(0, $dwoo->isTraversable($data, true));
$data = new TestArrayAccess(array('moo')); $data = new TestIterator(array('moo'));
$this->assertEquals(true, $dwoo->isArray($data)); $this->assertEquals(true, $dwoo->isTraversable($data));
$this->assertEquals(true, $dwoo->isArray($data, true)); $this->assertEquals(true, $dwoo->isTraversable($data, true));
} }
public function testIsArrayCountableArrayAccess() public function testIsTraversableCountableIterator()
{ {
$dwoo = new Dwoo(); $dwoo = new Dwoo();
$data = new TestCountableArrayAccess(array()); $data = new TestCountableIterator(array());
$this->assertEquals(true, $dwoo->isArray($data)); $this->assertEquals(true, $dwoo->isTraversable($data));
$this->assertEquals(false, $dwoo->isArray($data, true)); $this->assertEquals(0, $dwoo->isTraversable($data, true));
$data = new TestCountableArrayAccess(array('moo')); $data = new TestCountableIterator(array('moo'));
$this->assertEquals(true, $dwoo->isArray($data)); $this->assertEquals(true, $dwoo->isTraversable($data));
$this->assertEquals(true, $dwoo->isArray($data, true)); $this->assertEquals(1, $dwoo->isTraversable($data, true));
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment