Skip to content

Commit

Permalink
#2283 - Improve CompilerFile:genIR() method
Browse files Browse the repository at this point in the history
By removing .php files and require each to get AST array
  • Loading branch information
Jeckerson committed Oct 2, 2021
1 parent a4c4d88 commit 15e10d3
Showing 1 changed file with 11 additions and 35 deletions.
46 changes: 11 additions & 35 deletions Library/CompilerFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ final class CompilerFile implements FileInterface
use LoggerAwareTrait;

/**
* @var string
* @var string|null
*/
private string $namespace;
private ?string $namespace = null;

/**
* @var string
* @var string|null
*/
private string $className;
private ?string $className = null;

/**
* @var string
* @var string|null
*/
private string $filePath;
private ?string $filePath = null;

/**
* @var bool
Expand Down Expand Up @@ -207,48 +207,24 @@ public function genIR(Compiler $compiler): array
{
$normalizedPath = $this->filesystem->normalizePath($this->filePath);

// TODO: JS => JSON
$compilePath = "{$normalizedPath}.js";
$compilePath = "{$normalizedPath}.json";
$zepRealPath = realpath($this->filePath);

if ($this->filesystem->exists($compilePath)) {
$modificationTime = $this->filesystem->modificationTime($compilePath);
if ($modificationTime < filemtime($zepRealPath)) {
if ($this->filesystem->modificationTime($compilePath) < filemtime($zepRealPath)) {
$this->filesystem->delete($compilePath);
if (false != $this->filesystem->exists($compilePath.'.php')) {
$this->filesystem->delete($compilePath.'.php');
}
}
}

$ir = null;
if (!$this->filesystem->exists($compilePath)) {
$parser = $compiler->getParserManager()->getParser();
$ir = $parser->parse($zepRealPath);
$this->filesystem->write($compilePath, json_encode($ir, JSON_PRETTY_PRINT));
} else {
$ir = json_decode($this->filesystem->read($compilePath), true);
}

if (!$this->filesystem->exists($compilePath.'.php')) {
if (empty($ir)) {
$ir = json_decode($this->filesystem->read($compilePath), true);
}

$data = '<?php return '.var_export($ir, true).';';
$this->filesystem->write($compilePath.'.php', $data);
}

$contents = $this->filesystem->requireFile($compilePath.'.php');

if (!is_array($contents)) {
throw new IllegalStateException(
sprintf(
'Generating the intermediate representation for the file "%s" was failed.',
realpath($this->filePath)
)
);
}

return $contents;
return $ir;
}

/**
Expand Down

0 comments on commit 15e10d3

Please sign in to comment.