From 745a19744bf6e00843b8f66f24b8b08d409c4149 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Wed, 13 Mar 2019 13:11:01 +0100 Subject: [PATCH] Polyfill for `is_countable()`, optimized `{foreach}`, short array notation --- wcfsetup/install.php | 4 +++ wcfsetup/install/files/lib/core.functions.php | 4 +++ .../TemplateScriptingCompiler.class.php | 33 ++++++++++--------- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/wcfsetup/install.php b/wcfsetup/install.php index afb7caf719..87e5f21177 100644 --- a/wcfsetup/install.php +++ b/wcfsetup/install.php @@ -528,6 +528,10 @@ function handleError($errorNo, $message, $filename, $lineNo) { } } +if (!function_exists('is_countable')) { + function is_countable($var) { return is_array($var) || $var instanceof Countable || $var instanceof ResourceBundle || $var instanceof SimpleXmlElement; } +} + /** @noinspection PhpMultipleClassesDeclarationsInOneFile */ /** * BasicFileUtil contains file-related functions. diff --git a/wcfsetup/install/files/lib/core.functions.php b/wcfsetup/install/files/lib/core.functions.php index 468a2d211b..970d31211f 100644 --- a/wcfsetup/install/files/lib/core.functions.php +++ b/wcfsetup/install/files/lib/core.functions.php @@ -88,6 +88,10 @@ namespace { if (@ini_get('zlib.output_compression')) { @ini_set('zlib.output_compression', '0'); } + + if (!function_exists('is_countable')) { + function is_countable($var) { return is_array($var) || $var instanceof Countable || $var instanceof ResourceBundle || $var instanceof SimpleXmlElement; } + } } // @codingStandardsIgnoreStart diff --git a/wcfsetup/install/files/lib/system/template/TemplateScriptingCompiler.class.php b/wcfsetup/install/files/lib/system/template/TemplateScriptingCompiler.class.php index dd588b43db..010ea46ebe 100644 --- a/wcfsetup/install/files/lib/system/template/TemplateScriptingCompiler.class.php +++ b/wcfsetup/install/files/lib/system/template/TemplateScriptingCompiler.class.php @@ -455,7 +455,7 @@ class TemplateScriptingCompiler { $tagArgs = static::makeArgString($this->parseTagArgs($tagArgs, $tagCommand)); - return "pluginObjects['".$className."']->execute(array(".$tagArgs."), \$this); ?>"; + return "pluginObjects['".$className."']->execute([".$tagArgs."], \$this);?>"; } /** @@ -489,7 +489,7 @@ class TemplateScriptingCompiler { $tagArgs = static::makeArgString($this->parseTagArgs($tagArgs, $tagCommand)); - $phpCode = "tagStack[] = array('".$tagCommand."', array(".$tagArgs."));\n"; + $phpCode = "tagStack[] = ['".$tagCommand."', [".$tagArgs."]];\n"; $phpCode .= "\$this->pluginObjects['".$className."']->init(\$this->tagStack[count(\$this->tagStack) - 1][1], \$this);\n"; $phpCode .= "while (\$this->pluginObjects['".$className."']->next(\$this)) { ob_start(); ?>"; } @@ -616,7 +616,7 @@ class TemplateScriptingCompiler { $phpCode = " 0) || ".$foreachHash.") {\n"; + } + else { + $phpCode .= $foreachHash."_cnt = (".$foreachHash." !== null ? 1 : 0);\n"; + $phpCode .= "if (is_countable(".$foreachHash.")) {\n"; + $phpCode .= $foreachHash."_cnt = count(".$foreachHash.");\n"; + $phpCode .= "}\n"; $phpCode .= $foreachProp."['total'] = ".$foreachHash."_cnt;\n"; $phpCode .= $foreachProp."['show'] = (".$foreachProp."['total'] > 0 ? true : false);\n"; $phpCode .= $foreachProp."['iteration'] = 0;\n"; + $phpCode .= "if (".$foreachHash."_cnt > 0) {\n"; } - $phpCode .= "if (".$foreachHash."_cnt > 0) {\n"; if (isset($args['key'])) { $phpCode .= "foreach (".$foreachHash." as ".(mb_substr($args['key'], 0, 1) != '$' ? "\$this->v[".$args['key']."]" : $args['key'])." => ".(mb_substr($args['item'], 0, 1) != '$' ? "\$this->v[".$args['item']."]" : $args['item']).") {\n"; @@ -847,7 +850,7 @@ class TemplateScriptingCompiler { if (strpos($application, '$') === false) { $application = "'" . $application . "'"; } - $phpCode .= '$this->includeTemplate('.$file.', '.$application.', array('.$argString.'), '.($sandbox ? 1 : 0).');'."\n"; + $phpCode .= '$this->includeTemplate('.$file.', '.$application.', ['.$argString.'], '.($sandbox ? 1 : 0).');'."\n"; if ($assignVar !== false) { $phpCode .= '$this->'.($append ? 'append' : 'assign').'('.$assignVar.', ob_get_clean());'."\n"; @@ -1088,7 +1091,7 @@ class TemplateScriptingCompiler { $parsedTag = 'wcf\util\StringUtil::formatNumeric('.$parsedTag.')'; } - return ''; + return ''; } /** @@ -1116,7 +1119,7 @@ class TemplateScriptingCompiler { */ protected function compileModifier($data) { if (isset($data['className'])) { - return "\$this->pluginObjects['".$data['className']."']->execute(array(".implode(',', $data['parameter'])."), \$this)"; + return "\$this->pluginObjects['".$data['className']."']->execute([".implode(',', $data['parameter'])."], \$this)"; } else { return $data['name'].'('.implode(',', $data['parameter']).')'; @@ -1641,9 +1644,9 @@ class TemplateScriptingCompiler { $string = str_replace('', '@@PHP_END_TAG@@', $string); - $string = str_replace('@@PHP_END_TAG@@', "'; ?>\n", $string); - $string = str_replace('@@PHP_SHORT_START_TAG@@', "\n", $string); - $string = str_replace('@@PHP_START_TAG@@', "\n", $string); + $string = str_replace('@@PHP_END_TAG@@', "';?>\n", $string); + $string = str_replace('@@PHP_SHORT_START_TAG@@', "\n", $string); + $string = str_replace('@@PHP_START_TAG@@', "\n", $string); } return $string; -- 2.20.1