- First commit of first draft
This commit is contained in:
commit
66c7b349d1
36 files changed
+820
No files matched your search
@@ -0,0 +1,84 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Picea;
|
||||
|
||||
class Compiler
|
||||
{
|
||||
protected string $sourceCode = "";
|
||||
|
||||
protected array $syntaxObjectList = [];
|
||||
|
||||
protected array $tagList = [];
|
||||
|
||||
protected string $tagTokenOpen = "\{%";
|
||||
|
||||
protected string $tagTokenClose = "%\}";
|
||||
|
||||
protected array $extensionList = [];
|
||||
|
||||
public function compile(Compiler\Context $context) : string
|
||||
{
|
||||
$context->compiler = $this;
|
||||
|
||||
/**
|
||||
* Syntaxes such as {{ echo }} & {{= echo raw }} && {# comment #}
|
||||
*/
|
||||
foreach($this->syntaxObjectList as $syntax) {
|
||||
$syntax->parse($context, $this->sourceCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Control structures & functions {% functionName arguments %}
|
||||
*/
|
||||
$replace = "#({$this->tagTokenOpen})(.*?)({$this->tagTokenClose})#s";
|
||||
|
||||
$this->sourceCode = preg_replace_callback($replace, function ($matches) use (&$context) {
|
||||
$matches[2] = trim($matches[2]);
|
||||
|
||||
list($token, $arguments) = array_pad(array_filter(explode(' ', $matches[2], 2), 'strlen'), 2, null);
|
||||
|
||||
# @TODO Refractor this parts to allows registration to the tag's name
|
||||
if ( $this->tagList[$token] ?? false ) {
|
||||
return $this->tagList[$token]->parse($context, $arguments);
|
||||
}
|
||||
elseif ( $this->extensionList[$token] ?? false ) {
|
||||
return $this->extensionList[$token]->parse($context, $arguments);
|
||||
}
|
||||
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 ?");
|
||||
}
|
||||
|
||||
}, $this->sourceCode);
|
||||
|
||||
return $this->sourceCode;
|
||||
}
|
||||
|
||||
public function loadSourceCode($html) : self
|
||||
{
|
||||
$this->sourceCode = $html;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function registerExtension(Extension\Extension $extension) : self
|
||||
{
|
||||
$this->extensionList[$extension->token] = $extension;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function registerCaching(CachingStrategy $cachingStrategy) : self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function registerSyntax(Syntax\Syntax $syntaxObject) : self
|
||||
{
|
||||
$this->syntaxObjectList[] = $syntaxObject;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function registerControlStructure(ControlStructure\ControlStructure $controlStructureObject) : self
|
||||
{
|
||||
$this->tagList[$controlStructureObject->token] = $controlStructureObject;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user