Commit Graph

6553 Commits

Author SHA1 Message Date
Fabien Potencier f4bca4fa44 Remove obsolete comment 2024-08-20 18:06:11 +02:00
Fabien Potencier 5918aa939f feature #4212 ChainLoader constructor should accept iterable instead of array (TheCelavi)
This PR was merged into the 3.x branch.

Discussion
----------

ChainLoader constructor should accept iterable instead of array

Closes #4200

Commits
-------

6ef13d1e Resolves #4200
2024-08-20 18:00:03 +02:00
Nikola Svitlica a.k.a TheCelavi 6ef13d1e0b Resolves #4200 2024-08-20 17:30:37 +02:00
Fabien Potencier b9b8d52572 minor #4217 Refactor code (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Refactor code

Commits
-------

31037d0e Refactor code
2024-08-20 17:20:15 +02:00
Fabien Potencier 31037d0e51 Refactor code 2024-08-20 15:39:54 +02:00
Fabien Potencier 63e35b9620 Add a test 2024-08-20 09:34:22 +02:00
Fabien Potencier 3a307cd41d Update CHANGELOG and docs 2024-08-19 20:06:41 +02:00
Fabien Potencier 80c15749f9 feature #4216 Swap BC layer for yield-ready and reclaim perf loss (nicolas-grekas)
This PR was merged into the 3.x branch.

Discussion
----------

Swap BC layer for yield-ready and reclaim perf loss

Follows #3999

Fix #4146
Fix #4103

When `use_yield` is set to false (the default), this PR reverts the implementation of the `render()` method to use a wrapping output buffer instead of hooking between each steps of generators. In this mode, the behavior of the yield method is not "pure": it triggers a mix of yield and echo. But this is fine for render and display methods.

When `use_yield` is set to `true`, we skip that wrapping output buffer. This makes twig compatible with fibers (and this also makes compilation fail if a non-YieldReady extension is found.)

That makes the name of the option not ideal, but BC rulez FTW.

Commits
-------

5d1a19a8 Swap BC layer for yield-ready and reclaim perf loss
2024-08-19 20:02:33 +02:00
Nicolas Grekas 5d1a19a80c Swap BC layer for yield-ready and reclaim perf loss 2024-08-19 17:31:12 +02:00
Fabien Potencier 17997cf7fc minor #4214 Add test about Environment version constants. (VincentLanglet)
This PR was merged into the 3.x branch.

Discussion
----------

Add test about Environment version constants.

Hi `@fabpot`

The Environment Version constants seems to be manually updated.
https://github.com/twigphp/Twig/blob/fe36e084b4e208c44e30886053c8594bb99bd78f/src/Environment.php#L46-L51

A mistake was made on the 3.11 version, the MAJOR_VERSION was set to 4 instead of 3
https://github.com/twigphp/Twig/blob/e80fb8ebba85c7341a97a9ebf825d7fd4b77708d/src/Environment.php#L48

It was fixed in another version, but I think it could be useful to have some test about the consistency of all those constants.
Such test would have fail for the 3.11 version and avoid the mistake.

Commits
-------

254abc8c Add test about version
2024-08-19 08:48:36 +02:00
Fabien Potencier 675cb2d141 Fix CS 2024-08-18 19:25:19 +02:00
Vincent Langlet 254abc8c4c Add test about version 2024-08-18 18:25:33 +02:00
Fabien Potencier fe36e084b4 feature #4209 Accept colons instead of equals for named arguments (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Accept colons instead of equals for named arguments

Refs #3475
Refs #3635

Commits
-------

84116e5f Fix typos in CHANGELOG
2024-08-18 17:53:41 +02:00
Fabien Potencier 84116e5ff7 Fix typos in CHANGELOG 2024-08-18 17:52:16 +02:00
Fabien Potencier 1d5a8dc98b Fix html_cva docs 2024-08-18 09:38:48 +02:00
Fabien Potencier e85519fcd9 Fix html_cva docs 2024-08-18 09:38:02 +02:00
Fabien Potencier 0bfef5b300 feature #4006 Introduce CVA to html-extra (WebMamba)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Introduce CVA to html-extra

