Skip to content

Commit

Permalink
Merge pull request #154 from Chi-teck/sort-tag
Browse files Browse the repository at this point in the history
[3.x] Add sort_namespaces filter and update sort tag
  • Loading branch information
Chi-teck authored May 17, 2024
2 parents 74c2dc6 + 09c4b1f commit 7add204
Show file tree
Hide file tree
Showing 50 changed files with 156 additions and 102 deletions.
28 changes: 21 additions & 7 deletions src/Twig/TwigEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ public function __construct(FilesystemLoader $loader, array $options = []) {

$this->addFilter(new TwigFilter('pluralize', [Utils::class, 'pluralize']));
$this->addFilter(new TwigFilter('camelize', [Utils::class, 'camelize']));

$article = static function (string $input): string {
$first_char = \strtolower($input[0]);
$article = \in_array($first_char, ['a', 'e', 'i', 'o', 'u']) ? 'an' : 'a';
return $article . ' ' . $input;
};
$this->addFilter(new TwigFilter('article', $article));
$this->addFilter(new TwigFilter('sort_namespaces', [self::class, 'sortNamespaces']));
$this->addFilter(new TwigFilter('article', [self::class, 'article']));

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

/**
* {@selfdoc}
*/
public static function sortNamespaces(string $input): string {
$lines = \explode(\PHP_EOL, $input);
$lines = \array_unique($lines);
\sort($lines, \SORT_FLAG_CASE | \SORT_NATURAL);
return \trim(\implode(\PHP_EOL, $lines)) . \PHP_EOL;
}

/**
* {@selfdoc}
*/
public static function article(string $input): string {
$first_char = \strtolower($input[0]);
$article = \in_array($first_char, ['a', 'e', 'i', 'o', 'u']) ? 'an' : 'a';
return $article . ' ' . $input;
}

}
11 changes: 7 additions & 4 deletions src/Twig/TwigSortSetNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace DrupalCodeGenerator\Twig;

use Twig\Attribute\YieldReady;
use Twig\Compiler;
use Twig\Node\Node;

/**
* A class that defines the compiler for 'sort' token.
*/
#[YieldReady]
final class TwigSortSetNode extends Node {

/**
Expand All @@ -18,12 +20,13 @@ final class TwigSortSetNode extends Node {
public function compile(Compiler $compiler): void {
$compiler
->addDebugInfo($this)
->write("ob_start();\n")
->subcompile($this->getNode('body'))
->write('$data = explode("\n", ob_get_clean());' . "\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")
->write('echo ltrim(implode("\n", $data)) . "\n";' . "\n");
->write('yield ltrim(implode("\n", $data)) . "\n";' . "\n");
}

}
18 changes: 15 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,25 @@ final class TwigSortTokenParser extends AbstractTokenParser {
/**
* {@inheritdoc}
*/
public function parse(Token $token): TwigSortSetNode {

public function parse(Token $token): Node {
\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);
$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: 2 additions & 2 deletions templates/Drush/_symfony-command/command.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Command;

{% sort %}
{% apply sort_namespaces %}
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
{% if services %}
{{ di.use(services) }}
{% endif %}
{% endsort %}
{% endapply %}

// phpcs:disable Drupal.Commenting.ClassComment.Missing
#[AsCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Entity;

{% sort %}
{% apply sort_namespaces %}
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\{{ machine_name }}\{{ class_prefix }}Interface;
{% endsort %}
{% endapply %}

/**
* Defines the {{ entity_type_label|lower }} entity type.
Expand Down
4 changes: 2 additions & 2 deletions templates/Entity/_content-entity/model.module.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ declare(strict_types=1);
* Provides {{ entity_type_label|article|lower }} entity type.
*/

{% sort %}
{% apply sort_namespaces %}
use Drupal\Core\Render\Element;
{% if author_base_field %}
use Drupal\user\UserInterface;
{% endif %}
{% endsort %}
{% endapply %}

/**
* Implements hook_theme().
Expand Down
4 changes: 2 additions & 2 deletions templates/Entity/_content-entity/src/Entity/Example.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Entity;

{% sort %}
{% apply sort_namespaces %}
{% if not revisionable %}
use Drupal\Core\Entity\ContentEntityBase;
{% endif %}
Expand All @@ -25,7 +25,7 @@ use Drupal\user\EntityOwnerTrait;
{% if changed_base_field %}
use Drupal\Core\Entity\EntityChangedTrait;
{% endif %}
{% endsort %}
{% endapply %}

/**
* Defines the {{ entity_type_label|lower }} entity class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Form;

{% sort %}
{% apply sort_namespaces %}
use Drupal\Core\Entity\BundleEntityFormBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\{{ machine_name }}\Entity\{{ class }}Type;
{% endsort %}
{% endapply %}

/**
* Form handler for {{ entity_type_label|lower }} type forms.
Expand Down
4 changes: 2 additions & 2 deletions templates/Plugin/Field/_widget/widget.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Plugin\Field\FieldWidget;

{% sort %}
{% apply sort_namespaces %}
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
Expand All @@ -14,7 +14,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endsort %}
{% endapply %}

/**
* Defines the '{{ plugin_id }}' field widget.
Expand Down
4 changes: 2 additions & 2 deletions templates/Plugin/Migrate/_destination/destination.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Plugin\migrate\destination;

{% sort %}
{% apply sort_namespaces %}
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
Expand All @@ -14,7 +14,7 @@ use Drupal\migrate\Row;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endsort %}
{% endapply %}

/**
* The '{{ plugin_id }}' destination plugin.
Expand Down
4 changes: 2 additions & 2 deletions templates/Plugin/Migrate/_process/process.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Plugin\migrate\process;

{% sort %}
{% apply sort_namespaces %}
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
Expand All @@ -14,7 +14,7 @@ use Drupal\migrate\Row;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endsort %}
{% endapply %}

/**
* Provides {{ plugin_id|article }} plugin.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Plugin\views\argument_default;

{% sort %}
{% apply sort_namespaces %}
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
{% if configurable %}
Expand All @@ -16,7 +16,7 @@ use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
{{ di.use(services) }}
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endsort %}
{% endapply %}

/**
* @todo Add plugin description here.
Expand Down
4 changes: 2 additions & 2 deletions templates/Plugin/Views/_field/field.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Plugin\views\field;

{% sort %}
{% apply sort_namespaces %}
use Drupal\Component\Render\MarkupInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
Expand All @@ -16,7 +16,7 @@ use Drupal\Core\Form\FormStateInterface;
{{ di.use(services) }}
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endsort %}
{% endapply %}

/**
* Provides {{ plugin_label }} field handler.
Expand Down
4 changes: 2 additions & 2 deletions templates/Plugin/_action/action.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Plugin\Action;

{% sort %}
{% apply sort_namespaces %}
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Entity\ContentEntityInterface;
{% if configurable %}
Expand All @@ -21,7 +21,7 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endsort %}
{% endapply %}

/**
* Provides {{ plugin_label|article }} action.
Expand Down
4 changes: 2 additions & 2 deletions templates/Plugin/_block/block.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Plugin\Block;

{% sort %}
{% apply sort_namespaces %}
{% if access %}
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
Expand All @@ -19,7 +19,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endsort %}
{% endapply %}

/**
* Provides {{ plugin_label|article|lower }} block.
Expand Down
4 changes: 2 additions & 2 deletions templates/Plugin/_condition/condition.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Plugin\Condition;

{% sort %}
{% apply sort_namespaces %}
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
{% if services %}
{{ di.use(services) }}
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endsort %}
{% endapply %}

/**
* Provides a '{{ plugin_label }}' condition.
Expand Down
4 changes: 2 additions & 2 deletions templates/Plugin/_constraint/validator.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Plugin\Validation\Constraint;

{% sort %}
{% apply sort_namespaces %}
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
{% if input_type == 'item' %}
Expand All @@ -20,7 +20,7 @@ use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endsort %}
{% endapply %}

/**
* Validates the {{ plugin_label }} constraint.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Plugin\EntityReferenceSelection;

{% sort %}
{% apply sort_namespaces %}
use Drupal\Core\Entity\Query\QueryInterface;
{% if configurable %}
use Drupal\Core\Form\FormStateInterface;
{% endif %}
use {{ base_class_full }}{% if base_class == class %} as Base{{ base_class }}{% endif %};
{% endsort %}
{% endapply %}

/**
* @todo Add plugin description here.
Expand Down
4 changes: 2 additions & 2 deletions templates/Plugin/_filter/filter.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare(strict_types=1);

namespace Drupal\{{ machine_name }}\Plugin\Filter;

{% sort %}
{% apply sort_namespaces %}
{% if configurable %}
use Drupal\Core\Form\FormStateInterface;
{% endif %}
Expand All @@ -16,7 +16,7 @@ use Drupal\filter\Plugin\FilterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
{% endif %}
{% endsort %}
{% endapply %}

/**
* @todo Add filter description here.
Expand Down
Loading

0 comments on commit 7add204

Please sign in to comment.