- Small refractoring of code, merge tokens

- Completed block token
This commit is contained in:
Dave Mc Nicoll committed 2020-05-04 22:50:02 -04:00
1 parent bf3816c3db
commit ce4ef6351a
21 files changed
+370 -194

No files matched your search

+195 -7
View File
@@ -4,14 +4,20 @@ namespace Picea\ControlStructure;
class BlockToken implements ControlStructure {
public array $token = [ "block", "arguments" ];
public array $token = [ "arguments", "block", "endblock", "define", "slot", "endslot", "using" ];
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
static $slotDefinitions = [];
switch($token) {
case "block":
return "<?php echo \$___class__template->picea->inlineHtml(\$this, $arguments); ?>";
$slotDefinitions[] = $this->slotDefinitions();
return "<?php \$___block = \Picea\ControlStructure\BlockToken::instanciateBlock($arguments); ?>";
case "endblock":
return "<?php echo \$___block->render(\$___class__template); unset(\$___block); ?>";
case "arguments":
$class = static::class;
@@ -26,10 +32,73 @@ class BlockToken implements ControlStructure {
throw new \Exception('A block awaiting arguments `$arguments` instead received `' . implode(', ', array_map('gettype', \$inlineVariables)) . '`');
}
?>
PHP;
}
PHP;
case "define":
list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, "");
end($slotDefinitions)->setDefinition(eval("return $name;"), $definition);
return <<<PHP
<?php \$this->defineSlot($name, function($definition) {}); ?>
PHP;
case "slot":
$def = end($slotDefinitions);
list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, "");
if ($def->hasDefinitions() ) {
$slotName = eval("return $name;");
$def->currentSlot = $slotName;
$def->setDefinitionVars($slotName, $definition);
$definition = $def->printDefinition($slotName);
return <<<PHP
<?php \$this->printSlot($name, function($definition, array \$___using = []) { extract(\$___using, \EXTR_SKIP); ?>
PHP;
}
else {
return <<<PHP
<?php \$___block->slotIsSet($name) || \$___block->setSlot($name, function($definition, array \$___using = []) { extract(\$___using, \EXTR_SKIP); ?>
PHP;
}
case "endslot":
$def =end($slotDefinitions);
if ($def->hasDefinitions() ) {
$definition = $def->getCurrentSlotDefinitionVars();
if ($definition) {
$definition = "$definition,";
}
return <<<PHP
<?php })->call(\$this, $definition \$this->using); ?>
PHP;
}
else {
return "<?php }); ?>";
}
case "using":
return "<?php \$___block->setUsing($arguments); ?>";
}
}
public function inlineHtml(? object $proxy, string $viewPath, ... $variables) {
return $this->renderHtml($viewPath, [ 'inlineVariables' => $variables ], $proxy);
}
public static function parseSlotArguments(Callable $method) : array
{
#return
}
public static function parseArguments(Callable $method, array $arguments) : array
{
try{
@@ -42,9 +111,128 @@ class BlockToken implements ControlStructure {
$parameters = [];
foreach((new \ReflectionFunction($method))->getParameters() as $key => $value) {
$parameters[ $value->getName() ] = $arguments[$key] ?? $value->getDefaultValue();
if ( isset($arguments[$key]) ) {
$parameters[ $value->getName() ] = $arguments[$key];
}
elseif ( $value->isDefaultValueAvailable() ) {
$parameters[ $value->getName() ] = $value->getDefaultValue();
}
elseif ( $value->isVariadic() ) {
$parameters[ $value->getName() ] = [];
}
}
return $parameters;
}
public static function slotDefinitions() {
return new class() {
public array $definitions = [];
public array $variables = [];
public bool $rendering = false;
public string $currentSlot;
public function setDefinition(string $name, string $definition) : self
{
$this->definitions[$name] = $definition;
return $this;
}
public function setDefinitionVars(string $name, string $variables) : self
{
$this->variables[$name] = $variables;
return $this;
}
public function getDefinitionVars(string $name) : string
{
return $this->variables[$name] ?? "";
}
public function getCurrentSlotDefinitionVars() : string
{
return $this->getDefinitionVars($this->currentSlot);
}
public function hasDefinitions() : bool
{
return count($this->definitions) > 0;
}
public function printDefinition(string $name) : string
{
if ( ! isset($this->definitions[$name]) ) {
throw new \Exception("Slot definition for `$name` was not found. Have you defined it in your block header ?");
}
return $this->definitions[$name];
}
public function render() : void
{
$this->rendering = true;
}
};
}
public static function instanciateBlock(string $viewPath, ... $arguments)
{
return new class($viewPath, ...$arguments) {
public bool $rendering = false;
public string $viewPath;
public array $using = [];
public array $arguments = [];
public array $slots = [];
public array $definition = [];
public function __construct(string $viewPath, ...$arguments) {
$this->viewPath = $viewPath;
$this->arguments = $arguments;
}
public function render(object $classTemplate) : string
{
$this->rendering = true;
return $classTemplate->picea->inlineHtml($this, $this->viewPath, ...$this->arguments);
}
public function setSlot(string $name, Callable $method) : void
{
$this->slots[$name] = $method;
}
public function defineSlot(string $name, Callable $method) : void
{
$this->definition[$name] = $method;
}
public function slotIsSet(string $name) : bool
{
return ! empty($this->slots[$name]);
}
public function printSlot(string $name, Callable $default)
{
return $this->slotIsSet($name) ? $this->slots[$name] : $default;
}
public function setUsing(array $variables) {
$this->using = $variables;
}
};
}
}
-13
View File
@@ -1,13 +0,0 @@
<?php
namespace Picea\ControlStructure;
class ElseIfToken implements ControlStructure {
public string $token = "elseif";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
return "<?php elseif ($arguments): ?>";
}
}
-13
View File
@@ -1,13 +0,0 @@
<?php
namespace Picea\ControlStructure;
class ElseToken implements ControlStructure {
public string $token = "else";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
return "<?php else: ?>";
}
}
-7
View File
@@ -1,7 +0,0 @@
<?php
namespace Picea\ControlStructure;
class EndCaseToken extends BreakToken {
public string $token = "endcase";
}
-13
View File
@@ -1,13 +0,0 @@
<?php
namespace Picea\ControlStructure;
class EndIfToken implements ControlStructure {
public string $token = "endif";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
return "<?php endif ?>";
}
}
-18
View File
@@ -1,18 +0,0 @@
<?php
namespace Picea\ControlStructure;
class EndSectionToken implements ControlStructure {
public string $token = "endsection";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
if ( empty($context->sections) ) {
throw new \RuntimeException("A section closing tag {% endsection %} was found without an opening {% section %} tag");
}
$section = array_pop($context->sections);
$build = $context->extendFrom ? "" : "\$___class__template->renderSection({$section['name']});";
return "<?php }]; $build?>";
}
}
-12
View File
@@ -1,12 +0,0 @@
<?php
namespace Picea\ControlStructure;
class EndSwitchToken implements ControlStructure {
public string $token = "endswitch";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
return "<?php endswitch; ?>";
}
}
-20
View File
@@ -1,20 +0,0 @@
<?php
namespace Picea\ControlStructure;
class EndforToken implements ControlStructure {
public string $token = "endfor";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
if ( end($context->iterationStack)['or'] === false ) {
$output = "<?php endfor; ?>";
}
else {
$output = "<?php endif; ?>";
}
array_pop($context->iterationStack);
return $output;
}
}
-20
View File
@@ -1,20 +0,0 @@
<?php
namespace Picea\ControlStructure;
class EndforeachToken implements ControlStructure {
public string $token = "endforeach";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
if ( end($context->iterationStack)['or'] === false ) {
$output = "<?php endforeach; ?>";
}
else {
$output = "<?php endif; ?>";
}
array_pop($context->iterationStack);
return $output;
}
}
+23 -8
View File
@@ -4,17 +4,32 @@ namespace Picea\ControlStructure;
class ForToken implements ControlStructure {
public string $token = "for";
public array $token = [ "for", "endfor" ];
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
$uid = "$".uniqid("for_");
switch($token) {
case "for":
$uid = "$".uniqid("for_");
$context->iterationStack[] = [
'or' => false,
'uid' => $uid,
'token' => 'endfor',
];
$context->iterationStack[] = [
'or' => false,
'uid' => $uid,
'token' => 'endfor',
];
return "<?php for ($arguments): {$uid} = 1; ?>";
return "<?php for ($arguments): {$uid} = 1; ?>";
case "endfor":
if ( end($context->iterationStack)['or'] === false ) {
$output = "<?php endfor; ?>";
}
else {
$output = "<?php endif; ?>";
}
array_pop($context->iterationStack);
return $output;
}
}
}
+30 -15
View File
@@ -4,23 +4,38 @@ namespace Picea\ControlStructure;
class ForeachToken implements ControlStructure {
public string $token = "foreach";
public array $token = [ "foreach", "endforeach" ];
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
$name = "$".uniqid("foreach_");
$count = count($context->iterationStack ?? []);
if ( $count > 0 ) {
$name .= "[" . $context->iterationStack[$count - 1]['uid'] . "]";
}
switch($token) {
case "foreach":
$name = "$".uniqid("foreach_");
$context->iterationStack[] = [
'or' => false,
'uid' => $name,
'token' => 'endforeach',
];
return "<?php foreach ($arguments): $name = ( $name ?? 0 ) + 1; ; ?>";
$count = count($context->iterationStack ?? []);
if ( $count > 0 ) {
$name .= "[" . $context->iterationStack[$count - 1]['uid'] . "]";
}
$context->iterationStack[] = [
'or' => false,
'uid' => $name,
'token' => 'endforeach',
];
return "<?php foreach ($arguments): $name = ( $name ?? 0 ) + 1; ; ?>";
case "endforeach":
if ( end($context->iterationStack)['or'] === false ) {
$output = "<?php endforeach; ?>";
}
else {
$output = "<?php endif; ?>";
}
array_pop($context->iterationStack);
return $output;
}
}
}
+14 -2
View File
@@ -4,10 +4,22 @@ namespace Picea\ControlStructure;
class IfToken implements ControlStructure {
public string $token = "if";
public array $token = [ "if", "else", "elseif", "endif" ];
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
return "<?php if ($arguments): ?>";
switch($token) {
case "if":
return "<?php if ($arguments): ?>";
case "else":
return "<?php else: ?>";
case "elseif":
return "<?php elseif ($arguments): ?>";
case "endif":
return "<?php endif ?>";
}
}
}
+24 -2
View File
@@ -4,9 +4,24 @@ namespace Picea\ControlStructure;
class SectionToken implements ControlStructure {
public string $token = "section";
public array $token = [ "section", "endsection" ];
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
switch($token) {
case "section":
return $this->printSection($context, $arguments);
case "endsection":
if ( empty($context->sections) ) {
throw new \RuntimeException("A section closing tag {% endsection %} was found without an opening {% section %} tag");
}
return $this->printEndSection($context);
}
}
protected function printSection($context, ?string $arguments) : string
{
list($name, $options) = array_pad(explode(',', $arguments, 2), 2, null);
if ( $options ?? false ) {
@@ -34,4 +49,11 @@ class SectionToken implements ControlStructure {
\$___class__template->sectionList[$name]['$action'][] = [ 'order' => $order, 'callback' => function() use (\$picea, \$___class__template, \$___global_variables, \$___variables) {
extract(\$___global_variables); extract(\$___variables, \EXTR_OVERWRITE); ?>";
}
}
protected function printEndSection($context) : string
{
$section = array_pop($context->sections);
$build = $context->extendFrom ? "" : "\$___class__template->renderSection({$section['name']});";
return "<?php }]; $build?>";
}
}
+24 -3
View File
@@ -4,10 +4,31 @@ namespace Picea\ControlStructure;
class SwitchToken implements ControlStructure {
public string $token = "switch";
public array $token = [ "switch", "case", "endswitch" ];
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
$context->switchStack[] = true;
return "<?php switch($arguments):";
switch($token) {
case "switch":
$context->switchStack[] = true;
return "<?php switch($arguments):";
case "case":
$output = "";
if ( $context->switchStack ) {
array_pop($context->switchStack);
}
else {
$output = "<?php ";
}
return ( $output ?? "" ) . "case $arguments: ?>";
case "endcase":
return "<?php break; ?>";
case "endswitch":
return "<?php endswitch; ?>";
}
}
}