clean install

This commit is contained in:
2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 changed files with 3555714 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Twig;
use DrupalCodeGenerator\Utils;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Twig\Source;
use Twig\TokenStream;
use Twig\TwigFilter;
/**
* Stores the Twig configuration.
*/
final class TwigEnvironment extends Environment {
/**
* Constructs Twig environment object.
*/
public function __construct(FilesystemLoader $loader, array $options = []) {
parent::__construct($loader, $options);
$this->addTokenParser(new TwigSortTokenParser());
$this->addFilter(new TwigFilter('pluralize', [Utils::class, 'pluralize']));
$this->addFilter(new TwigFilter('camelize', [Utils::class, 'camelize']));
$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));
$h2u = static fn (string $input): string => \str_replace('-', '_', $input);
$this->addFilter(new TwigFilter('h2u', $h2u));
$this->addFilter(new TwigFilter('m2h', [Utils::class, 'machine2human']));
$this->addFilter(new TwigFilter('h2m', [Utils::class, 'human2machine']));
$this->addFilter(new TwigFilter('c2m', [Utils::class, 'camel2machine']));
$this->addGlobal('SUT_TEST', \getenv('SUT_TEST'));
}
/**
* {@inheritdoc}
*/
public function tokenize(Source $source): TokenStream {
// Remove leading whitespaces to preserve indentation.
// This has been resolved in Twig 2 but unfortunately neither PhpStorm nor
// Twig Code sniffer supports this yet.
// @see https://github.com/twigphp/Twig/issues/1423
$code = $source->getCode();
if (!\str_contains($code, '{% verbatim %}')) {
$code = \preg_replace("/\n +\{%/", "\n{%", $source->getCode());
}
// Twig source has no setters.
$source = new Source($code, $source->getName(), $source->getPath());
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;
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
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 {
/**
* {@inheritdoc}
*/
public function compile(Compiler $compiler): void {
$compiler
->addDebugInfo($this)
->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('yield ltrim(implode("\n", $data)) . "\n";' . "\n");
}
}

View File

@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace DrupalCodeGenerator\Twig;
use Twig\Node\Expression\TempNameExpression;
use Twig\Node\Node;
use Twig\Node\SetNode;
use Twig\Token;
use Twig\TokenParser\AbstractTokenParser;
/**
* A class that defines the Twig 'sort' token parser.
*/
final class TwigSortTokenParser extends AbstractTokenParser {
/**
* {@inheritdoc}
*/
public function parse(Token $token): Node {
\trigger_deprecation('chi-teck/drupal-code-generator', '3.6.0', 'The sort twig tag is deprecated and will be removed in 5.x, use the sort_namespaces twig filter.');
$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);
$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()),
]);
}
/**
* {@inheritdoc}
*/
public function getTag(): string {
return 'sort';
}
}