Hey! This PR introduces CVA to Twig. All of this has already been merged into SymfonyUX (https://github.com/symfony/ux/pull/1416), but `@kbond` suggested that this repo can be a better place for this feature.

Here is a description from the PR merged in to SymfonyUX:

------------------------------

This PR introduces a new concept CVA (Class Variance Authority), by adding a1 twig function, to help you manage your class in your component.

Let's take an example an Alert component. In your app, an alert can have a lot of different styles one for success, one for alert, one for warning, and different sizes, with icons or not... You need something that lets you completely change the style of your component without creating a new component, and without creating too much complexity in your template.

Here is the reason came CVA.

Your Alert component can now look like this:

```twig
{% props color = 'blue', size = 'md' %}

{% set alert =  html_cva(
      'alert rounded-lg',
     {
        color: {
            blue: 'text-blue-800 bg-blue-50 dark:bg-gray-800 dark:text-blue-400',
            red: 'text-red-800 bg-red-50 dark:bg-gray-800 dark:text-red-400',
            green: 'text-green-800 bg-green-50 dark:bg-gray-800 dark:text-green-400',
            yellow: 'text-yellow-800 bg-yellow-50 dark:bg-gray-800 dark:text-yellow-400',
        },
        size: {
            sm: 'px-4 py-3 text-sm',
            md: 'px-6 py-4 text-base',
            lg: 'px-8 py-5 text-lg',
        }
    },
    [{
           color: ['red'],
           size: ['lg'],
          class: 'font-semibold'
    }],
   {
           rounded: 'md'
    }
}) %}

<div class="{{ cva.apply({color, size}, attribute.render('class'), 'flex p-4') }}">
    ...
</div>
```

So here you have a `cva` function that lets you define different variants of your component.

You can now use your component like this:
```twig
<twig:Alert color="red" size="md"/>

<twig:Alert color="green" size="sm"/>

<twig:Alert color="yellow" size="lg"/>

<twig:Alert color="red" size="md" class="dark:bg-gray-800"/>
```

And then you get the following result:

<img width="1269" alt="Capture d’écran 2024-01-24 à 00 52 33" src="https://github.com/symfony/ux/assets/32077734/6a5e25be-5b81-4ae7-8385-0fa5422d0396">

If you want to know more about the concept I implement here you can look at:
- CVA (js version): https://cva.style/docs
- tailwind merge: https://github.com/gehrisandro/tailwind-merge-php, https://github.com/dcastil/tailwind-merge
- this implementation by using tailwind-merge and cva is inspired a lot by: https://ui.shadcn.com/ (shadcn is the most starred library on github in 2023)
- a really good article that explains the philosophy behind https://manupa.dev/blog/anatomy-of-shadcn-ui
- this PR works great in a LASTstack: https://symfonycasts.com/screencast/last-stack/last-stack

Tell me what you think about it! Thanks for your time! Cheers 🧡

------------

Commits
-------

e3ac14e1 Introduce CVA to html-extra
2024-08-18 09:36:20 +02:00
Matheo Daninos e3ac14e1c8 Introduce CVA to html-extra 2024-08-18 09:36:16 +02:00
Fabien Potencier 8370e8a9a7 Move some methods to static calls 2024-08-17 23:15:53 +02:00
Fabien Potencier 003b171ea6 feature #4210 Special functions extension point (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Special functions extension point

The `parent`, `attribute`, `block` (and `loop` in 4.0) functions are handled directly in ExpressionParser as they are converted to specific Nodes.

It means that recursive loops can only be implemented in core. By adding a new extension point, we are removing the hardcoded handling of such functions and we open up the possibility for user-land functions to do the same.

*Review is easier when hiding whitespace.*

Commits
-------

44280790 Add an extension point for parsing special functions
2024-08-17 23:05:16 +02:00
Fabien Potencier 4428079003 Add an extension point for parsing special functions 2024-08-17 18:06:14 +02:00
Fabien Potencier ff55738f19 Fix typos in CHANGELOG 2024-08-17 17:59:11 +02:00
Fabien Potencier dbc934ba78 Fix typo 2024-08-17 11:28:14 +02:00
Fabien Potencier 2e43eaa4ad Fix some minor issues 2024-08-16 22:51:19 +02:00
Fabien Potencier 5a66385a3f feature #4173 Add support for named arguments on special functions (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Add support for named arguments on special functions

Commits
-------

0c30e78b Add support for named arguments on special functions
2024-08-16 22:50:11 +02:00
Fabien Potencier 0c30e78b1c Add support for named arguments on special functions 2024-08-16 08:37:24 +02:00
Fabien Potencier 26bcadeaeb Fix missing code 2024-08-16 08:13:22 +02:00
Fabien Potencier 5c52e2d361 feature #4207 Throw a SyntaxError exception at compile time when a Twig callable has not the minimum number of required arguments (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Throw a SyntaxError exception at compile time when a Twig callable has not the minimum number of required arguments

When using named arguments, a compilation error is thrown when some required arguments are missing.
But when not using arguments, the error was thrown at runtime. This PR fixed this.

Commits
-------

0823d234 Throw a SyntaxError exception at compile time when a Twig callable has not the minimum number of required arguments
2024-08-16 08:07:54 +02:00
Fabien Potencier 0823d23488 Throw a SyntaxError exception at compile time when a Twig callable has not the minimum number of required arguments 2024-08-15 23:00:02 +02:00
Fabien Potencier 55733a2da1 feature #4206 Extract a new CallableArgumentsExtractor class (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Extract a new CallableArgumentsExtractor class

Commits
-------

e1705f88 Extract a new CallableArgumentsExtractor class
2024-08-15 20:55:15 +02:00
Fabien Potencier e1705f8831 Extract a new CallableArgumentsExtractor class 2024-08-15 20:37:59 +02:00
Fabien Potencier fa1220951f minor #4205 Make Node::__toString() more readable (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Make Node::__toString() more readable

Commits
-------

b431ecad Make Node::__toString() more readable
2024-08-15 20:27:24 +02:00
Fabien Potencier 3c7469ec09 minor #4204 Remove remaining const optimizations (smnandre)
This PR was merged into the 3.x branch.

Discussion
----------

Remove remaining const optimizations

After #4198, some Token:: constant optimizations remained.

This PR removes them.. **in case** this was not intended

Commits
-------

c662e0ce Remove remaining const optimizations
2024-08-15 20:22:54 +02:00
Fabien Potencier b431ecad3a Make Node::__toString() more readable 2024-08-15 20:18:30 +02:00
Simon André c662e0ce27 Remove remaining const optimizations
After #4198 some `Token::` const optimization remains.

This PR remove them.. **in case** it was not intended
2024-08-15 19:11:55 +02:00
Fabien Potencier 72d6d9c9c5 bug #4203 Fix RawFilter (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Fix RawFilter

Commits
-------

397b7c47 Fix RawFilter
2024-08-14 23:43:27 +02:00
Fabien Potencier 397b7c4782 Fix RawFilter 2024-08-14 23:42:25 +02:00
Fabien Potencier fb96eed2c6 feature #4184 Move FunctionExpression/FilterExpression/TestExpression attributes from compilation time to parsing time (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Move FunctionExpression/FilterExpression/TestExpression attributes from compilation time to parsing time

Closes #3895

Opened early to gather feedback.

Commits
-------

3156d809 Move FunctionExpression/FilterExpression/TestExpression attributes from compilation time to parsing time
2024-08-14 22:55:12 +02:00
Fabien Potencier 3156d8093e Move FunctionExpression/FilterExpression/TestExpression attributes from compilation time to parsing time 2024-08-14 18:45:35 +02:00
Fabien Potencier 85a5b7c2d1 minor #4202 Cleanup the implementation of the defined test for constants (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Cleanup the implementation of the defined test for constants

Let's avoid modifying the `TwigFunction`.

Commits
-------

6ecd1f13 Cleanup the implementation of the defined test for constants
2024-08-13 22:43:21 +02:00
Fabien Potencier 6ecd1f1350 Cleanup the implementation of the defined test for constants 2024-08-13 22:40:15 +02:00
Fabien Potencier d3ab783595 Add missing method in TwigCallableInterface 2024-08-13 11:28:14 +02:00
Fabien Potencier 21df1ad782 Fix TwigCallableInterface 2024-08-12 11:03:48 +02:00
Fabien Potencier 1da763408b Simplify code 2024-08-12 10:49:48 +02:00
Fabien Potencier 66ace94b06 feature #4194 Make various optimizations for dynamic Twig callables (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Make various optimizations for dynamic Twig callables

Commits
-------

148d3e07 Make various optimization for dynamic Twig callables
2024-08-12 10:37:09 +02:00
Fabien Potencier 148d3e079d Make various optimization for dynamic Twig callables 2024-08-12 09:45:32 +02:00
Fabien Potencier 2fa8a24e0b minor #4198 Remove const optimization (fabpot)
This PR was merged into the 3.x branch.

Discussion
----------

Remove const optimization

Closes #4197

Commits
-------

cd0ac3cc Remove const optimization
2024-08-12 09:42:24 +02:00
Fabien Potencier cd0ac3cc0a Remove const optimization 2024-08-12 09:40:52 +02:00
Fabien Potencier 93e66ad141 minor #4196 Optimize stripcslashes (ruudk)
This PR was squashed before being merged into the 3.x branch.

Discussion
----------

Optimize stripcslashes

See https://github.com/twigphp/Twig/pull/4176#issuecomment-2283168241 by `@smnandre`

```
# Old
Benchmark 1: php bench.php
  Time (mean ± σ):      1.192 s ±  0.004 s    [User: 1.163 s, System: 0.009 s]
  Range (min … max):    1.186 s …  1.198 s    10 runs

# New
Benchmark 1: php bench.php
  Time (mean ± σ):     356.5 ms ±   1.1 ms    [User: 342.0 ms, System: 7.6 ms]
  Range (min … max):   354.6 ms … 358.2 ms    10 runs
```

Using bench.php:
```php
<?php

require_once __DIR__.'/vendor/autoload.php';

$string = str_repeat("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut a tincidunt turpis. \\App\\Entity\\Product \World \065 App\#{var} \'quoted\' Donec pharetra enim quis erat pharetra, dignissim molestie erat laoreet. Vivamus auctor purus sed lorem vestibulum rhoncus.", 10);
$loader = new Twig\Loader\ArrayLoader([
    'index.twig' => <<<EOF
{{ "$string" }}
EOF
    ,
]);

$twig = new Twig\Environment($loader);

for ($i = 0; $i < 10000; $i++) {
    $twig->tokenize(new Twig\Source($twig->getLoader()->getSourceContext('index.twig')->getCode(), 'index.twig'));
}

```

Commits
-------

bc6e36de Optimize stripcslashes
2024-08-12 09:25:01 +02:00
Ruud Kamphuis bc6e36dee9 Optimize stripcslashes 2024-08-12 09:24:57 +02:00