Use interfaces instead of abstract class for Twig extensions.

This commit is contained in:
flash 2025-04-01 12:36:15 +00:00
parent ea549dd0eb
commit 6efd02465f
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E
4 changed files with 84 additions and 15 deletions

View file

@ -1 +1 @@
0.2503.260138
0.2504.11235

22
composer.lock generated
View file

@ -976,16 +976,16 @@
},
{
"name": "phpstan/phpstan",
"version": "2.1.8",
"version": "2.1.11",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpstan.git",
"reference": "f9adff3b87c03b12cc7e46a30a524648e497758f"
"reference": "8ca5f79a8f63c49b2359065832a654e1ec70ac30"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/f9adff3b87c03b12cc7e46a30a524648e497758f",
"reference": "f9adff3b87c03b12cc7e46a30a524648e497758f",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/8ca5f79a8f63c49b2359065832a654e1ec70ac30",
"reference": "8ca5f79a8f63c49b2359065832a654e1ec70ac30",
"shasum": ""
},
"require": {
@ -1030,7 +1030,7 @@
"type": "github"
}
],
"time": "2025-03-09T09:30:48+00:00"
"time": "2025-03-24T13:45:00+00:00"
},
{
"name": "phpunit/php-code-coverage",
@ -1356,16 +1356,16 @@
},
{
"name": "phpunit/phpunit",
"version": "12.0.9",
"version": "12.0.10",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "7835bb4276780e0bbb385ce0a777b839e03096db"
"reference": "6075843014de23bcd6992842d69ca99d25d6a433"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/7835bb4276780e0bbb385ce0a777b839e03096db",
"reference": "7835bb4276780e0bbb385ce0a777b839e03096db",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/6075843014de23bcd6992842d69ca99d25d6a433",
"reference": "6075843014de23bcd6992842d69ca99d25d6a433",
"shasum": ""
},
"require": {
@ -1433,7 +1433,7 @@
"support": {
"issues": "https://github.com/sebastianbergmann/phpunit/issues",
"security": "https://github.com/sebastianbergmann/phpunit/security/policy",
"source": "https://github.com/sebastianbergmann/phpunit/tree/12.0.9"
"source": "https://github.com/sebastianbergmann/phpunit/tree/12.0.10"
},
"funding": [
{
@ -1449,7 +1449,7 @@
"type": "tidelift"
}
],
"time": "2025-03-19T13:47:33+00:00"
"time": "2025-03-23T16:03:59+00:00"
},
{
"name": "sebastian/cli-parser",

View file

@ -0,0 +1,65 @@
<?php
// TplExtensionCommon.php
// Created: 2025-04-01
// Updated: 2025-04-01
namespace Index\Templating\Extension;
use ReflectionClass;
use Twig\Node\Expression\Binary\AbstractBinary;
use Twig\Node\Expression\Unary\AbstractUnary;
use Twig\NodeVisitor\NodeVisitorInterface;
use Twig\TokenParser\TokenParserInterface;
use Twig\{ExpressionParser,OperatorPrecedenceChange,TwigFilter,TwigFunction,TwigTest};
/**
* Provides common implementations of the Twig extension methods so you can yoink them without having to extend AbstractExtension.
*/
trait TplExtensionCommon {
/** @return TokenParserInterface[] */
public function getTokenParsers() {
return [];
}
/** @return NodeVisitorInterface[] */
public function getNodeVisitors() {
return [];
}
/** @return TwigFilter[] */
public function getFilters() {
return [];
}
/** @return TwigTest[] */
public function getTests() {
return [];
}
/** @return TwigFunction[] */
public function getFunctions() {
return [];
}
/**
* @return array{
* array<string, array{precedence: int, precedence_change?: OperatorPrecedenceChange, class: class-string<AbstractUnary>}>,
* array<string, array{precedence: int, precedence_change?: OperatorPrecedenceChange, class?: class-string<AbstractBinary>, associativity: ExpressionParser::OPERATOR_*}>
* }
*/
public function getOperators() {
return [[], []];
}
public function getLastModified(): int {
$path = (new ReflectionClass($this))->getFileName();
if($path === false || !is_file($path))
return 0;
$time = filemtime($path);
if($time === false)
return 0;
return $time;
}
}

View file

@ -1,24 +1,28 @@
<?php
// TplIndexExtension.php
// Created: 2024-08-04
// Updated: 2024-10-04
// Updated: 2025-04-01
namespace Index\Templating\Extension;
use Index\{ByteFormat,Index};
use Twig\{Environment,TwigFilter,TwigFunction};
use Twig\Extension\AbstractExtension;
use Twig\Extension\LastModifiedExtensionInterface;
/**
* Provides version functions and additional functionality implemented in Index.
*/
class TplIndexExtension extends AbstractExtension {
class TplIndexExtension implements LastModifiedExtensionInterface {
use TplExtensionCommon;
#[\Override]
public function getFilters() {
return [
new TwigFilter('format_filesize', ByteFormat::format(...)),
];
}
#[\Override]
public function getFunctions() {
return [
new TwigFunction('ndx_version', Index::version(...)),