Skip to content

Commit

Permalink
skip exports unless asked for
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Nov 5, 2023
1 parent 250aea4 commit 5a0a26b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/twig/ExportTokenNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ public function compile(\Twig\Compiler $compiler)
{
$compiler
->addDebugInfo($this)
->write('$cb=function() use ($context){')
->write($this->getAttribute('capture') ? 'ob_start();'.PHP_EOL : '$value=')
->subcompile($this->getNode('value'))
->write($this->getAttribute('capture') ? '$value=ob_get_contents();ob_end_clean();'.PHP_EOL : '')
->write(';'.PHP_EOL)
->write('($context[\'exports\'] ?? new class{function add(){}})->add(\''.$this->getAttribute('name').'\', $value);'.PHP_EOL);
->write('return $value;};')
->write('(! empty($context[\'exports\'])) ? $context[\'exports\']->add(\''.$this->getAttribute('name').'\', $cb()) : null;'.PHP_EOL)
;
}
}
12 changes: 12 additions & 0 deletions tests/ExportsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use markhuot\keystone\models\Component;
use Twig\Error\RuntimeError;

it('nulls circular references', function () {
$component = Component::factory()->type('site/components/dynamic-prop-types')->create();
Expand All @@ -24,3 +25,14 @@

expect($component->getSummary())->toBe('bar');
});

it('skips exports unless instructed', function () {
$component = Component::factory()->type('site/components/skipped-export')->create();
$component->render();
});

it('gets exports when instructed', function() {
$this->expectException(RuntimeError::class);
$component = Component::factory()->type('site/components/skipped-export')->create();
$component->getType()->getExports();
});
1 change: 1 addition & 0 deletions tests/templates/components/skipped-export.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% export foo = craft.app.end() %}

0 comments on commit 5a0a26b

Please sign in to comment.