-
-
Save chx/9f289ffd541701228ba98243dd7eaff7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$autoloader = require_once 'autoload.php'; | |
use PhpParser\{Node, ParserFactory}; | |
use Drupal\Core\Extension\Hook; | |
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); | |
$stmts = $parser->parse(file_get_contents("core/modules/node/src/Hooks/Whatever.php")); | |
$nameResolver = new PhpParser\NodeVisitor\NameResolver; | |
$nodeTraverser = new PhpParser\NodeTraverser; | |
$nodeTraverser->addVisitor($nameResolver); | |
$stmts = $nodeTraverser->traverse($stmts); | |
$nodeFinder = new PhpParser\NodeFinder(); | |
$classes = $nodeFinder->findInstanceOf($stmts, Node\Stmt\Class_::class); | |
foreach ($classes as $class) { | |
$class_is_hook = FALSE; | |
foreach ($class->attrGroups as $attrGroup) { | |
foreach ($attrGroup->attrs as $attribute) { | |
if ((string) $attribute->name === Hook::class) { | |
$arguments = []; | |
foreach ($attribute->args as $arg) { | |
$arguments[(string) $arg->name] = $arg->value->value; | |
} | |
if (isset($arguments['hook'])) { | |
if (!isset($arguments['method'])) { | |
throw new \LogicException(''); | |
} | |
$implementations[] = new Hook(... $arguments); | |
} | |
} | |
} | |
} | |
$methods = $nodeFinder->findInstanceOf($class->stmts, Node\Stmt\ClassMethod::class); | |
foreach ($methods as $method) { | |
foreach ($method->attrGroups as $attrGroup) { | |
foreach ($attrGroup->attrs as $attribute) { | |
if ((string) $attribute->name === Hook::class) { | |
$arguments = []; | |
foreach ($attribute->args as $arg) { | |
$arguments[(string) $arg->name] = $arg->value->value; | |
} | |
if (isset($arguments['hook'])) { | |
$implementations[] = new Hook(... $arguments)->setMethod((string) $method->name]); | |
} | |
} | |
} | |
} | |
} | |
} | |
var_dump($implementations); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment