Upgrade helper functions loading
This commit is contained in:
parent
9cea7b71fd
commit
8f3fff2c12
1 changed files with 22 additions and 42 deletions
|
@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $functions;
|
}
|
||||||
|
catch (ReflectionException $e) {
|
||||||
|
ExceptionHandler::make($e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue