Skip to content

Commit 7add204

Browse files
authored
Merge pull request #154 from Chi-teck/sort-tag
[3.x] Add sort_namespaces filter and update sort tag
2 parents 74c2dc6 + 09c4b1f commit 7add204

File tree

50 files changed

+156
-102
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+156
-102
lines changed

src/Twig/TwigEnvironment.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,8 @@ public function __construct(FilesystemLoader $loader, array $options = []) {
2626

2727
$this->addFilter(new TwigFilter('pluralize', [Utils::class, 'pluralize']));
2828
$this->addFilter(new TwigFilter('camelize', [Utils::class, 'camelize']));
29-
30-
$article = static function (string $input): string {
31-
$first_char = \strtolower($input[0]);
32-
$article = \in_array($first_char, ['a', 'e', 'i', 'o', 'u']) ? 'an' : 'a';
33-
return $article . ' ' . $input;
34-
};
35-
$this->addFilter(new TwigFilter('article', $article));
29+
$this->addFilter(new TwigFilter('sort_namespaces', [self::class, 'sortNamespaces']));
30+
$this->addFilter(new TwigFilter('article', [self::class, 'article']));
3631

3732
$u2h = static fn (string $input): string => \str_replace('_', '-', $input);
3833
$this->addFilter(new TwigFilter('u2h', $u2h));
@@ -64,4 +59,23 @@ public function tokenize(Source $source): TokenStream {
6459
return parent::tokenize($source);
6560
}
6661

62+
/**
63+
* {@selfdoc}
64+
*/
65+
public static function sortNamespaces(string $input): string {
66+
$lines = \explode(\PHP_EOL, $input);
67+
$lines = \array_unique($lines);
68+
\sort($lines, \SORT_FLAG_CASE | \SORT_NATURAL);
69+
return \trim(\implode(\PHP_EOL, $lines)) . \PHP_EOL;
70+
}
71+
72+
/**
73+
* {@selfdoc}
74+
*/
75+
public static function article(string $input): string {
76+
$first_char = \strtolower($input[0]);
77+
$article = \in_array($first_char, ['a', 'e', 'i', 'o', 'u']) ? 'an' : 'a';
78+
return $article . ' ' . $input;
79+
}
80+
6781
}

src/Twig/TwigSortSetNode.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace DrupalCodeGenerator\Twig;
66

7+
use Twig\Attribute\YieldReady;
78
use Twig\Compiler;
89
use Twig\Node\Node;
910

1011
/**
1112
* A class that defines the compiler for 'sort' token.
1213
*/
14+
#[YieldReady]
1315
final class TwigSortSetNode extends Node {
1416

1517
/**
@@ -18,12 +20,13 @@ final class TwigSortSetNode extends Node {
1820
public function compile(Compiler $compiler): void {
1921
$compiler
2022
->addDebugInfo($this)
21-
->write("ob_start();\n")
22-
->subcompile($this->getNode('body'))
23-
->write('$data = explode("\n", ob_get_clean());' . "\n")
23+
->write('$data = ')
24+
->subcompile($this->getNode('ref'))
25+
->raw(";\n")
26+
->write('$data = explode("\n", $data);' . "\n")
2427
->write('$data = array_unique($data);' . "\n")
2528
->write('sort($data, SORT_FLAG_CASE|SORT_NATURAL);' . "\n")
26-
->write('echo ltrim(implode("\n", $data)) . "\n";' . "\n");
29+
->write('yield ltrim(implode("\n", $data)) . "\n";' . "\n");
2730
}
2831

2932
}

src/Twig/TwigSortTokenParser.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace DrupalCodeGenerator\Twig;
66

7+
use Twig\Node\Expression\TempNameExpression;
8+
use Twig\Node\Node;
9+
use Twig\Node\SetNode;
710
use Twig\Token;
811
use Twig\TokenParser\AbstractTokenParser;
912

@@ -15,16 +18,25 @@ final class TwigSortTokenParser extends AbstractTokenParser {
1518
/**
1619
* {@inheritdoc}
1720
*/
18-
public function parse(Token $token): TwigSortSetNode {
19-
21+
public function parse(Token $token): Node {
22+
\trigger_error('The sort tag is deprecated in 3.6.0 and will be removed in 4.x, use the sort_namespaces twig filter.', \E_USER_WARNING);
2023
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
2124
$body = $this->parser->subparse(
2225
static fn (Token $token): bool => $token->test('endsort'),
2326
TRUE,
2427
);
2528
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
2629

27-
return new TwigSortSetNode(['body' => $body], [], $token->getLine(), $this->getTag());
30+
$lineno = $token->getLine();
31+
$name = $this->parser->getVarName();
32+
33+
$ref = new TempNameExpression($name, $lineno);
34+
$ref->setAttribute('always_defined', TRUE);
35+
36+
return new Node([
37+
new SetNode(TRUE, $ref, $body, $lineno, $this->getTag()),
38+
new TwigSortSetNode(['ref' => $ref], [], $lineno, $this->getTag()),
39+
]);
2840
}
2941

3042
/**

templates/Drush/_symfony-command/command.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ declare(strict_types=1);
55
66
namespace Drupal\{{ machine_name }}\Command;
77
8-
{% sort %}
8+
{% apply sort_namespaces %}
99
use Symfony\Component\Console\Attribute\AsCommand;
1010
use Symfony\Component\Console\Command\Command;
1111
use Symfony\Component\Console\Input\InputInterface;
1212
use Symfony\Component\Console\Output\OutputInterface;
1313
{% if services %}
1414
{{ di.use(services) }}
1515
{% endif %}
16-
{% endsort %}
16+
{% endapply %}
1717
1818
// phpcs:disable Drupal.Commenting.ClassComment.Missing
1919
#[AsCommand(

templates/Entity/_configuration-entity/src/Entity/Example.php.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ declare(strict_types=1);
44
55
namespace Drupal\{{ machine_name }}\Entity;
66
7-
{% sort %}
7+
{% apply sort_namespaces %}
88
use Drupal\Core\Config\Entity\ConfigEntityBase;
99
use Drupal\{{ machine_name }}\{{ class_prefix }}Interface;
10-
{% endsort %}
10+
{% endapply %}
1111
1212
/**
1313
* Defines the {{ entity_type_label|lower }} entity type.

templates/Entity/_content-entity/model.module.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ declare(strict_types=1);
77
* Provides {{ entity_type_label|article|lower }} entity type.
88
*/
99
10-
{% sort %}
10+
{% apply sort_namespaces %}
1111
use Drupal\Core\Render\Element;
1212
{% if author_base_field %}
1313
use Drupal\user\UserInterface;
1414
{% endif %}
15-
{% endsort %}
15+
{% endapply %}
1616
1717
/**
1818
* Implements hook_theme().

templates/Entity/_content-entity/src/Entity/Example.php.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ declare(strict_types=1);
44
55
namespace Drupal\{{ machine_name }}\Entity;
66
7-
{% sort %}
7+
{% apply sort_namespaces %}
88
{% if not revisionable %}
99
use Drupal\Core\Entity\ContentEntityBase;
1010
{% endif %}
@@ -25,7 +25,7 @@ use Drupal\user\EntityOwnerTrait;
2525
{% if changed_base_field %}
2626
use Drupal\Core\Entity\EntityChangedTrait;
2727
{% endif %}
28-
{% endsort %}
28+
{% endapply %}
2929
3030
/**
3131
* Defines the {{ entity_type_label|lower }} entity class.

templates/Entity/_content-entity/src/Form/ExampleTypeForm.php.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ declare(strict_types=1);
44
55
namespace Drupal\{{ machine_name }}\Form;
66
7-
{% sort %}
7+
{% apply sort_namespaces %}
88
use Drupal\Core\Entity\BundleEntityFormBase;
99
use Drupal\Core\Entity\EntityTypeInterface;
1010
use Drupal\Core\Form\FormStateInterface;
1111
use Drupal\{{ machine_name }}\Entity\{{ class }}Type;
12-
{% endsort %}
12+
{% endapply %}
1313
1414
/**
1515
* Form handler for {{ entity_type_label|lower }} type forms.

templates/Plugin/Field/_widget/widget.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ declare(strict_types=1);
55
66
namespace Drupal\{{ machine_name }}\Plugin\Field\FieldWidget;
77
8-
{% sort %}
8+
{% apply sort_namespaces %}
99
use Drupal\Core\Field\FieldItemListInterface;
1010
use Drupal\Core\Field\WidgetBase;
1111
use Drupal\Core\Form\FormStateInterface;
@@ -14,7 +14,7 @@ use Drupal\Core\Form\FormStateInterface;
1414
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
1515
use Symfony\Component\DependencyInjection\ContainerInterface;
1616
{% endif %}
17-
{% endsort %}
17+
{% endapply %}
1818
1919
/**
2020
* Defines the '{{ plugin_id }}' field widget.

templates/Plugin/Migrate/_destination/destination.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ declare(strict_types=1);
55
66
namespace Drupal\{{ machine_name }}\Plugin\migrate\destination;
77
8-
{% sort %}
8+
{% apply sort_namespaces %}
99
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
1010
use Drupal\migrate\Plugin\MigrationInterface;
1111
use Drupal\migrate\Row;
@@ -14,7 +14,7 @@ use Drupal\migrate\Row;
1414
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
1515
use Symfony\Component\DependencyInjection\ContainerInterface;
1616
{% endif %}
17-
{% endsort %}
17+
{% endapply %}
1818
1919
/**
2020
* The '{{ plugin_id }}' destination plugin.

0 commit comments

Comments
 (0)