Commit Graph

1380 Commits

Author SHA1 Message Date
Fabien Potencier 32f88a2368 feature #4689 Support two words test guard (VincentLanglet)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Support two words test guard

Extracted from https://github.com/twigphp/Twig/pull/4687

Commits
-------

30977bdea9 Support two words test guard
2025-09-15 07:51:44 +02:00
Vincent Langlet 30977bdea9 Support two words test guard 2025-09-15 07:51:41 +02:00
Fabien Potencier 57a2f85783 Bump version 2025-09-15 07:48:29 +02:00
Christophe Coevoet 62747cee27 Avoid errors when failing to guess the template info for an error 2025-09-02 14:21:58 +02:00
Fabien Potencier 806049f7a2 Fix compatibility layer 2025-08-17 19:23:48 +02:00
Kevin van Sonsbeek 8c78952757 #4677: Add use statements for classes referenced in the getOperators @psalm-return doc 2025-08-04 21:13:52 +02:00
Nicolas Grekas fec73458dc minor #4664 CS fixes (nicolas-grekas)
This PR was merged into the 3.x branch.

Discussion
----------

CS fixes

Commits
-------

85a4817128 CS fixes
2025-07-29 10:10:06 +02:00
Nicolas Grekas 85a4817128 CS fixes 2025-07-29 10:07:07 +02:00
Gregor Harlan 823f50297b Escaper performance: avoid static variables 2025-07-12 15:47:09 +02:00
Fabien Potencier 12a40e714c Bump version 2025-05-03 09:23:07 +02:00
Fabien Potencier 285123877d Prepare the 3.21.1 release 2025-05-03 09:21:55 +02:00
Vincent Langlet e14a2474fb Fix warning 2025-05-02 17:41:32 +02:00
Vincent Langlet 00d29e6657 Fix instantiation 2025-05-02 17:39:47 +02:00
Fabien Potencier 06eb9eafe2 Bump version 2025-05-02 15:19:55 +02:00
Fabien Potencier b54a265712 Prepare the 3.21.0 release 2025-05-02 15:18:20 +02:00
Fabien Potencier 8a7d912deb feature #3916 Create attributes AsTwigFilter, AsTwigFunction and AsTwigTest to ease extension development (GromNaN)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Create attributes `AsTwigFilter`, `AsTwigFunction` and `AsTwigTest` to ease extension development

One drawback to writing extensions at present is that the declaration of functions/filters/tests is not directly adjacent to the methods. It's worse for runtime extensions because they need to be in 2 different classes. See [`SerializerExtension`](https://github.com/symfony/symfony/blob/7.0/src/Symfony/Bridge/Twig/Extension/SerializerExtension.php) and [`SerializerRuntime`](https://github.com/symfony/symfony/blob/7.0/src/Symfony/Bridge/Twig/Extension/SerializerRuntime.php) as an example.

By using attributes for filters, functions and tests definition, we can make writing extensions more expressive, and use reflection to detect particular options (`needs_environment`, `needs_context`, `is_variadic`).

Example if we implemented the `formatDate` filter: https://github.com/twigphp/Twig/blob/aeeec9a5e907a79e50a6bb78979154599401726e/extra/intl-extra/IntlExtension.php#L392-L395

By using the `AsTwigFilter` attribute, it is not necessary to create the `getFilters()` method. The `needs_environment` option is detected from method signature. The name is still required as the method naming convention (camelCase) doesn't match with Twig naming convention (snake_case).

```php
use Twig\Extension\Attribute\AsTwigFilter;

class IntlExtension
{
    #[AsTwigFilter(name: 'format_date')]
    public function formatDate(Environment $env, $date, ?string $dateFormat = 'medium', string $pattern = '', $timezone = null, string $calendar = 'gregorian', string $locale = null): string
    {
        return $this->formatDateTime($env, $date, $dateFormat, 'none', $pattern, $timezone, $calendar, $locale);
    }
}
```

This approach does not totally replace the current definition of extensions, which is still necessary for advanced needs. It does, however, make for more pleasant reading and writing.

This makes writing lazy-loaded runtime extension the easiest way to create Twig extension in Symfony: https://github.com/symfony/symfony/pull/52748

Related to https://github.com/symfony/symfony/issues/50016

Is there any need to cache the parsing of method attributes? They are only read at compile time, but that can have a performance impact during development or when using dynamic templates.

Commits
-------

