Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion packages/admin/src/Charcoal/Admin/Mustache/AssetsHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,23 @@ protected function reset()
/**
* Magic: Render the Mustache section.
*
* Mustache 3+ may invoke callables while resolving dotted names
* (e.g. `assets.output.css`) with zero arguments. In that case,
* return `$this` so `__get()` can continue.
*
* Mustache 3.2 interpolates the final dotted-name value instead of
* invoking it as a lambda. `__toString()` runs the pending action.
*
* @param string $text The translation key.
* @param LambdaHelper|null $helper For rendering strings in the current context.
* @return string
* @return string|self
*/
public function __invoke($text = null, LambdaHelper $helper = null)
{
if (func_num_args() === 0 || $this->action === null) {
return $this;
}

if ($helper) {
$text = (string)$helper->render($text);
}
Expand All @@ -97,6 +108,23 @@ public function __invoke($text = null, LambdaHelper $helper = null)
return $text;
}

/**
* Magic: Render the pending action when Mustache interpolates this helper.
*
* @return string
*/
public function __toString(): string
{
if ($this->action === null) {
return '';
}

$return = $this->{$this->action}($this->collection, '');
$this->reset();

return (string)$return;
}

/**
* Magic: Determine if a property is set and is not NULL.
*
Expand Down
65 changes: 65 additions & 0 deletions packages/admin/tests/Charcoal/Admin/Mustache/AssetsHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Charcoal\Tests\Admin\Mustache;

use Assetic\Asset\AssetCollection;
use Assetic\Asset\StringAsset;
use Assetic\AssetManager;
use Charcoal\Admin\Mustache\AssetsHelpers;
use Charcoal\Tests\AbstractTestCase;
use Mustache\Engine as MustacheEngine;

/**
*
*/
class AssetsHelpersTest extends AbstractTestCase
{
/**
* @var AssetsHelpers
*/
private $obj;

/**
* @var AssetManager
*/
private $assets;

/**
* @return void
*/
public function setUp(): void
{
$this->assets = new AssetManager();
$this->assets->set('css', new AssetCollection([
new StringAsset('.login { color: red; }'),
]));

$this->obj = new AssetsHelpers([
'assets' => $this->assets,
]);
}

/**
* @return void
*/
public function testDottedOutputWithMustache()
{
$mustache = new MustacheEngine([
'helpers' => $this->obj->toArray(),
'strict_callables' => true,
]);

$this->assertEquals(
'{{=<<<<% %>>>>=}}.login { color: red; }<<<<%={{ }}=%>>>>',
$mustache->render('{{& assets.output.css }}')
);
}

/**
* @return void
*/
public function testInvokeWithoutArgumentsReturnsSelf()
{
$this->assertSame($this->obj, ($this->obj)());
}
}
Loading