- bugfix/optimization {foreach} did not execute the {foreachelse} when iterating empty objects https://github.com/smarty-php/smarty/pull/287

This commit is contained in:
uwetews
2016-09-09 18:43:37 +02:00
parent 124b333da2
commit 21aa211ddb
3 changed files with 22 additions and 23 deletions

View File

@@ -1,4 +1,7 @@
===== 3.1.31-dev ===== (xx.xx.xx) ===== 3.1.31-dev ===== (xx.xx.xx)
09.09.2016
- bugfix/optimization {foreach} did not execute the {foreachelse} when iterating empty objects https://github.com/smarty-php/smarty/pull/287
08.09.2016 08.09.2016
- bugfix implement wrapper for removed method getConfigVariable() https://github.com/smarty-php/smarty/issues/286 - bugfix implement wrapper for removed method getConfigVariable() https://github.com/smarty-php/smarty/issues/286

View File

@@ -114,7 +114,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/** /**
* smarty version * smarty version
*/ */
const SMARTY_VERSION = '3.1.31-dev/13'; const SMARTY_VERSION = '3.1.31-dev/14';
/** /**
* define variable scopes * define variable scopes

View File

@@ -38,20 +38,23 @@ class Smarty_Internal_Runtime_Foreach
$properties = array()) $properties = array())
{ {
$saveVars = array(); $saveVars = array();
if (!is_array($from) && !is_object($from)) { $total = null;
if (!is_array($from)) {
if (is_object($from)) {
$total = $this->count($from);
} else {
settype($from, 'array'); settype($from, 'array');
} }
$total = ($needTotal || isset($properties[ 'total' ])) ? $this->count($from) : 1; }
if (!isset($total)) {
$total = empty($from) ? 0 : (($needTotal || isset($properties[ 'total' ])) ? count($from) : 1);
}
if (isset($tpl->tpl_vars[ $item ])) { if (isset($tpl->tpl_vars[ $item ])) {
$saveVars[ $item ] = $tpl->tpl_vars[ $item ]; $saveVars[ $item ] = $tpl->tpl_vars[ $item ];
} }
$tpl->tpl_vars[ $item ] = new Smarty_Variable(null, $tpl->isRenderingCache); $tpl->tpl_vars[ $item ] = new Smarty_Variable(null, $tpl->isRenderingCache);
if (empty($from)) { if ($total === 0) {
$from = null; $from = null;
$total = 0;
if ($needTotal) {
$tpl->tpl_vars[ $item ]->total = 0;
}
} else { } else {
if ($key) { if ($key) {
if (isset($tpl->tpl_vars[ $key ])) { if (isset($tpl->tpl_vars[ $key ])) {
@@ -59,10 +62,10 @@ class Smarty_Internal_Runtime_Foreach
} }
$tpl->tpl_vars[ $key ] = new Smarty_Variable(null, $tpl->isRenderingCache); $tpl->tpl_vars[ $key ] = new Smarty_Variable(null, $tpl->isRenderingCache);
} }
}
if ($needTotal) { if ($needTotal) {
$tpl->tpl_vars[ $item ]->total = $total; $tpl->tpl_vars[ $item ]->total = $total;
} }
}
if ($name) { if ($name) {
$namedVar = "__smarty_foreach_{$name}"; $namedVar = "__smarty_foreach_{$name}";
if (isset($tpl->tpl_vars[ $namedVar ])) { if (isset($tpl->tpl_vars[ $namedVar ])) {
@@ -110,28 +113,21 @@ class Smarty_Internal_Runtime_Foreach
*/ */
public function count($value) public function count($value)
{ {
if (is_array($value) === true || $value instanceof Countable) { if ($value instanceof Countable) {
return count($value); return count($value);
} elseif ($value instanceof IteratorAggregate) { } elseif ($value instanceof IteratorAggregate) {
// Note: getIterator() returns a Traversable, not an Iterator // Note: getIterator() returns a Traversable, not an Iterator
// thus rewind() and valid() methods may not be present // thus rewind() and valid() methods may not be present
return iterator_count($value->getIterator()); return iterator_count($value->getIterator());
} elseif ($value instanceof Iterator) { } elseif ($value instanceof Iterator) {
if ($value instanceof Generator) { return $value instanceof Generator ? 1 : iterator_count($value);
return 1;
}
return iterator_count($value);
} elseif ($value instanceof PDOStatement) { } elseif ($value instanceof PDOStatement) {
return $value->rowCount(); return $value->rowCount();
} elseif ($value instanceof Traversable) { } elseif ($value instanceof Traversable) {
return iterator_count($value); return iterator_count($value);
} elseif ($value instanceof ArrayAccess) { } elseif ($value instanceof ArrayAccess) {
if ($value->offsetExists(0)) { return $value->offsetExists(0) ? 1 : 0;
return 1;
} }
} elseif (is_object($value)) {
return count((array) $value); return count((array) $value);
} }
return 0;
}
} }