-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('); ?>'); | ||
} | ||
} |