This commit is contained in:
Dave M. 2022-06-27 17:29:31 +00:00
commit 531f786747
5 changed files with 97 additions and 6 deletions

View File

@ -24,14 +24,13 @@ class BlockToken implements ControlStructure {
return <<<PHP
<?php
try {
extract( \$this->using ?? [], \EXTR_OVERWRITE);
extract( \\$class::parseArguments(function($arguments) {}, \$inlineVariables), \EXTR_OVERWRITE);
unset(\$inlineVariables);
}
catch(\TypeError \$ex) {
throw new \Exception(
sprintf('A block awaiting arguments `%s` instead received `%s` with values `%s`', '$arguments', implode(', ', array_map('gettype', \$inlineVariables)), json_encode(\$inlineVariables))
sprintf('A block awaiting arguments `%s` instead received `%s` with values `%s`', '$arguments', implode(', ', array_map('gettype', \$inlineVariables)), json_encode(\$inlineVariables))
);
}
?>
@ -72,7 +71,7 @@ class BlockToken implements ControlStructure {
}
return <<<PHP
<?php \$___block->slotIsSet($name) || \$___block->setSlot($name, function($definition array \$___using = []) use (\$picea) { extract(\$___using, \EXTR_SKIP); ?>
<?php (\$___block ?? \$this)->slotIsSet($name) || \$___block->setSlot($name, function($definition array \$___using = []) use (\$picea) { extract(\$___using, \EXTR_SKIP); ?>
PHP;
}
@ -131,6 +130,9 @@ class BlockToken implements ControlStructure {
elseif ( $value->isVariadic() ) {
$parameters[ $value->getName() ] = [];
}
else {
$parameters[ $value->getName() ] = null;
}
}
return $parameters;

View File

@ -0,0 +1,47 @@
<?php
namespace Picea\ControlStructure;
class FunctionToken implements ControlStructure {
public array $token = [ "function", "endfunction", "return" ];
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
switch($token) {
case "function":
return $this->printFunction($context, $arguments);
case "return":
if ( empty($context->functions) ) {
throw new \RuntimeException("A function return tag {% return %} was found without an opening {% function ... %} tag");
}
return $this->printReturn($context, $arguments);
case "endfunction":
if ( empty($context->functions) ) {
throw new \RuntimeException("A function closing tag {% endfunction %} was found without an opening {% function ... %} tag");
}
$context->functions--;
return $this->printEndFunction($context);
}
}
protected function printFunction($context, ?string $arguments) : string
{
$context->functions++;
return "<?php function $arguments { ?>";
}
protected function printReturn($context, ?string $arguments) : string
{
return "<?php return $arguments; ?>";
}
protected function printEndFunction($context) : string
{
return "<?php } ?>";
}
}

View File

@ -53,7 +53,7 @@ class SectionToken implements ControlStructure {
protected function printEndSection($context) : string
{
$section = array_pop($context->sections);
$build = $context->extendFrom ? "\$___class__template->sectionStack && \$___class__template->renderSection({$section['name']});" : "\$___class__template->renderSection({$section['name']});";
$build = $context->extendFrom ? "!empty(\$___class__template->sectionStack) && \$___class__template->renderSection({$section['name']});" : "\$___class__template->renderSection({$section['name']});";
return "<?php array_pop(\$___class__template->sectionStack); }]; $build?>";
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Picea\Extension;
use Picea\Compiler\Context;
class MoneyExtension implements Extension {
public string $token = "money";
public string $title = "";
public string $locale;
public \NumberFormatter $formatter;
public function __construct(Context $context) {
$this->register($context);
$this->locale = explode('.', \Locale::getDefault())[0];
$this->formatter = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY);
}
public function register(Context $context) : void
{
$context->pushFunction("money", [ $this, 'money' ]);
}
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
return "<?php echo money($arguments) ?>";
}
public function money(float $money, ? string $currency = null) /* : string|false */
{
$this->formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, 'CAD');
$this->formatter->setPattern( str_replace('¤#','¤ #', $this->formatter->getPattern() ) );
return $this->formatter->formatCurrency($money, $currency ?? $this->formatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE));
}
}

View File

@ -52,6 +52,7 @@ class DefaultRegistrations implements LanguageRegistration
$compiler->registerControlStructure(new \Picea\ControlStructure\ContinueToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\ExtendsToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\SectionToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\FunctionToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\BlockToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\IncludeToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\ViewToken());