-
-
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.
Improves compilation of custom functions declared within a component'…
…s template
- Loading branch information
1 parent
8d94b4a
commit b5fb64a
Showing
7 changed files
with
159 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
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
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,27 @@ | ||
<?php | ||
|
||
namespace Stillat\Dagger\Compiler\ComponentStages; | ||
|
||
use Closure; | ||
use PhpParser\NodeTraverser; | ||
use Stillat\Dagger\Parser\Visitors\FindFunctionsVisitor; | ||
use Stillat\Dagger\Parser\Visitors\RenameFunctionVisitor; | ||
|
||
class RewriteFunctions extends AbstractStage | ||
{ | ||
public function handle($ast, Closure $next) | ||
{ | ||
$traverser = new NodeTraverser; | ||
|
||
$finder = new FindFunctionsVisitor; | ||
$traverser->addVisitor($finder); | ||
$traverser->traverse($ast); | ||
|
||
$traverser->removeVisitor($finder); | ||
|
||
$modifyFunctionsVisitor = new RenameFunctionVisitor($finder->getFunctionNames()); | ||
$traverser->addVisitor($modifyFunctionsVisitor); | ||
|
||
return $next($traverser->traverse($ast)); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace Stillat\Dagger\Parser\Visitors; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeVisitorAbstract; | ||
|
||
class FindFunctionsVisitor extends NodeVisitorAbstract | ||
{ | ||
protected array $functionNames = []; | ||
|
||
public function enterNode(Node $node) | ||
{ | ||
if (! $node instanceof Node\Stmt\Function_) { | ||
return; | ||
} | ||
|
||
$this->functionNames[] = $node->name->toString(); | ||
} | ||
|
||
public function getFunctionNames(): array | ||
{ | ||
return $this->functionNames; | ||
} | ||
} |
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,70 @@ | ||
<?php | ||
|
||
namespace Stillat\Dagger\Parser\Visitors; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeVisitorAbstract; | ||
use Stillat\Dagger\Support\Utils; | ||
|
||
class RenameFunctionVisitor extends NodeVisitorAbstract | ||
{ | ||
protected array $functionNames = []; | ||
|
||
public function __construct(array $functionNames) | ||
{ | ||
$this->functionNames = $this->buildFunctionNameMap($functionNames); | ||
} | ||
|
||
protected function buildFunctionNameMap(array $functionNames): array | ||
{ | ||
return collect($functionNames) | ||
->mapWithKeys(function ($name) { | ||
return [$name => $name.'_'.Utils::makeRandomString()]; | ||
}) | ||
->all(); | ||
} | ||
|
||
public function enterNode(Node $node) | ||
{ | ||
if (! $node instanceof Node\Expr\FuncCall || ! $node->name instanceof Node\Name) { | ||
return; | ||
} | ||
|
||
$functionName = $node->name->toString(); | ||
|
||
if (! isset($this->functionNames[$functionName])) { | ||
return; | ||
} | ||
|
||
$node->name = new Node\Name($this->functionNames[$functionName]); | ||
} | ||
|
||
public function leaveNode(Node $node) | ||
{ | ||
if (! $node instanceof Node\Stmt\Function_) { | ||
return null; | ||
} | ||
|
||
$functionName = $node->name->toString(); | ||
|
||
if (! isset($this->functionNames[$functionName])) { | ||
return null; | ||
} | ||
|
||
$newFunctionName = $this->functionNames[$functionName]; | ||
|
||
$node->name = new Node\Identifier($newFunctionName); | ||
|
||
return new Node\Stmt\If_( | ||
new Node\Expr\BooleanNot( | ||
new Node\Expr\FuncCall( | ||
new Node\Name('function_exists'), | ||
[new Node\Arg(new Node\Scalar\String_($newFunctionName))] | ||
) | ||
), | ||
[ | ||
'stmts' => [$node], | ||
] | ||
); | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
use Stillat\Dagger\Tests\CompilerTestCase; | ||
|
||
uses(CompilerTestCase::class); | ||
|
||
test('custom functions in a component are compiled', function () { | ||
$template = <<<'BLADE' | ||
@for ($i = 0; $i < 5; $i++) | ||
<c-functions.declared title="The Title: {{ $i }}" /> | ||
@endfor | ||
BLADE; | ||
|
||
$expected = <<<'EXPECTED' | ||
THE TITLE: 0THE TITLE: 1THE TITLE: 2THE TITLE: 3THE TITLE: 4 | ||
EXPECTED; | ||
|
||
$this->assertSame( | ||
$expected, | ||
$this->render($template) | ||
); | ||
}); |
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,9 @@ | ||
<?php | ||
\Stillat\Dagger\component()->props(['title'])->trimOutput(); | ||
function toUpper($value) { | ||
return mb_strtoupper($value); | ||
} | ||
?> | ||
|
||
{{ toUpper($title) }} |