summaryrefslogtreecommitdiffstatshomepage
path: root/core/lib/Drupal/Core/Utility/PhpRequirements.php
blob: c88d82b18a5ae6fbf691862922f4588fd3dc43f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

namespace Drupal\Core\Utility;

/**
 * Provides an object for dynamically identifying the minimum supported PHP.
 */
final class PhpRequirements {

  /**
   * The minimum PHP version requirement for the installed Drupal version.
   *
   * This property is maintained to make the class testable.
   *
   * @var string
   *
   * @see version_compare()
   */
  private static $drupalMinimumPhp = \Drupal::MINIMUM_PHP;

  /**
   * The expected PHP version end-of-life dates, keyed by PHP minor version.
   *
   * The array keys are in 'major.minor' format, and the date values are in ISO
   * 8601 format.
   *
   * @var string[]
   *   An array of end-of-life dates in ISO 8601 format, keyed by the PHP minor
   *   version in 'major.minor' format. The list must be sorted in an ascending
   *   order by the date. Multiple versions EOL on the same day must be sorted
   *   by the PHP version.
   */
  private static $phpEolDates = [
    '8.3' => '2027-12-31',
  ];

  /**
   * This class should not be instantiated.
   */
  private function __construct() {
  }

  /**
   * Dynamically identifies the minimum supported PHP version based on the date.
   *
   * Drupal automatically increases the minimum supported PHP version from
   * \Drupal::MINIMUM_PHP to a newer version after PHP's documented end-of-life
   * date for the previous version.
   *
   * Below this version:
   * - New sites can be installed (to allow update deployment workflows that
   *   reinstall sites from configuration), but a warning is displayed in the
   *   installer that the PHP version is too old (except within tests).
   * - Updates from previous Drupal versions can be run, but users are warned
   *   that Drupal no longer supports that PHP version.
   * - An error is shown in the status report that the PHP version is too old.
   *
   * @param \DateTime|null $date
   *   The DateTime to check. Defaults to the current datetime (now) if NULL.
   *
   * @return string
   *   The minimum supported PHP version on the date in a PHP-standardized
   *   number format supported by version_compare(). For example, '8.0.2' or
   *   '8.1'. This will be the lowest PHP version above the minimum PHP version
   *   supported by Drupal that is still supported, or the highest known PHP
   *   version if no known versions are still supported.
   *
   * @see version_compare()
   */
  public static function getMinimumSupportedPhp(?\DateTime $date = NULL): string {
    // By default, use the current date (right now).
    $date = $date ?? new \DateTime('now');

    // In case no data are available or all known PHP versions in this class
    // are already end-of-life, default to the version that had the most recent
    // end-of-life (the key of the last element in the sorted array).
    // The string cast ensures the value is a string, even if the PHP EOL date
    // array is empty. As of PHP 8.1, version_compare() no longer accepts NULL
    // as a parameter; empty string must be used instead.
    $lowest_supported_version = (string) array_key_last(static::$phpEolDates);

    // Next, look at versions that are end-of-life after the current date.
    // Find the lowest PHP version that is still supported.
    foreach (static::$phpEolDates as $version => $eol_date) {
      $eol_datetime = new \DateTime($eol_date);

      if ($eol_datetime > $date) {
        // If $version is less than the previously discovered lowest supported
        // version, use $version as the lowest supported version instead.
        if (version_compare($version, $lowest_supported_version) < 0) {
          $lowest_supported_version = $version;
        }
      }
    }

    // If PHP versions older than the Drupal minimum PHP version are still
    // supported, return Drupal minimum PHP version instead.
    if (version_compare($lowest_supported_version, static::$drupalMinimumPhp) < 0) {
      return static::$drupalMinimumPhp;
    }

    // Otherwise, return the lowest supported PHP version.
    return $lowest_supported_version;
  }

}