From 21aa211ddbc0be879d0ce7a7090114b2792d9c72 Mon Sep 17 00:00:00 2001 From: uwetews Date: Fri, 9 Sep 2016 18:43:37 +0200 Subject: [PATCH] - bugfix/optimization {foreach} did not execute the {foreachelse} when iterating empty objects https://github.com/smarty-php/smarty/pull/287 --- change_log.txt | 3 ++ libs/Smarty.class.php | 2 +- .../smarty_internal_runtime_foreach.php | 40 +++++++++---------- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/change_log.txt b/change_log.txt index 018d008e..c68d2dfe 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,7 @@ ===== 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 - bugfix implement wrapper for removed method getConfigVariable() https://github.com/smarty-php/smarty/issues/286 diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 016e058c..6e159b79 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -114,7 +114,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.31-dev/13'; + const SMARTY_VERSION = '3.1.31-dev/14'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_runtime_foreach.php b/libs/sysplugins/smarty_internal_runtime_foreach.php index c93c6276..ed3b5f54 100644 --- a/libs/sysplugins/smarty_internal_runtime_foreach.php +++ b/libs/sysplugins/smarty_internal_runtime_foreach.php @@ -38,20 +38,23 @@ class Smarty_Internal_Runtime_Foreach $properties = array()) { $saveVars = array(); - if (!is_array($from) && !is_object($from)) { - settype($from, 'array'); + $total = null; + if (!is_array($from)) { + if (is_object($from)) { + $total = $this->count($from); + } else { + settype($from, 'array'); + } + } + if (!isset($total)) { + $total = empty($from) ? 0 : (($needTotal || isset($properties[ 'total' ])) ? count($from) : 1); } - $total = ($needTotal || isset($properties[ 'total' ])) ? $this->count($from) : 1; if (isset($tpl->tpl_vars[ $item ])) { $saveVars[ $item ] = $tpl->tpl_vars[ $item ]; } $tpl->tpl_vars[ $item ] = new Smarty_Variable(null, $tpl->isRenderingCache); - if (empty($from)) { + if ($total === 0) { $from = null; - $total = 0; - if ($needTotal) { - $tpl->tpl_vars[ $item ]->total = 0; - } } else { if ($key) { if (isset($tpl->tpl_vars[ $key ])) { @@ -59,9 +62,9 @@ class Smarty_Internal_Runtime_Foreach } $tpl->tpl_vars[ $key ] = new Smarty_Variable(null, $tpl->isRenderingCache); } - if ($needTotal) { - $tpl->tpl_vars[ $item ]->total = $total; - } + } + if ($needTotal) { + $tpl->tpl_vars[ $item ]->total = $total; } if ($name) { $namedVar = "__smarty_foreach_{$name}"; @@ -110,28 +113,21 @@ class Smarty_Internal_Runtime_Foreach */ public function count($value) { - if (is_array($value) === true || $value instanceof Countable) { + if ($value instanceof Countable) { return count($value); } elseif ($value instanceof IteratorAggregate) { // Note: getIterator() returns a Traversable, not an Iterator // thus rewind() and valid() methods may not be present return iterator_count($value->getIterator()); } elseif ($value instanceof Iterator) { - if ($value instanceof Generator) { - return 1; - } - return iterator_count($value); + return $value instanceof Generator ? 1 : iterator_count($value); } elseif ($value instanceof PDOStatement) { return $value->rowCount(); } elseif ($value instanceof Traversable) { return iterator_count($value); } elseif ($value instanceof ArrayAccess) { - if ($value->offsetExists(0)) { - return 1; - } - } elseif (is_object($value)) { - return count((array)$value); + return $value->offsetExists(0) ? 1 : 0; } - return 0; + return count((array) $value); } }