Skip to content

Commit

Permalink
propnode added
Browse files Browse the repository at this point in the history
  • Loading branch information
Rein Van Oyen authored and Rein Van Oyen committed Dec 20, 2019
1 parent 36fc1f5 commit 4ea6b25
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/Node/PropNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Aegis\Node;

use Aegis\Contracts\CompilerInterface;
use Aegis\Contracts\ParserInterface;
use Aegis\Token\TokenType;

/**
* Class PropNode
* @package Aegis\Node
*/
class PropNode extends Node
{
public static function parse(ParserInterface $parser)
{
if (
$parser->accept(TokenType::T_IDENT, 'prop') ||
$parser->accept(TokenType::T_IDENT, 'property')
) {
$parser->insert(new static());
$parser->advance();
$parser->traverseUp();

if (!ExpressionNode::parse($parser)) {
$parser->syntaxError('Unexpected token' . $parser->getCurrentToken() . ', expected expression');
}
$parser->setAttribute('propName');

if (ExpressionNode::parse($parser)) {
$parser->setAttribute('propValue');
}

$parser->expect(TokenType::T_CLOSING_TAG);
$parser->advance();
$parser->traverseDown();
$parser->parseOutsideTag();
}
}

public function compile(CompilerInterface $compiler)
{
$propNameAttribute = $this->getAttribute('propName');
$subcompiler = $compiler->clone();
$propName = $subcompiler->compile($propNameAttribute);

// Write head of itself
$compiler->head('<?php $env->setBlock(');
$compiler->head($propName);
$compiler->head(', function() use ($env, $tpl) { ?>');

if ($this->getAttribute('propValue')) {

$subcompiler = $compiler->clone();
$propValue = $subcompiler->compile($this->getAttribute('propValue'));

$compiler->head('<?php echo htmlspecialchars(');
$compiler->head($propValue);
$compiler->head('); ?>');
}

$compiler->head('<?php }); ?>');

// Render itself
$compiler->write('<?php $env->getBlock( ');
$compiler->write($propName);
$compiler->write('); ?>');
}
}

0 comments on commit 4ea6b25

Please sign in to comment.