2016-05-19 18:50:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Core\Manifest;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\NodeTraverser;
|
|
|
|
use PhpParser\NodeVisitorAbstract;
|
|
|
|
|
|
|
|
class ClassManifestVisitor extends NodeVisitorAbstract
|
|
|
|
{
|
|
|
|
|
|
|
|
private $classes = [];
|
|
|
|
|
|
|
|
private $traits = [];
|
|
|
|
|
|
|
|
private $interfaces = [];
|
|
|
|
|
|
|
|
public function resetState()
|
|
|
|
{
|
|
|
|
$this->classes = [];
|
|
|
|
$this->traits = [];
|
|
|
|
$this->interfaces = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function beforeTraverse(array $nodes)
|
|
|
|
{
|
|
|
|
$this->resetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function enterNode(Node $node)
|
|
|
|
{
|
|
|
|
if ($node instanceof Node\Stmt\Class_) {
|
2017-06-22 22:50:45 +12:00
|
|
|
$extends = [];
|
2016-05-19 18:50:51 +01:00
|
|
|
$interfaces = [];
|
|
|
|
|
|
|
|
if ($node->extends) {
|
2020-04-20 18:58:09 +01:00
|
|
|
$extends = [(string)$node->extends];
|
2016-05-19 18:50:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($node->implements) {
|
|
|
|
foreach ($node->implements as $interface) {
|
|
|
|
$interfaces[] = (string)$interface;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->classes[(string)$node->namespacedName] = [
|
|
|
|
'extends' => $extends,
|
|
|
|
'interfaces' => $interfaces,
|
|
|
|
];
|
|
|
|
} elseif ($node instanceof Node\Stmt\Trait_) {
|
2020-04-20 18:58:09 +01:00
|
|
|
$this->traits[(string)$node->namespacedName] = [];
|
2016-05-19 18:50:51 +01:00
|
|
|
} elseif ($node instanceof Node\Stmt\Interface_) {
|
2020-04-20 18:58:09 +01:00
|
|
|
$extends = [];
|
2016-05-19 18:50:51 +01:00
|
|
|
foreach ($node->extends as $ancestor) {
|
|
|
|
$extends[] = (string)$ancestor;
|
|
|
|
}
|
|
|
|
$this->interfaces[(string)$node->namespacedName] = [
|
|
|
|
'extends' => $extends,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
if (!$node instanceof Node\Stmt\Namespace_) {
|
|
|
|
//break out of traversal as we only need highlevel information here!
|
|
|
|
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClasses()
|
|
|
|
{
|
|
|
|
return $this->classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTraits()
|
|
|
|
{
|
|
|
|
return $this->traits;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInterfaces()
|
|
|
|
{
|
|
|
|
return $this->interfaces;
|
|
|
|
}
|
|
|
|
}
|