Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Templating optional #520

Merged
merged 21 commits into from
Jul 20, 2022
Merged

Conversation

Hanmac
Copy link
Contributor

@Hanmac Hanmac commented May 25, 2022

Subject

This makes the Symfony/Templating Dependency Optional.
The Twig Components don't need it, so they use the new base classes.
The Old Services for the Helper still exist, but they are only loaded when the templating component is used.

i don't know if it is 100% BC or not.

I am targeting this branch, because it should be BC.

Closes #518.

Changelog

### Deprecated
- Deprecated the Templating\Helper classes
- Deprecated the use of the Templating\Helper classes inside the Twig Runtime and Extensions
- Deprecated uses of LocaleDetectorInterface in favor of Symfony\Contracts\Translation\LocaleAwareInterface

### Changed
- Make `Symfony/Templating` Component optional

Copy link
Member

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this PR is made of multiple BC-break.

I think you'll have to duplicate the BaseHelper so far (one in the Helper folder and one in the Helper/Templating).

Also you'll have to keep the old alias/service declaration. But you can add some TODO for the next major in order to remove the deprecated code.

src/Helper/BaseHelper.php Show resolved Hide resolved
src/Helper/BaseHelper.php Outdated Show resolved Hide resolved
src/Resources/config/autowire.php Outdated Show resolved Hide resolved
@Hanmac
Copy link
Contributor Author

Hanmac commented May 25, 2022

the problem isn't a possible duplicated base helper, but if we really need duplicated DateTimeHelper code (if i can't have the Templating ones extend the Twig ones)

the question is more if it is really a BC break if the Templating\Helper\DateTimeHelper doesn't extend Symfony\Component\Templating\Helper anymore but implements the Symfony\Component\Templating\Helper\HelperInterface

or is it an BC break if Templating\Helper\DateTimeHelper doesn't extend from Templating\Helper\BaseHelper but from Helper\BaseHelper instead over Helper\DateTimeHelper ?

@VincentLanglet
Copy link
Member

the question is more if it is really a BC break if the Templating\Helper\DateTimeHelper doesn't extend Symfony\Component\Templating\Helper anymore but implements the Symfony\Component\Templating\Helper\HelperInterface

If someone is doing if ($foo instanceof Symfony\Component\Templating\Helper) the condition was true and after this pr it will be false ; so it's a bc break.

@Hanmac
Copy link
Contributor Author

Hanmac commented May 25, 2022

so the only way without BC break would be to duplicate all the helper classes,

because i probably can't change the constructor to give them the Twig Helper. right?

@VincentLanglet
Copy link
Member

so the only way without BC break would be to duplicate all the helper classes,
because i probably can't change the constructor to give them the Twig Helper. right?

I'm not sure about the purpose of this PR and the strategy you have in mind to do it so I currently cannot say what is the best way to do this without BC-break.

A constructor can be change if you do it this way:

public construct(Foo $foo) {}

to

public construct(Foo $foo, ?Bar $bar = null) {}

or

public construct(Foo|Bar $fooOrBar) {}

but it has to still work if you pass the old elements.

@Hanmac
Copy link
Contributor Author

Hanmac commented May 30, 2022

so the only way without BC break would be to duplicate all the helper classes,
because i probably can't change the constructor to give them the Twig Helper. right?

I'm not sure about the purpose of this PR and the strategy you have in mind to do it so I currently cannot say what is the best way to do this without BC-break.

the main reason was to make the templating dependency optional

for this i moved/created helpers that doesn't extend on templating Helper (no BC)
than can be used for the Twig Extension/Runtime. This should also be no BC, right?
Until this point that should be no BC, just duplicated code.

because of duplicated code, i wanted the templating helper use the twig helper, but i can't do that because it would change the templating Helper in a way that would be a BC.
The only way that would work where i can remove duplicated code:

  • either inherit from them, (not BC)
  • or use the twig helper as constructor param (would also not be BC because it can't be null)

@VincentLanglet
Copy link
Member

for this i moved/created helpers that doesn't extend on templating Helper (no BC)
than can be used for the Twig Extension/Runtime. This should also be no BC, right?
Until this point that should be no BC, just duplicated code.

You can have a Helper and a HelperWithoutTemplating and then changing the constructor of the twig Extension to accepts

Helper|HelperWithoutTemplating

This is BC.

@Hanmac
Copy link
Contributor Author

Hanmac commented May 30, 2022

for this i moved/created helpers that doesn't extend on templating Helper (no BC)
than can be used for the Twig Extension/Runtime. This should also be no BC, right?
Until this point that should be no BC, just duplicated code.

You can have a Helper and a HelperWithoutTemplating and then changing the constructor of the twig Extension to accepts

Helper|HelperWithoutTemplating

This is BC.

This wouldn't work with the current php version defined in composer.json
currently that is ^7.3 || ^8.0, your suggestion would drop the 7.3 and make 8.0 required.

because my projects aren't for 8.0 yet i would like to avoid that.

@VincentLanglet
Copy link
Member

VincentLanglet commented May 30, 2022

This wouldn't work with the current php version defined in composer.json
currently that is ^7.3 || ^8.0, your suggestion would drop the 7.3 and make 8.0 required.
because my projects aren't for 8.0 yet i would like to avoid that.

You dont have to use the typehint, just the phpdoc.

Changing

    public function __construct(DateTimeHelper $helper)
    {
        $this->helper = $helper;
    }

to

/** @param DateTimeHelper|DateTimeHelperWithoutTemplating $helper */
    public function __construct(object $helper)
    {
        $this->helper = $helper;
    }

@VincentLanglet VincentLanglet requested a review from a team May 30, 2022 19:38
@SonataCI
Copy link
Collaborator

Could you please rebase your PR and fix merge conflicts?

@SonataCI
Copy link
Collaborator

Could you please rebase your PR and fix merge conflicts?

@Hanmac Hanmac marked this pull request as ready for review June 28, 2022 08:03
@derrabus
Copy link

Hello. I'm currently working on a codebase that uses this package and this PR would allow us to remove the templating component from our dependencies. I see that progress on the PR is stalling somehow. Is there anything I can do to help?

@Hanmac
Copy link
Contributor Author

Hanmac commented Jul 12, 2022

@VincentLanglet should i mark the Templating Helpers as Deprecated to be removed in a later PR?

src/DependencyInjection/SonataIntlExtension.php Outdated Show resolved Hide resolved
src/Twig/DateTimeRuntime.php Show resolved Hide resolved
@VincentLanglet
Copy link
Member

@VincentLanglet should i mark the Templating Helpers as Deprecated to be removed in a later PR?

Yes please :)

