From f8e3dbd60f0e28a9c02c33f05352edc520b12dba Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 4 Sep 2019 15:14:49 +0000 Subject: [PATCH] - Work on extension integration. - WIP of the inline view token - Began work on compiled-code caching --- src/Builder.php | 13 ++++++++--- src/Caching/Memory.php | 32 +++++++++++++++++++++++++++ src/Caching/Opcache.php | 35 ++++++++++++++++++++++++++++++ src/Compiler.php | 4 ++-- src/ControlStructure/ViewToken.php | 13 +++++++++++ src/Extension/Extension.php | 2 +- src/Extension/JsonExtension.php | 2 +- src/Picea.php | 15 +++++++++---- 8 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 src/Caching/Memory.php create mode 100644 src/Caching/Opcache.php create mode 100644 src/ControlStructure/ViewToken.php diff --git a/src/Builder.php b/src/Builder.php index 854f061..c4aab6b 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -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); } diff --git a/src/Caching/Memory.php b/src/Caching/Memory.php new file mode 100644 index 0000000..6fb08c0 --- /dev/null +++ b/src/Caching/Memory.php @@ -0,0 +1,32 @@ +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; + } +} diff --git a/src/Caching/Opcache.php b/src/Caching/Opcache.php new file mode 100644 index 0000000..71eb5d6 --- /dev/null +++ b/src/Caching/Opcache.php @@ -0,0 +1,35 @@ +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; + } +} diff --git a/src/Compiler.php b/src/Compiler.php index 7cf3b03..1c9280d 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -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 ?"); @@ -52,7 +52,7 @@ class Compiler return $this->sourceCode; } - + public function loadSourceCode($html) : self { $this->sourceCode = $html; diff --git a/src/ControlStructure/ViewToken.php b/src/ControlStructure/ViewToken.php new file mode 100644 index 0000000..d4f70bf --- /dev/null +++ b/src/ControlStructure/ViewToken.php @@ -0,0 +1,13 @@ +switchStack[] = true; + #return "flags}) ?>"; } diff --git a/src/Picea.php b/src/Picea.php index aea30c1..d0b90ea 100644 --- a/src/Picea.php +++ b/src/Picea.php @@ -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);