Commit Graph

495 Commits

Author SHA1 Message Date
HypeMC d56e8e2dba Support short-circuiting in null-safe operator chains 2026-02-06 22:36:57 +01:00
HypeMC a16ac6bd35 Fix null-safe operator test 2026-01-25 04:57:19 +01:00
Fabien Potencier 8a0f8acbdf Add support for object and mapping destructuring 2026-01-21 13:21:14 +01:00
Fabien Potencier bb99af3b39 Assignment operator array destructuring 2026-01-21 08:46:28 +01:00
Fabien Potencier bfbbef05f2 Add the = assignment operator 2026-01-19 16:39:57 +01:00
Felds Liscia 263c04fd1f Add null-safe operator 2026-01-17 14:57:47 +01:00
Fabien Potencier 94c8bdd6a6 Add === and !== operators 2026-01-14 09:58:48 +01:00
Fabien Potencier 2d741f9a16 bug #4731 Fix spread operator behavior (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Fix spread operator behavior

Closes #4637

Commits
-------

2376bbaf3e Fix spread operator behavior
2026-01-13 10:14:41 +01:00
Fabien Potencier 8391928896 Fix deprecations 2026-01-12 09:52:14 +01:00
Fabien Potencier 2376bbaf3e Fix spread operator behavior 2026-01-12 08:54:38 +01:00
Younes ENNAJI 473653d19b [Core] Fix cycle() with non-countable ArrayAccess+Traversable objects 2025-11-23 13:20:23 +01:00
Thomas Cochard 40248e4ca9 CoreExtension : Make error message more readable 2025-11-15 08:57:35 +01:00
Nicolas Grekas 9ae75d315c Fix accessing arrays with stringable objects as key 2025-10-24 14:19:16 +02:00
Simon André e4d7915702 Compile 'index' with repr (not string) in EmbedNode
Before this fix, the generated Template code had quotes around the
index (integer) parameter value.
2025-09-25 08:28:51 +02:00
Vincent Langlet cc12df995c Introduce registerUndefinedTestCallback 2025-09-16 07:40:27 +02:00
Vincent Langlet 30977bdea9 Support two words test guard 2025-09-15 07:51:41 +02:00
Nicolas Grekas 85a4817128 CS fixes 2025-07-29 10:07:07 +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
Fabien Potencier da64c42dc2 Add tests 2025-02-28 08:52:34 +01:00
Fabien Potencier b86fd0a059 Remove obsolete files 2025-02-26 22:13:11 +01:00
Fabien Potencier 56204e951a Move some tests 2025-02-26 22:10:10 +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 d5dc3eee99 Improve error reporting 2025-02-22 10:22:21 +01:00
Fabien Potencier d7702840da Remove $templateName from Template::loadTemplate() 2025-02-21 23:48:41 +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 2d84abfd08 Make the defined test implementation more generic 2025-02-21 08:36:21 +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 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 4b57f48a65 Use generics in ExpressionParsers 2025-02-14 08:38:53 +01:00
Fabien Potencier 8ff19090f3 Fix precedence rules 2025-02-14 08:38:53 +01:00
Fabien Potencier f67078b5c4 Move Operators to ExpressionParsers, deprecate ExpressionParser 2025-02-14 08:38:53 +01:00
Fabien Potencier 42c82e1e9b Extract operators logic from ExpressionParser to their own classes 2025-02-14 08:38:53 +01:00
Fabien Potencier df877a1d53 Introduce operator classes to describe operators provided by extensions instead of arrays 2025-02-14 08:38:53 +01:00
Christophe Coevoet 3794efe662 Fix support for ignoring syntax erros in an undefined handler in guard 2025-02-09 15:10:34 +01:00
Fabien Potencier fab1a37d8e bug #4546 Fix timezone conversion on strings (PrinsFrank)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Fix timezone conversion on strings

When setting a default application timezone, and a display timezone:
```php
date_default_timezone_set('UTC');
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone('Europe/Paris');
```

Date objects get converted, but strings don't. See #4545.

For most other paths in `convertDate`, the dateTime object is initialized, and afterwards the timezone gets set. For strings, the timezone is passed as a constructor argument before having the timezone set again, losing its converting behaviour. This makes the convertDate method work identical for DateTime objects as strings. This different behaviour was already spotted in #3568.

Fixes #4545 #3568 #2819

Commits
-------

f69531fa00 Fix timezone conversion on strings
2025-02-07 22:52:13 +01:00
PrinsFrank f69531fa00 Fix timezone conversion on strings 2025-02-07 22:52:10 +01:00
Fabien Potencier 7f080086dc Fix wrong array index 2025-02-01 16:22:20 +01:00
Fabien Potencier b0399992be Fix wrong array index 2025-02-01 10:33:30 +01:00
Fabien Potencier e48abfe47c Add even more tests 2025-01-31 19:03:58 +01:00
Fabien Potencier 3b605c4180 Add some tests on balanced brackets 2025-01-30 13:38:40 +01:00
Fabien Potencier 445f74cfe0 [SECURITY] Fix a security issue where escaping was missing when using ?? 2025-01-29 07:52:07 +01:00
Fabien Potencier ade8bce516 Deprecate Token::getType() 2025-01-24 15:48:04 +01:00
Fabien Potencier 609767522a Add ForElseNode 2025-01-24 15:25:30 +01:00
Nicolas Grekas 1d5180549f Ignore static properties when using the dot operator 2025-01-22 09:18:31 +01:00