From 1f18062168407d22d5057ce5718bc807378e1547 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 20 Aug 2019 19:48:41 -0700 Subject: [PATCH] (dev/drupal#79) Fail more gracefully when attempting to install on PHP 5.x Before ------ If an admin stracts the code and navigates to `/sites/all/modules/civicrm/install/index.php`, it displays a syntax error. After ----- If an admin stracts the code and navigates to `/sites/all/modules/civicrm/install/index.php`, it displays the message: > __PHP Version Requirement__ > CiviCRM requires PHP 7.0+. The web server is running PHP 5.6.38. Comments -------- This is similar to https://github.com/civicrm/civicrm-drupal/pull/583 The canonical representation of the minimum PHP version is in `$civicrm_root/CRM/Upgrade/Form.php`. However, setting up the classloader triggers a syntax error, so we need to read this without having access to the classloader. The approach herein has a few effects: * The minimum PHP can be read from a JSON file. * That JSON file is also used by `composer`, so you'll also get better errors when downloading that way. * At some unknown point, the minimum will probably bump up again (7.1 or 7.2 or whatever). When that happens, the unit-test will ensure we keep `CRM/Upgrade/Form.php` and `composer.json` in sync. Note: I was little concerned that the `composer.json` file might not be available when normal installers run, so I checked the published tarballs for D7, BD, WP, and J - in all cases, the `composer.json` looks to be included at the expected location. --- composer.json | 1 + install/index.php | 9 +++++++++ tests/phpunit/CRM/Upgrade/FormTest.php | 23 +++++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 tests/phpunit/CRM/Upgrade/FormTest.php diff --git a/composer.json b/composer.json index 1cfa14b776..f5dfe17601 100644 --- a/composer.json +++ b/composer.json @@ -34,6 +34,7 @@ }, "include-path": ["vendor/tecnickcom"], "require": { + "php": "~7.0", "dompdf/dompdf" : "0.8.*", "electrolinux/phpquery": "^0.9.6", "symfony/config": "^2.8.44 || ~3.0", diff --git a/install/index.php b/install/index.php index 296f576ad6..585cea24ac 100644 --- a/install/index.php +++ b/install/index.php @@ -91,6 +91,15 @@ else { errorDisplayPage($errorTitle, $errorMsg, FALSE); } +$composerJsonPath = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'composer.json'; +if (file_exists($composerJsonPath)) { + $composerJson = json_decode(file_get_contents($composerJsonPath), 1); + $minPhpVer = preg_replace(';[~^];', '', $composerJson['require']['php']); + if (!version_compare(phpversion(), $minPhpVer, '>=')) { + errorDisplayPage('PHP Version Requirement', sprintf("CiviCRM requires PHP %s+. The web server is running PHP %s.", $minPhpVer, phpversion()), FALSE); + } +} + $pkgPath = $crmPath . DIRECTORY_SEPARATOR . 'packages'; require_once $crmPath . '/CRM/Core/ClassLoader.php'; diff --git a/tests/phpunit/CRM/Upgrade/FormTest.php b/tests/phpunit/CRM/Upgrade/FormTest.php new file mode 100644 index 0000000000..a9af358959 --- /dev/null +++ b/tests/phpunit/CRM/Upgrade/FormTest.php @@ -0,0 +1,23 @@ +assertFileExists($composerJsonPath); + $composerJson = json_decode(file_get_contents($composerJsonPath), 1); + $composerJsonRequirePhp = preg_replace(';[~^];', '', $composerJson['require']['php']); + $actualMajorMinor = preg_replace(';^[\^]*(\d+\.\d+)\..*$;', '\1', $composerJsonRequirePhp); + $expectMajorMinor = preg_replace(';^[\^]*(\d+\.\d+)\..*$;', '\1', \CRM_Upgrade_Form::MINIMUM_PHP_VERSION); + $this->assertEquals($expectMajorMinor, $actualMajorMinor, "The PHP version requirements in CRM_Upgrade_Form ($expectMajorMinor) and composer.json ($actualMajorMinor) should specify same major+minor versions."); + } + +} -- 2.25.1