From 8f3fff2c122c1fe836e9fec227421d929cbebfb8 Mon Sep 17 00:00:00 2001 From: Maarten Date: Thu, 13 Feb 2020 18:34:04 +0100 Subject: [PATCH] Upgrade helper functions loading --- src/Runtime/Factory/AppFactory.php | 64 ++++++++++-------------------- 1 file changed, 22 insertions(+), 42 deletions(-) diff --git a/src/Runtime/Factory/AppFactory.php b/src/Runtime/Factory/AppFactory.php index b39e388..0fca3b5 100644 --- a/src/Runtime/Factory/AppFactory.php +++ b/src/Runtime/Factory/AppFactory.php @@ -2,56 +2,36 @@ namespace Runtime\Factory; +use ReflectionFunction; +use ReflectionException; +use Runtime\Exceptions\ExceptionHandler; + class AppFactory { - protected function getHelperFunctions() { - $file = getcwd() . '/../src/Runtime/Helpers/helpers.php'; + /** + * @return array + */ + protected function getHelperFunctions() + { + $result = []; - $source = file_get_contents($file); - $tokens = token_get_all($source); + $functions = get_defined_functions()['user']; - $functions = array(); - $nextStringIsFunc = false; - $inClass = false; - $bracesCount = 0; + try { + foreach ($functions as $function) { + $refl = new ReflectionFunction($function); - foreach($tokens as $token) { - switch($token[0]) { - case T_CLASS: - $inClass = true; - break; - case T_FUNCTION: - if(!$inClass) $nextStringIsFunc = true; - break; - - case T_STRING: - if($nextStringIsFunc) { - $nextStringIsFunc = false; - $functions[] = $token[1]; - } - break; - - // Anonymous functions - case '(': - case ';': - $nextStringIsFunc = false; - break; - - // Exclude Classes - case '{': - if($inClass) $bracesCount++; - break; - - case '}': - if($inClass) { - $bracesCount--; - if($bracesCount === 0) $inClass = false; - } - break; + if(endsWith($refl->getFileName(), 'helpers.php')) { + $result[] = $function; + } } + + } + catch (ReflectionException $e) { + ExceptionHandler::make($e); } - return $functions; + return $result; } } \ No newline at end of file