Upgrade helper functions loading

This commit is contained in:
Maarten 2020-02-13 18:34:04 +01:00
parent 9cea7b71fd
commit 8f3fff2c12

View file

@ -2,56 +2,36 @@
namespace Runtime\Factory; namespace Runtime\Factory;
use ReflectionFunction;
use ReflectionException;
use Runtime\Exceptions\ExceptionHandler;
class AppFactory { class AppFactory {
protected function getHelperFunctions() { /**
$file = getcwd() . '/../src/Runtime/Helpers/helpers.php'; * @return array
*/
protected function getHelperFunctions()
{
$result = [];
$source = file_get_contents($file); $functions = get_defined_functions()['user'];
$tokens = token_get_all($source);
$functions = array(); try {
$nextStringIsFunc = false; foreach ($functions as $function) {
$inClass = false; $refl = new ReflectionFunction($function);
$bracesCount = 0;
foreach($tokens as $token) { if(endsWith($refl->getFileName(), 'helpers.php')) {
switch($token[0]) { $result[] = $function;
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;
} }
}
catch (ReflectionException $e) {
ExceptionHandler::make($e);
} }
return $functions; return $result;
} }
} }