Managing Flex, extra.symfony.require & Version Constraints

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

We just ran:

composer update "symfony/*"

Thanks to the extra.symfony.require key in our composer.json file:

104 lines composer.json
{
... lines 2 - 95
"extra": {
"symfony": {
... lines 98 - 99
"require": "4.3.*"
}
}
}

Which is currently set to 4.3.*, it only upgraded things to the latest 4.3 version - not 4.4. Let's change this to 4.4.*:

104 lines composer.json
{
... lines 2 - 95
"extra": {
"symfony": {
... lines 98 - 99
"require": "4.4.*"
}
}
}

But wait... why are we upgrading to Symfony 4.4? Isn't this a tutorial about upgrading to Symfony 5? Why not just go straight there? The reason is due to Symfony's, honestly, incredible upgrade policy. Symfony never breaks backwards compatibility for a minor release - like from 4.3 to 4.4. Instead, it deprecates code... and you can see what deprecated code you're using with some special tools. By upgrading to 4.4, we'll be able to see the full list of deprecated things we need to fix. Then we can fix them before upgrading to Symfony 5. We'll see this later.

Anyways, find your terminal and, once again, run:

composer update "symfony/*"

Yea! This time it is updating the Symfony packages to 4.4. That was easy!

composer.json Version Constraints for symfony/ Packages

Except... come on... it's never quite that easy. In fact, some Symfony packages did not upgrade. Check it out. Run:

composer show symfony/mailer

Scroll up: woh! This is still on version 4.3! Why?

Open up the composer.json file and find symfony/mailer:

104 lines composer.json
{
... lines 2 - 3
"require": {
... lines 5 - 24
"symfony/framework-bundle": "^4.0",
"symfony/mailer": "4.3.*",
... lines 27 - 40
},
... lines 42 - 102
}

Interesting: some packages - like symfony/form or symfony/framework-bundle are set to ^4.0 - which more or less means 4.*. But the symfony/mailer version if 4.3.*.

Symfony Flex: composer.json Version Formatting

There are two things I need to say about this. First, usually when you run compose require some/package, when Composer updates your composer.json file, it uses the "caret" (^) format. That's why you see ^3.0 and ^1.1.

But, when you use Symfony Flex and composer require a Symfony package, it changes that to use the * format - like 4.3.*. That's not a huge deal. In fact, it's almost an accidental feature - but it is nice because the best-practice is typically to control the "minor" version of your Symfony packages - that's the middle number - so that you can upgrade them all at the same time.

But... Flex didn't always do this. That's why, in my project, you see a mixture: some libraries like symfony/form have the "caret" format and other libraries - that were installed more recently like symfony/mailer - use the "star" format.

Symfony Flex: symfony.extra.require is a "Soft" Requirement

The second thing I need to tell you is that the extra.symfony.require config - set to 4.4.* now - is... more of a "suggestion". It doesn't force all Symfony packages to this version. More accurately it says:

When any symfony/ package is updated, its upgrade will be restricted to a version matching 4.4.*

But if you have a package that is specifically locked to 4.3.*, it won't override that and force it to 4.4.*. That is why symfony/mailer didn't upgrade.

Changing symfony/ composer.json Versions

If all this explanation doesn't make total sense... or you just done care - Hey, that's ok! Here is what you need to know: whenever you upgrade Symfony to a new minor version - like 4.3 to 4.4, you need to do two things: (1) update the extra.symfony.require value and (2) update all the package versions to 4.4.*.

If that seems a bit redundant, it... kinda is! But changing the version next to the package to 4.4.* gives you clear control of what's going on... and it's how Composer normally works. And then, the extra.symfony.require config gives us a big performance boost in the background.

Let's do this next, upgrade to Symfony 4.4 and fix a few packages that ended up inside our "dev" dependencies incorrectly.

Leave a comment!

  • 2020-05-15 Vladimir Sadicov

    Hey @Simo He

    Yes it's correct it's soft requirement for automatic upgrading already installed libraries, but it will not give you ability to install whatever symfony library you want, because flex will limit the list of versions according to this require section. But it will not work for libraries which don't have this version in their list.

    Cheers!

  • 2020-05-15 Simo He
  • 2020-05-15 Simo He

    You write that

    symfony.extra.require is a soft requirement

    (in Symfony Flex: symfony.extra.require is a "Soft" Requirementhttps://symfonycasts.com/sc.... Would this mean I can install a package in a different symfony version? My tries show that this does not work, see

  • 2020-03-09 weaverryan

    Hey maxii123!

    Hmm, in theory, yes, that would work :). That it the most aggressive way to upgrade. What I mean is, you will be upgrading ALL your dependencies, and you could have some BIG upgrades. But it should work and *would* be simple. I would also *at least* upgrade a few of the most important recipes - like framework-bundle and symfony/console.

    If you try it, let me know how it goes!

    Cheers!

  • 2020-03-09 maxii123

    Should I be able to delete composer.lock, update composer.json to your code download , remove vendor and then composer install? There really is nothing "easy" about this upgrade it seems to puzzle after puzzle to get to the endpoint ;)

  • 2020-01-09 Diego Aguiar

    Hey Sung Lee

    Yeah! You need to install the composer plugin for PHPStorm :)

    Cheers!

  • 2020-01-09 Sung Lee

    In my PhpStorm IDE, exact version number is not displayed like the one in this video. In this video, when a cursor is in a line, it shows exact version number in light grey color. Do I need to install a plugin? Thanks!