Skip to content

Intl recommendation + PHP81_BC\strftime + Symfony ICU Polyfill#69

Merged
kamilwylegala merged 4 commits intomasterfrom
intl-requirement
Jun 5, 2024
Merged

Intl recommendation + PHP81_BC\strftime + Symfony ICU Polyfill#69
kamilwylegala merged 4 commits intomasterfrom
intl-requirement

Conversation

@kamilwylegala
Copy link
Copy Markdown
Owner

No description provided.

@diegosurita
Copy link
Copy Markdown
Contributor

Hey! I see that you only updated the README.md and composer.json files. There are some places that are using strftime function. Will you work on the removal of thoses functions or you want me to handle that in another PR?

@pbarabe
Copy link
Copy Markdown
Contributor

pbarabe commented May 17, 2024

Hi! Sorry to chime in so late, but regarding updating use of strftime...

If this MR is going to go that far, I'm wondering if it could be made to be less of a breaking change by preferring IntlDateFormatter or datefmt_create and falling back to date if a project does not (or cannot) require intl?

Maybe something like this in Utility\CakeTime:

protected static function _strftime($format, $timestamp) {

    // $format = strftime($format, $timestamp);

    if (function_exists('datefmt_create')) {
        $format = datefmt_create(/* args */);

    } else if (class_exists('IntlDateFormatter')) {
        $format = new IntlDateFormatter(/* args */);

    } else {
        // fallback to date() instead of strftime()
        $format = date($format, $timestamp);
    }
    
    // ...
    // ...the rest of _strftime()
    // ...
}

That strikes me as a bit friendlier approach for anyone who may have difficulty installing intl for some reason.

@kamilwylegala
Copy link
Copy Markdown
Owner Author

@pbarabe @diegosurita Thank you for your comments. Maybe we could use Symfony's polyfill for ithttps://github.com/symfony/polyfill-intl-icu ? What do you think?

@pbarabe
Copy link
Copy Markdown
Contributor

pbarabe commented May 20, 2024

Hmmm, so looking into this a bit more, I realize the code I suggested previously won't really work (not as suggested, anyway), since PHP's strftime, date, datefmt_create, and IntlDateFormatter all take different format arguments. (Sorry about that - I'd assumed their respective format args were the same or similar, but I was definitely wrong.)

I think that adding ext-intl under the composer.php's suggest, rather than require, is already fairly "nice", since someone can elect to install intl but isn't obliged to.

But since the real problem appears to be that strftime is deprecated in PHP 8.1, I don't think I understand how adding intl helps with that. Won't any use of strftime trigger deprecation warnings, regardless of whether intl is installed or not?

polyfill-intl-icu sounds useful, although since so many of CakeTime's methods wrap strftime in one way or another, it seems like you're then committing to a more substantial rewrite. Maybe that's OK?

I wonder if another path worth considering would be to simply deprecate the entire CakeTime class, given its reliance on strftime? That's effectively suggesting that client applications refactor on something else, though it would seem that one way or another, that'll eventually be necessary anyway.

@pbarabe
Copy link
Copy Markdown
Contributor

pbarabe commented May 20, 2024

But since the real problem appears to be that strftime is deprecated in PHP 8.1, I don't think I understand how adding intl helps with that. Won't any use of strftime trigger deprecation warnings, regardless of whether intl is installed or not?

Nevermind this. I just looked through the commit history of @diegosurita's PR #64 and saw where he'd started to refactor by using IntlDateFormatter::format() but backed out b/c ext-intl is not required. Sorry for my confusion... I get where that was all going now.

@diegosurita
Copy link
Copy Markdown
Contributor

diegosurita commented May 23, 2024

Hello guys!

Maybe we could use Symfony's polyfill for ithttps://github.com/symfony/polyfill-intl-icu ? What do you think?

@kamilwylegala Yeah, we definitely could use that polyfill.

@kamilwylegala
Copy link
Copy Markdown
Owner Author

Hello guys!

Maybe we could use Symfony's polyfill for ithttps://github.com/symfony/polyfill-intl-icu ? What do you think?

@kamilwylegala Yeah, we definitely could use that polyfill.

OK, I will work on extending this branch next week. 😁

@kamilwylegala kamilwylegala marked this pull request as draft May 25, 2024 21:59
@kamilwylegala kamilwylegala changed the title Intl extension requirement. Intl recommendation + PHP81_BC\strftime + Symfony ICU Polyfill May 30, 2024
@kamilwylegala
Copy link
Copy Markdown
Owner Author

@diegosurita Done ✔️ I had to also install PHP81_BC\strftime. Covering all features of strftime using Intl is tricky job. 😁

@kamilwylegala kamilwylegala marked this pull request as ready for review May 30, 2024 10:21
Comment thread lib/Cake/Utility/CakeTime.php Outdated
}
if (!$valid) {
$format = mb_convert_encoding($format, 'UTF-8', 'ISO-8859-1');
$formatted = mb_convert_encoding($format, 'UTF-8', 'ISO-8859-1');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @kamilwylegala, I'm curious about the change in this line. I see that instead of updating the value of $format, you're instantiating a new variable $formatted. But it doesn't seem like $formatted gets used anywhere, and then the method just returns $format. What am I missing?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Good catch @pbarabe ! I initially experimented with mimicking the behavior of strftime on my own and after that I discovered PHP81_BC/strftime - this is a leftover. I will revert this code. Thanks!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh good deal - glad I mentioned it. And thanks for finding an easy(ish) solution!

@pbarabe
Copy link
Copy Markdown
Contributor

pbarabe commented May 30, 2024

Looks like PHP81_BC\strftime is saving the day! I think this all looks great (but still curious about the intent of the change in my previous comment).

@kamilwylegala
Copy link
Copy Markdown
Owner Author

Looks like PHP81_BC\strftime is saving the day! I think this all looks great (but still curious about the intent of the change in my previous comment).

Thank you! I will soon merge this PR 😊

@kamilwylegala kamilwylegala merged commit b918df8 into master Jun 5, 2024
@kamilwylegala
Copy link
Copy Markdown
Owner Author

Thank you @diegosurita @pbarabe Merged!

@kamilwylegala kamilwylegala deleted the intl-requirement branch June 5, 2024 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants