-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
396a718
commit 8f110da
Showing
3 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Haemanthus\CodeIgniter3IdeHelper\Parsers; | ||
|
||
use Haemanthus\CodeIgniter3IdeHelper\Visitors\AssignArrayNodeVisitor; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\ParserFactory; | ||
|
||
class AutoloadFileParser extends AbstractFileParser | ||
{ | ||
protected AssignArrayNodeVisitor $assignArrayNodeVisitor; | ||
|
||
public function __construct( | ||
ParserFactory $parser, | ||
NodeTraverser $traverser | ||
) { | ||
parent::__construct($parser, $traverser); | ||
|
||
$this->assignArrayNodeVisitor = new AssignArrayNodeVisitor(); | ||
$this->traverser->addVisitor($this->assignArrayNodeVisitor); | ||
} | ||
|
||
public function parse(string $contents): array | ||
{ | ||
$this->traverser->traverse($this->parser->parse($contents)); | ||
|
||
dump($this->assignArrayNodeVisitor->getFoundAutoloadLibraryNodes()); | ||
dd($this->assignArrayNodeVisitor->getFoundAutoloadModelNodes()); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Haemanthus\CodeIgniter3IdeHelper\Visitors; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeTraverser; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
class AssignArrayNodeVisitor extends NodeVisitorAbstract | ||
{ | ||
protected array $libraries = []; | ||
|
||
protected array $models = []; | ||
|
||
protected function isAssignArrayAutoload(Node $node): bool | ||
{ | ||
return $node instanceof Node\Expr\Assign | ||
&& $node->var instanceof Node\Expr\ArrayDimFetch | ||
&& $node->var->var instanceof Node\Expr\Variable | ||
&& $node->var->var->name === 'autoload'; | ||
} | ||
|
||
protected function isAssignAutoloadLibrary(Node $node): bool | ||
{ | ||
return $node instanceof Node\Expr\Assign | ||
&& $node->var instanceof Node\Expr\ArrayDimFetch | ||
&& $node->var->dim instanceof Node\Scalar\String_ | ||
&& $node->var->dim->value === 'libraries'; | ||
} | ||
|
||
protected function isAssignAutoloadModel(Node $node): bool | ||
{ | ||
return $node instanceof Node\Expr\Assign | ||
&& $node->var instanceof Node\Expr\ArrayDimFetch | ||
&& $node->var->dim instanceof Node\Scalar\String_ | ||
&& $node->var->dim->value === 'models'; | ||
} | ||
|
||
public function enterNode(Node $node) | ||
{ | ||
switch (true) { | ||
case $this->isAssignArrayAutoload($node) === false: | ||
return parent::enterNode($node); | ||
|
||
case $this->isAssignAutoloadLibrary($node): | ||
$this->libraries[] = $node; | ||
|
||
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; | ||
|
||
case $this->isAssignAutoloadModel($node): | ||
$this->models[] = $node; | ||
|
||
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; | ||
|
||
default: | ||
return parent::enterNode($node); | ||
} | ||
} | ||
|
||
public function getFoundAutoloadLibraryNodes() | ||
{ | ||
return $this->libraries; | ||
} | ||
|
||
public function getFoundAutoloadModelNodes() | ||
{ | ||
return $this->models; | ||
} | ||
} |