And I assume you can add a NEXT_MAJOR: Remove this code above

if (interface_exists(HelperInterface::class)) {
            $loader->load('templating.php');
        }

then.

@VincentLanglet
Copy link
Member

Hello. I'm currently working on a codebase that uses this package and this PR would allow us to remove the templating component from our dependencies. I see that progress on the PR is stalling somehow. Is there anything I can do to help?

I made some reviews. I think the PR seems ready, it just require some deprecation message I think @Hanmac will add.
Any extra review is welcome.

@VincentLanglet VincentLanglet requested review from jordisala1991 and a team July 12, 2022 14:43
Copy link
Member

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for others and it will be perfect

src/Twig/DateTimeRuntime.php Outdated Show resolved Hide resolved
src/Twig/DateTimeRuntime.php Outdated Show resolved Hide resolved
@VincentLanglet
Copy link
Member

And take a look to the linter ;)

VincentLanglet
VincentLanglet previously approved these changes Jul 16, 2022
src/Helper/DateTimeHelper.php Outdated Show resolved Hide resolved
src/Helper/DateTimeHelper.php Outdated Show resolved Hide resolved
src/Helper/NumberHelper.php Outdated Show resolved Hide resolved
* @param array $symbols The symbols used by \NumberFormatter
* @param string|null $locale The locale used to format the number
*/
public function format($number, int $style, array $attributes = [], array $textAttributes = [], array $symbols = [], ?string $locale = null): string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to make all format methods public? or the class should be used with the other methods? is this used outside this class?

src/Helper/BaseHelper.php Outdated Show resolved Hide resolved
Copy link
Member

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the interface methods, I wonder if it shouldn't be better to rename the class/interface to
DateTimeFormatter
NumberFormatter
LocaleSomething

It could be better than Helper

@VincentLanglet
Copy link
Member

(and tests are failing)

@Hanmac
Copy link
Contributor Author

Hanmac commented Jul 19, 2022

Looking at the interface methods, I wonder if it shouldn't be better to rename the class/interface to DateTimeFormatter NumberFormatter LocaleSomething

It could be better than Helper

i don't know about different helper names, can you decide before i change the code again?

@VincentLanglet
Copy link
Member

i don't know about different helper names, can you decide before i change the code again?

To me, DateTimeFormatter and NumberFormatter are much better names since all the methods are "formatFoo".
For the LocaleHelper, there is better to find, not sure what.

Looking at https://github.com/sonata-project/SonataIntlBundle/blob/2.x/src/Twig/LocaleRuntime.php, all three method are returning a localized name. I don't know if something like a Localizer or NameLocalizer or ... would be a thing.

Let's wait for @jordisala1991 opinion.

@jordisala1991
Copy link
Member

i don't know about different helper names, can you decide before i change the code again?

To me, DateTimeFormatter and NumberFormatter are much better names since all the methods are "formatFoo". For the LocaleHelper, there is better to find, not sure what.

Looking at https://github.com/sonata-project/SonataIntlBundle/blob/2.x/src/Twig/LocaleRuntime.php, all three method are returning a localized name. I don't know if something like a Localizer or NameLocalizer or ... would be a thing.

Let's wait for @jordisala1991 opinion.

Agree! To me if we are introducing new code, let's try to be as good as possible, because we are going to maintain it.

@Hanmac
Copy link
Contributor Author

Hanmac commented Jul 20, 2022

@jordisala1991 your opinion for better names?

@VincentLanglet
Copy link
Member

@jordisala1991 your opinion for better names?

I talked with him.

DateTimeFormatter
NumberFormatter
Localizer

are ok.

@Hanmac
Copy link
Contributor Author

Hanmac commented Jul 20, 2022

i did better names, also i made twig use the interfaces instead.

and i added the interfaces to autowire

VincentLanglet
VincentLanglet previously approved these changes Jul 20, 2022
@VincentLanglet
Copy link
Member

Some static analysis build are failing

@VincentLanglet VincentLanglet merged commit 5f3469b into sonata-project:2.x Jul 20, 2022
@Hanmac Hanmac deleted the templatingOpt branch July 20, 2022 17:29
])

->set('sonata.intl.templating.helper.datetime', '%sonata.intl.templating.helper.datetime.class%')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing a service alias is a BC break (or this kind of change was handled as a BC break a few years ago...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make Templating Dependency Optional
6 participants