Problem/Motivation

Every time we update \Drupal::MINIMUM_PHP we have to update core/install.php.

Proposed resolution

Read the value from core/composer.json because we have to keep that in sync too and it is tested in \Drupal\Tests\Composer\ComposerTest::testEnsurePhpConfiguredVersion()

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#3 3266523-2.patch2.6 KBalexpott
#2 3266523-2.patch2.6 KBalexpott

Comments

alexpott created an issue. See original summary.

alexpott’s picture

Status: Active » Needs review
StatusFileSize
new2.6 KB
alexpott’s picture

Version: 9.4.x-dev » 10.0.x-dev
StatusFileSize
new2.6 KB

Has to go on 10.0.x first.

alexpott’s picture

Here's the contents on \Drupal\Tests\Composer\ComposerTest::testEnsurePhpConfiguredVersion

  /**
   * Ensure that the configured php version matches the minimum php version.
   *
   * Also ensure that the minimum php version in the root-level composer.json
   * file exactly matches \Drupal::MINIMUM_PHP.
   */
  public function testEnsurePhpConfiguredVersion() {
    $composer_json = json_decode(file_get_contents($this->root . '/composer.json'), TRUE);
    $composer_core_json = json_decode(file_get_contents($this->root . '/core/composer.json'), TRUE);
    $this->assertEquals(\Drupal::MINIMUM_PHP, $composer_json['config']['platform']['php'], 'The \Drupal::MINIMUM_PHP constant should always be exactly the same as the config.platform.php in the root composer.json.');
    $this->assertEquals($composer_core_json['require']['php'], '>=' . $composer_json['config']['platform']['php'], 'The config.platform.php configured version in the root composer.json file should always be exactly the same as the minimum php version configured in core/composer.json.');
  }

As we can see the assumption made about the contents of the require.php key are tested...

+++ b/core/install.php
@@ -23,14 +23,13 @@
+// result from loading the autoloader or core/lib/Drupal.php. The format in
+// composer.json is assumed to be ">=8.1.0".
+$minimum_version = substr(json_decode(file_get_contents(__DIR__ . '/composer.json'), TRUE)['require']['php'], 2);

So the substr() here is safe to do.

xjm’s picture

Priority: Normal » Major
  1. +++ b/core/install.php
    @@ -23,14 +23,13 @@
    +// result from loading the autoloader or core/lib/Drupal.php. The format in
    +// composer.json is assumed to be ">=8.1.0".
    +$minimum_version = substr(json_decode(file_get_contents(__DIR__ . '/composer.json'), TRUE)['require']['php'], 2);
    

    Doesn't Composer provide API for this?

    I'm not sure we can always guarantee that the composer.json constraint will be in this format, can we? Edit: Crossposted with #4 which answers this.

  2. +++ b/core/lib/Drupal.php
    @@ -108,11 +108,7 @@ class Drupal {
    -   * Note: To prevent the installer from having fatal errors on older versions
    -   * of PHP, the value of this constant is hardcoded twice in core/install.php:
    -   * - Once as a parameter of version_compare()
    -   * - Once in the error message printed to the user immediately after.
    -   * Remember to update both whenever this constant is updated.
    +   * Note: This is duplicated as a composer constraint in core/composer.json.
    

    I don't think we should remove these docs; rather, we should change them to remind the developer to update both the Composer constraint and the constant if we keep it (or while it is retained as deprecated).

longwave’s picture

> Doesn't Composer provide API for this?

This is pre-bootstrap and so I don't think we can rely on API, we are doing this before we have set up the autoloader even.

xjm’s picture

Sorry, #5 was half a thought. #6 is correct for the early bootstrap case -- that's why we hardcode the check there to begin with -- but I was wondering if we could do away with MINIMUM_PHP entirely and just load it from the Composer constraint everywhere.

alexpott’s picture

I think the notes to remember to update both are covered by the line added:

Note: This is duplicated as a composer constraint in core/composer.json.

And the existing test that's shown in #4 makes it impossible for them to get out of sync.

alexpott’s picture

I like the idea in #7 of deprecating \Drupal::MINIMUM_PHP - I think we should do this in a follow-up because I'd want to focus on any performance concerns. The constant is used when any instance of ModuleExtensionList, ThemeExtensionList etc... are created. I don't think any of these are on the performance critical path but reading the json will definitely not be as cheap as the constant and potentially during a site install these thing might be created a lot.

xjm’s picture

Status: Needs review » Needs work
Issue tags: +Needs followup

Hm, to me, #9 does not explain anything. It expects the developer to read our minds about what the comment means. The previous comment told the developer exactly what to do, and we shouldn't remove that sort of explicit info.

Tagging for a followup for #10.

Version: 10.0.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.