Skip to content

Commit

Permalink
Add sort_namespaces twig filter and improve sort tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
chesn0k committed May 14, 2024
1 parent c4a48de commit 89a6b47
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/Twig/TwigEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public function __construct(FilesystemLoader $loader, array $options = []) {
$this->addFilter(new TwigFilter('pluralize', [Utils::class, 'pluralize']));
$this->addFilter(new TwigFilter('camelize', [Utils::class, 'camelize']));

$sort_namespaces = static function (string $input): string {

This comment has been minimized.

Copy link
@Chi-teck

Chi-teck May 14, 2024

Owner

I propose we move this code to a static method of this class.

$lines = \explode(\PHP_EOL, $input);
\sort($lines);
return \trim(\implode(\PHP_EOL, $lines)) . \PHP_EOL;
};
$this->addFilter(new TwigFilter('sort_namespaces', $sort_namespaces));

$article = static function (string $input): string {
$first_char = \strtolower($input[0]);
$article = \in_array($first_char, ['a', 'e', 'i', 'o', 'u']) ? 'an' : 'a';
Expand Down
7 changes: 3 additions & 4 deletions src/Twig/TwigSortSetNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ final class TwigSortSetNode extends Node {
public function compile(Compiler $compiler): void {
$compiler
->addDebugInfo($this)
->write('$getData = function() use (&$context, $blocks, $macros) {')
->subcompile($this->getNode('body'))
->write('};'. "\n")
->write('$data = implode("", iterator_to_array($getData(), false));' . "\n")
->write('$data = ')
->subcompile($this->getNode('ref'))
->raw(";\n")
->write('$data = explode("\n", $data);' . "\n")
->write('$data = array_unique($data);' . "\n")
->write('sort($data, SORT_FLAG_CASE|SORT_NATURAL);' . "\n")
Expand Down
17 changes: 14 additions & 3 deletions src/Twig/TwigSortTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace DrupalCodeGenerator\Twig;

use Twig\Node\Expression\TempNameExpression;
use Twig\Node\Node;
use Twig\Node\SetNode;
use Twig\Token;
use Twig\TokenParser\AbstractTokenParser;

Expand All @@ -15,16 +18,24 @@ final class TwigSortTokenParser extends AbstractTokenParser {
/**
* {@inheritdoc}
*/
public function parse(Token $token): TwigSortSetNode {

public function parse(Token $token): Node {
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(
static fn (Token $token): bool => $token->test('endsort'),
TRUE,
);
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);

return new TwigSortSetNode(['body' => $body], [], $token->getLine(), $this->getTag());
$lineno = $token->getLine();
$name = $this->parser->getVarName();

$ref = new TempNameExpression($name, $lineno);
$ref->setAttribute('always_defined', TRUE);

return new Node([
new SetNode(TRUE, $ref, $body, $lineno, $this->getTag()),
new TwigSortSetNode(['ref' => $ref], [], $lineno, $this->getTag()),
]);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/_twig_environment_fixture.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ ThisShouldBeInCamelCase
Alpha
Beta
Gamma
-= Sort namespaces filter =-
Alpha
Beta
Gamma
6 changes: 6 additions & 0 deletions tests/unit/twig-environment-template.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ Gamma
Alpha
Beta
{% endsort %}
-= Sort namespaces filter =-
{% apply sort_namespaces %}
Gamma
Alpha
Beta
{% endapply %}

0 comments on commit 89a6b47

Please sign in to comment.