Skip to content
Permalink
Browse files

Make Build\Compiler reusable

  • Loading branch information...
apeschar committed Oct 5, 2019
1 parent 12f69bf commit 195990e870622e646a6c05b6fb3b208d0f031826
Showing with 40 additions and 24 deletions.
  1. +1 −5 bin/compile
  2. +24 −10 src/Build/Compiler.php
  3. +15 −9 src/Build/ScriptInliner.php
@@ -3,8 +3,4 @@
require_once __DIR__ . '/../vendor/autoload.php';
if (!chdir(__DIR__ . '/..')) {
throw new RuntimeException("Could not change dir into project root");
}
echo (new Kibo\Phast\Build\Compiler())->run();
echo Kibo\Phast\Build\Compiler::getPhastCompiler()->getResult();
@@ -13,22 +13,36 @@
class Compiler {
const INCLUDE_DIRS = [
'src',
'vendor/mrclay/jsmin-php/src',
];
private $include = [];
private $exclude = [];
const EXCLUDE_DIRS = [
'src/Build',
];
public static function getPhastCompiler(): self {
$root = __DIR__ . '/../..';
public function run() {
return (new self)
->include($root . '/src')
->exclude($root . '/src/Build')
->include($root . '/vendor/mrclay/jsmin-php/src');
}
public function include(string $dir): self {
$this->include[] = $dir;
return $this;
}
public function exclude(string $dir): self {
$this->exclude[] = $dir;
return $this;
}
public function getResult(): string {
$combinedTree = [];
/** @var SplFileInfo $fileinfo */
foreach ($this->getSourceFiles() as $fileinfo) {
$tree = $this->parseFile($fileinfo);
$namespace = $this->getSingleNamespace($tree, $fileinfo);
$namespace->file = $fileinfo;
$nameResolver = new NameResolver;
$nodeTraverser = new NodeTraverser;
$nodeTraverser->addVisitor($nameResolver);
@@ -46,12 +60,12 @@ public function run() {
}
private function getSourceFiles() {
foreach (self::INCLUDE_DIRS as $dir) {
foreach ($this->include as $dir) {
/** @var SplFileInfo $fileinfo */
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir))
as $fileinfo
) {
foreach (self::EXCLUDE_DIRS as $exclude) {
foreach ($this->exclude as $exclude) {
if (strpos($fileinfo->getPathname(), $exclude . '/') === 0) {
continue 2;
}
@@ -1,18 +1,25 @@
<?php
namespace Kibo\Phast\Build;
use Kibo\Phast\Common\JSMinifier;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\NodeVisitorAbstract;
class ScriptInliner extends NodeVisitorAbstract {
/** @var Namespace_|null */
private $namespace;
/** @var ClassLike|null */
private $class;
public function enterNode(Node $node) {
if ($node instanceof Namespace_) {
$this->namespace = $node;
return;
}
if ($node instanceof ClassLike) {
$this->class = $node;
return;
@@ -37,10 +44,6 @@ public function enterNode(Node $node) {
}
private function inlineValue($value, &$filename = null) {
if (!$this->class) {
throw new \RuntimeException(
"Expected to be inside class when seeing call with filename");
}
$file = $this->getFilenameFromValue($value);
if ($file === null) {
return;
@@ -82,9 +85,15 @@ public function leaveNode(Node $node) {
if ($node instanceof ClassLike) {
$this->class = null;
}
if ($node instanceof Namespace_) {
$this->namespace = null;
}
}
private function getFilenameFromValue($value) {
if (!isset($this->namespace->file)) {
throw new \RuntimeException("Namespace is missing file property");
}
if (!$value instanceof Node\Expr\BinaryOp\Concat) {
return;
}
@@ -97,10 +106,7 @@ private function getFilenameFromValue($value) {
if (strpos($value->right->value, '/') !== 0) {
return;
}
$nsParts = explode('\\', $this->class->namespacedName);
$nsParts = array_slice($nsParts, 2);
array_pop($nsParts);
return implode('/', array_merge(['src'], $nsParts, [substr($value->right->value, 1)]));
return $this->namespace->file->getPath() . $value->right->value;
}
}

0 comments on commit 195990e

Please sign in to comment.
You can’t perform that action at this time.