- Work on extension integration.

- WIP of the inline view token
- Began work on compiled-code caching
This commit is contained in:
Dave M. 2019-09-04 15:14:49 +00:00
parent 66c7b349d1
commit f8e3dbd60f
8 changed files with 105 additions and 11 deletions

View File

@ -4,6 +4,8 @@ namespace Picea;
class Builder
{
const TEMPLATE_CLASSNAME_PREFIX = "PiceaTemplate_";
protected string $templatePath = "";
public function __construct(string $templatePath = "./Builder/ClassTemplate.php")
@ -13,13 +15,13 @@ class Builder
public function build(Compiler\Context &$context, string $compiledSource) : array
{
$className = "PiceaTemplate_" . $this->generateClassUID($compiledSource);
$className = static::generateClassName($compiledSource);
$replace = [
'%NAMESPACE%' => $context->namespace,
'%USE%' => $context->renderUses(),
'%CLASSNAME%' => $className,
'%EXTENDS%' => '',
'%EXTENDS%' => $context->extendFrom ? static::generateClassUID($context->extendFrom) : '',
'%CONTENT%' => $compiledSource,
];
@ -30,7 +32,12 @@ class Builder
];
}
public function generateClassUID(string $filePath) : string
public static function generateClassName(string $filepath) : string
{
return static::TEMPLATE_CLASSNAME_PREFIX . static::generateClassUID($filepath);
}
public static function generateClassUID(string $filePath) : string
{
return md5($filePath);
}

32
src/Caching/Memory.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace Picea\Caching;
class Memory {
protected string $cachePath = "";
public function __construct(string $cachePath)
{
$this->cachePath = $cachePath;
}
public function loadFile(string $fileName) : bool
{
if ( file_exists($path = $this->filePath($fileName)) ) {
require_once($path);
}
return false;
}
public function saveFile(string $fileName) : bool
{
}
public function filePath(string $fileName) : string
{
return $this->cachePath . $fileName;
}
}

35
src/Caching/Opcache.php Normal file
View File

@ -0,0 +1,35 @@
<?php
namespace Picea\Caching;
class Opcache {
protected string $cachePath = "";
public function __construct(string $cachePath)
{
$this->cachePath = $cachePath;
}
public function loadFile(string $fileName) : bool
{
if ( file_exists($path = $this->filePath($fileName)) ) {
require_once($path);
return true;
}
return false;
}
public function saveFile(string $fileName, string $content) : bool
{
file_put_contents($this->filePath($fileName), $content);
return false;
}
public function filePath(string $fileName) : string
{
return $this->cachePath . $fileName;
}
}

View File

@ -42,7 +42,7 @@ class Compiler
return $this->tagList[$token]->parse($context, $arguments);
}
elseif ( $this->extensionList[$token] ?? false ) {
return $this->extensionList[$token]->parse($context, $arguments);
return $this->extensionList[$token]->parse($context, $arguments, $token);
}
else {
throw new \LogicException("Impossible to find token `$token` declared in `{$matches[2]}`. Perhapse you forgot to add a custom token to Picea's engine ?");

View File

@ -0,0 +1,13 @@
<?php
namespace Picea\ControlStructure;
class ViewToken implements ControlStructure {
public string $token = "view";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
#$context->switchStack[] = true;
#return "<?php switch($arguments):";
}
}

View File

@ -3,5 +3,5 @@
namespace Picea\Extension;
interface Extension {
public function parse(\Picae\Compiler\Context &$context, string $sourceCode);
public function parse(\Picae\Compiler\Context &$context, string $sourceCode, string $token);
}

View File

@ -7,7 +7,7 @@ class JsonExtension implements Extension {
public string $token = "json";
public int $flags = JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_QUOT | \JSON_THROW_ON_ERROR;
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments) {
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
return "<?php echo json_encode($arguments, {$this->flags}) ?>";
}

View File

@ -6,16 +6,21 @@ class Picea
{
public Compiler\Context $context;
public function __construct(?Context $context = null)
public Compiler $compiler;
public function __construct(?Context $context = null, ?Compiler $compiler = null)
{
$this->context = $context ?? new Compiler\BaseContext();
$this->compiler = $compiler ?: $this->instanciateCompiler();
}
public function compileSource(string $source) : array
{
$this->compiler->loadSourceCode($source);
return [
'context' => $context = clone $this->context,
'source' => $this->instanciateCompiler($source)->compile($context)
'source' => $this->compiler->compile($context),
];
}
@ -29,9 +34,11 @@ class Picea
throw new \ErrorException("Given file could not be opened `$filePath`. This could indicate a permission misconfiguration on your file or folder");
}
$this->compiler->loadSourceCode($fileContent);
return [
'context' => $context = clone $this->context,
'source' => $this->instanciateCompiler($fileContent)->compile($context)
'source' => $this->compiler->compile($context),
];
}
@ -57,7 +64,7 @@ class Picea
}
public function instanciateCompiler(string $sourceCode) : Compiler
public function instanciateCompiler(string $sourceCode = "") : Compiler
{
$compiler = new Compiler();
$compiler->loadSourceCode($sourceCode);