forked from maglnet/ComposerRequireChecker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
processing includes as proposed in maglnet#67
- Loading branch information
Showing
6 changed files
with
189 additions
and
7 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,45 @@ | ||
<?php | ||
|
||
namespace ComposerRequireChecker\ASTLocator; | ||
|
||
use Traversable; | ||
|
||
class FileAST | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $file; | ||
|
||
/** | ||
* @var Traversable | ||
*/ | ||
private $ast; | ||
|
||
/** | ||
* @param string $file | ||
* @param Traversable|array|null $ast | ||
* | ||
*/ | ||
public function __construct(string $file, $ast) | ||
{ | ||
$this->file = $file; | ||
$this->ast = $ast; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getFile() | ||
{ | ||
return $this->file; | ||
} | ||
|
||
/** | ||
* @return Traversable|array | ||
*/ | ||
public function getAst() | ||
{ | ||
return $this->ast; | ||
} | ||
} |
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
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
129 changes: 129 additions & 0 deletions
129
src/ComposerRequireChecker/NodeVisitor/IncludeCollector.php
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,129 @@ | ||
<?php | ||
|
||
namespace ComposerRequireChecker\NodeVisitor; | ||
|
||
use FilesystemIterator; | ||
use InvalidArgumentException; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\BinaryOp\Concat; | ||
use PhpParser\Node\Expr\ConstFetch; | ||
use PhpParser\Node\Expr\Include_; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Scalar\MagicConst\Dir; | ||
use PhpParser\Node\Scalar\MagicConst\File; | ||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\NodeVisitorAbstract; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
|
||
final class IncludeCollector extends NodeVisitorAbstract | ||
{ | ||
/** | ||
* @var Expr[] | ||
*/ | ||
private $included = []; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function beforeTraverse(array $nodes) | ||
{ | ||
$this->included = []; | ||
return parent::beforeTraverse($nodes); | ||
} | ||
|
||
/** | ||
* @param string $file | ||
* @return string[] | ||
*/ | ||
public function getIncluded(string $file): array | ||
{ | ||
$included = []; | ||
foreach ($this->included as $exp) { | ||
try { | ||
$this->computePath($included, $this->processIncludePath($exp, $file), $file); | ||
} catch(InvalidArgumentException $x) { | ||
var_dump($x->getMessage()); | ||
} | ||
} | ||
return $included; | ||
} | ||
|
||
/** | ||
* @param array $included | ||
* @param string $path | ||
* @param string $self | ||
* @return void | ||
*/ | ||
private function computePath(array &$included, string $path, string $self) | ||
{ | ||
if (!preg_match('#^([A-Z]:)?/#i', str_replace('\\', '/', $path))) { | ||
$path = dirname($self).'/'.$path; | ||
} | ||
if (false === strpos($path, '{var}')) { | ||
$included[] = $path; | ||
return; | ||
} | ||
$parts = explode('{var}', $path); | ||
$regex = []; | ||
foreach($parts as $part) { | ||
$regex[] = preg_quote(str_replace('\\', '/', $part), '/'); | ||
} | ||
$regex = '/^'.implode('.+', $regex).'$/'; | ||
$self = str_replace('\\', '/', $self); | ||
foreach (new RecursiveIteratorIterator( | ||
new RecursiveDirectoryIterator( | ||
$parts[0], | ||
FilesystemIterator::CURRENT_AS_PATHNAME | FilesystemIterator::SKIP_DOTS | ||
) | ||
) as $file) { | ||
$rfile = str_replace('\\', '/', $file); | ||
if ($rfile !== $self && preg_match('/\\.php$/i', $rfile) && preg_match($regex, $rfile)) { | ||
$included[] = $file; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param string|Exp $exp | ||
* @param string $file | ||
* @return string | ||
* @throws InvalidArgumentException | ||
*/ | ||
private function processIncludePath($exp, string $file): string | ||
{ | ||
if (is_string($exp)) { | ||
return $exp; | ||
} | ||
if ($exp instanceof Concat) { | ||
return $this->processIncludePath($exp->left, $file).$this->processIncludePath($exp->right, $file); | ||
} | ||
if ($exp instanceof Dir) { | ||
return dirname($file); | ||
} | ||
if ($exp instanceof File) { | ||
return $file; | ||
} | ||
if ($exp instanceof ConstFetch && $exp->name === 'DIRECTORY_SEPARATOR') { | ||
return DIRECTORY_SEPARATOR; | ||
} | ||
if ($exp instanceof String_) { | ||
return $exp->value; | ||
} | ||
if ($exp instanceof Variable || $exp instanceof ConstFetch) { | ||
return '{var}'; | ||
} | ||
throw new InvalidArgumentException('can\'t yet handle '.$exp->getType()); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function enterNode(Node $node) | ||
{ | ||
if ($node instanceof Include_) { | ||
$this->included[] = $node->expr; | ||
} | ||
} | ||
} |
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