5886907b28 Create attributes `AsTwigFilter`, `AsTwigFunction` and `AsTwigTest` to ease extension development
2025-03-24 22:34:23 +01:00
Jérôme Tamarelle 5886907b28 Create attributes AsTwigFilter, AsTwigFunction and AsTwigTest to ease extension development 2025-03-24 22:34:21 +01:00
Fabien Potencier 09f9a22310 Fix wrong array index (again) 2025-03-21 09:42:17 +01:00
Christian Flothmann e31a6e4534 use EmptyNode instead of an Nodes instance without children 2025-02-28 22:12:08 +01:00
Alexandre Daubois 81c1fcc7d6 Fix ModuleNode instanciation when $embeddedTemplates is null 2025-02-28 20:12:40 +01:00
Christian Flothmann 155d0faab4 reduce the number of deprecations being triggered
Triggering deprecations for specific arguments is not needed as the method
itself is deprecated as well.
2025-02-28 14:36:39 +01:00
Fabien Potencier 76b86aa9da Add missing @deprecated tag 2025-02-28 09:01:15 +01:00
Fabien Potencier c0db761477 minor #4604 Avoid polluting ModuleNode::toString() with embedded templates (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Avoid polluting ModuleNode::toString() with embedded templates

Commits
-------

3d760aec1c Avoid polluting ModuleNode::toString() with embedded templates
2025-02-28 08:58:45 +01:00
Fabien Potencier 3d760aec1c Avoid polluting ModuleNode::toString() with embedded templates 2025-02-28 08:57:55 +01:00
Fabien Potencier 719bba9d1e Simplify code 2025-02-28 08:54:29 +01:00
Christian Flothmann 53facb74c1 remove not needed code 2025-02-27 09:49:34 +01:00
Fabien Potencier 4e1fa3aa43 Remove obsolete comment 2025-02-26 22:19:38 +01:00
Fabien Potencier 7b43bd0fd7 Fix Error when the trace has Twig file/line information instead of the original PHP info 2025-02-26 21:02:58 +01:00
Fabien Potencier 4a121d90c6 Sync Error file and line 2025-02-26 13:26:24 +01:00
Fabien Potencier 7cde13f6c9 bug #4595 Improve error reporting (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Improve error reporting

Commits
-------

d5dc3eee99 Improve error reporting
2025-02-22 18:27:58 +01:00
Fabien Potencier ad203c774d bug #4583 Remove template source from Template::loadTemplate() (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Remove template source from Template::loadTemplate()

Closes #3266

The first commit supports string and Source. But I think we don't even need to pass anything (that's the other commits).

Commits
-------

d7702840da Remove $templateName from Template::loadTemplate()
2025-02-22 10:43:11 +01:00
Fabien Potencier d5dc3eee99 Improve error reporting 2025-02-22 10:22:21 +01:00
Fabien Potencier d13d31bb68 minor #4594 Simplify Error (smnandre)
This PR was merged into the 3.x branch.

Discussion
----------

Simplify Error

Follows #4593

Commits
-------

6a651ccfdc Simplify Error
2025-02-22 10:02:33 +01:00
Simon André 6a651ccfdc Simplify Error 2025-02-22 04:07:17 +01:00
Fabien Potencier 68481a2a3a Simplify code 2025-02-22 00:03:13 +01:00
Fabien Potencier d7702840da Remove $templateName from Template::loadTemplate() 2025-02-21 23:48:41 +01:00
Fabien Potencier 1c6ac163c9 minor #4589 Make in_array() calls strict (alexandre-daubois)
This PR was merged into the 3.x branch.

Discussion
----------

Make `in_array()` calls strict

I propose to add `strict: true` to all calls to `in_array()`, making the calls faster.

Commits
-------

9c6b95f2a6 Make `in_array()` calls strict
2025-02-21 19:06:58 +01:00
Fabien Potencier d72c1c6b1e Fix testing and expression when it evaluates to an instance of Markup 2025-02-21 18:29:32 +01:00
Fabien Potencier 9f275f6ac6 feature #4590 Make the defined test implementation more generic (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Make the defined test implementation more generic

Commits
-------

2d84abfd08 Make the defined test implementation more generic
2025-02-21 18:22:44 +01:00
Fabien Potencier 9b4db2e931 minor #4592 Simplify Error implementation (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Simplify Error implementation

Commits
-------

d10bcadd75 Simplify Error implementation
2025-02-21 15:39:18 +01:00
Fabien Potencier d10bcadd75 Simplify Error implementation 2025-02-21 11:34:30 +01:00
Fabien Potencier 2d84abfd08 Make the defined test implementation more generic 2025-02-21 08:36:21 +01:00
Alexandre Daubois 9c6b95f2a6 Make in_array() calls strict 2025-02-19 15:29:33 +01:00
Christian Flothmann 5f6c67f838 re-add mixed return type 2025-02-14 13:47:07 +01:00
Fabien Potencier 151aa4ecda Fix CS 2025-02-14 12:57:03 +01:00
Fabien Potencier 3964aeba78 Add a proper prefix spread operator 2025-02-14 11:45:39 +01:00
Fabien Potencier d6f539bb01 Avoid storing expression parser instances in Node attributes 2025-02-14 11:32:12 +01:00
Fabien Potencier 79d828b09d Fix version 2025-02-14 08:47:54 +01:00
Fabien Potencier 251c0b5da9 Fix CS 2025-02-14 08:45:28 +01:00
Fabien Potencier 561c6629e1 Remove obsolete code 2025-02-14 08:38:53 +01:00