From 0e26e815077290a416d9ab42d192ce8c37389c4a Mon Sep 17 00:00:00 2001 From: rawsrc Date: Mon, 6 Apr 2020 10:46:42 +0200 Subject: [PATCH] Performance optimisation --- PhpEcho.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/PhpEcho.php b/PhpEcho.php index 3540667..c7c4563 100644 --- a/PhpEcho.php +++ b/PhpEcho.php @@ -210,7 +210,7 @@ public function setCode(string $code) public function __invoke(string $helper, ...$args) { if ($helper !== '') { - if ( ! self::isHelper($helper)) { + if ( ! empty(self::$helpers_file_to_inject)) { self::injectHelpers(); } if (self::isHelper($helper)) { @@ -260,6 +260,10 @@ public function __toString() * @var array [helper's name => [type]] */ private static $helpers_types = []; + /** + * @var array [helpers filepath to inject] + */ + private static $helpers_file_to_inject = []; /** * @param string $name @@ -306,7 +310,8 @@ public static function addPathToHelperFile(string ...$path) { foreach ($path as $p) { if ( ! isset(self::$helpers_file_path[$p])) { - self::$helpers_file_path[$p] = true; + self::$helpers_file_path[$p] = true; + self::$helpers_file_to_inject[] = $p; } } } @@ -316,12 +321,12 @@ public static function addPathToHelperFile(string ...$path) */ public static function injectHelpers() { - foreach (self::$helpers_file_path as $path => &$to_inject) { - if ($to_inject && is_file($path)) { + foreach (self::$helpers_file_to_inject as $path) { + if (is_file($path)) { self::addHelpers(include $path); - $to_inject = false; } } + self::$helpers_file_to_inject = []; } /**