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;
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;
}
}