- Base is done; most of the language's components are now working!
- Implemented OPCACHE caching - Added View() token logic - New FileFetcher added to gather files from given view path
This commit is contained in:
1 parent
bcd49884fc
commit
6701a1cd59
16 files changed
+323
-69
No files matched your search
+64
-17
@@ -22,36 +22,52 @@ class Picea implements LanguageRegistration
|
||||
|
||||
public Caching\Cache $cache;
|
||||
|
||||
public FileFetcher $fileFetcher;
|
||||
|
||||
public bool $debug;
|
||||
|
||||
public function __construct(
|
||||
?Closure $responseHtml = null,
|
||||
?Cache $cache = null,
|
||||
?Context $context = null,
|
||||
?Compiler\Context $context = null,
|
||||
?Caching\Cache $cache = null,
|
||||
?Compiler $compiler = null,
|
||||
?LanguageRegistration $languageRegistration = null,
|
||||
?string $builderTemplatePath = null
|
||||
?FileFetcher $fileFetcher = null,
|
||||
?string $builderTemplatePath = null,
|
||||
bool $debug = false
|
||||
){
|
||||
$this->response = $responseHtml;
|
||||
$this->cache = $cache ?? new Caching\Memory("");
|
||||
$this->context = $context ?? new Compiler\BaseContext();
|
||||
$this->compiler = $compiler ?? $this->instanciateCompiler();
|
||||
$this->languageRegistration = $languageRegistration ?? new Language\DefaultRegistrations();
|
||||
$this->builderTemplatePath = $builderTemplatePath ?? dirname(__FILE__) . static::DEFAULT_BUILDER_TEMPLATE;
|
||||
$this->compiler = $compiler ?? $this->instanciateCompiler();
|
||||
$this->fileFetcher = $fileFetcher ?? $this->instanciateFileFetcher();
|
||||
$this->debug = $debug;
|
||||
}
|
||||
|
||||
public function renderHtml(string $viewPath, array $variables = [], ?object $proxy = null) : string
|
||||
{
|
||||
if ( null === $object = $this->fetchFromCache($viewPath, $variables, $proxy) ) {
|
||||
throw new \RuntimeException("An error occured while trying to save a compiled template.");
|
||||
}
|
||||
|
||||
return $object();
|
||||
}
|
||||
|
||||
public function outputHtml(string $viewPath, array $variables) : ResponseInterface
|
||||
{
|
||||
if ( $this->response ?? false ) {
|
||||
if ( false === $content = $this->cache->handle($viewPath) ) {
|
||||
# if ( )
|
||||
|
||||
}
|
||||
|
||||
$source = $this->compileSource($source ?? "test");
|
||||
|
||||
$response = $this->response;
|
||||
return $response("abc");
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException("No \Psr\Http\Message\ResponseInterface closure provided. Please provide one using the constructor or assinging it to the class variable `responseHtml`.");
|
||||
throw new \InvalidArgumentException("No \Psr\Http\Message\ResponseInterface closure provided. Please provide one using the constructor or assigning it to the class variable `responseHtml`.");
|
||||
}
|
||||
|
||||
public function compileSource(string $source) : array
|
||||
@@ -64,22 +80,22 @@ class Picea implements LanguageRegistration
|
||||
];
|
||||
}
|
||||
|
||||
public function compileFile(string $filePath) : array
|
||||
public function compileFile(string $viewPath) : array
|
||||
{
|
||||
if (! file_exists($filePath) ) {
|
||||
throw new \InvalidArgumentException("File `$filePath` cannot be found or there is a permission problem.");
|
||||
if (! file_exists($viewPath) ) {
|
||||
throw new \InvalidArgumentException("File `$viewPath` cannot be found or there is a permission problem.");
|
||||
}
|
||||
|
||||
if ( false === $fileContent = file_get_contents($filePath) ) {
|
||||
throw new \ErrorException("Given file could not be opened `$filePath`. This could indicate a permission misconfiguration on your file or folder.");
|
||||
if ( false === $fileContent = file_get_contents($viewPath) ) {
|
||||
throw new \ErrorException("Given file could not be opened `$viewPath`. This could indicate a permission misconfiguration on your file or folder.");
|
||||
}
|
||||
|
||||
$this->compiler->loadSourceCode($fileContent);
|
||||
$context = clone $this->context;
|
||||
$context->viewPath = $viewPath;
|
||||
$context->source = $this->compiler->compile($context);
|
||||
|
||||
return [
|
||||
'context' => $context = clone $this->context,
|
||||
'source' => $this->compiler->compile($context),
|
||||
];
|
||||
return $context;
|
||||
}
|
||||
|
||||
public function buildFromSource(string $source) : array
|
||||
@@ -99,9 +115,18 @@ class Picea implements LanguageRegistration
|
||||
];
|
||||
}
|
||||
|
||||
public function buildFromPath(string $path) : string
|
||||
public function fetchFromCache(string $viewPath, array $variables = [], ?object $proxy = null) : ?object
|
||||
{
|
||||
if ( $this->debug || ! $this->cache->compiled($viewPath) ) {
|
||||
$context = $this->compileView($viewPath);
|
||||
$this->cache->save($context);
|
||||
|
||||
if ( $context->extendFrom ) {
|
||||
$this->fetchFromCache($context->extendFrom, $variables, $proxy);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->cache->load($viewPath, $this, $variables, $proxy);
|
||||
}
|
||||
|
||||
public function instanciateCompiler() : Compiler
|
||||
@@ -114,6 +139,11 @@ class Picea implements LanguageRegistration
|
||||
return new Builder($this->builderTemplatePath);
|
||||
}
|
||||
|
||||
public function instanciateFileFetcher() : FileFetcher
|
||||
{
|
||||
return new FileFetcher();
|
||||
}
|
||||
|
||||
public function registerAll(Compiler $compiler) : void
|
||||
{
|
||||
$this->registerSyntax($compiler);
|
||||
@@ -135,4 +165,21 @@ class Picea implements LanguageRegistration
|
||||
{
|
||||
$this->languageRegistration->registerExtension($compiler);
|
||||
}
|
||||
|
||||
protected function compileView(string $viewPath) : Compiler\Context
|
||||
{
|
||||
$tmpFolder = sys_get_temp_dir();
|
||||
$builder = $this->instanciateBuilder();
|
||||
$compiled = $this->compileSource($this->fileFetcher->getFileContent($viewPath));
|
||||
$context = $compiled['context'];
|
||||
$context->viewPath = $viewPath;
|
||||
$context = $builder->build($compiled['context'], $compiled['source']) ;
|
||||
$context->classPath = $tmpFolder . DIRECTORY_SEPARATOR . $context->className . ".php";
|
||||
|
||||
if ( $context->extendFrom ) {
|
||||
$this->compileView($context->extendFrom);
|
||||
}
|
||||
|
||||
return $context;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user