Skip to content

Commit d737795

Browse files
committed
feature #576 Enforce PHP version at runtime to be same or higher as at "composer update" time (nicolas-grekas)
This PR was merged into the 1.4-dev branch. Discussion ---------- Enforce PHP version at runtime to be same or higher as at "composer update" time When e.g. PHP 7.3 is used to compute dependencies, here is the message that is now generated when running `bin/console` or `symfony serve` *on PHP 7.1*: ``` Fatal Error: composer.lock was created for PHP version 7.3 or higher but the current PHP version is 7.1.33. ``` We have too many reports on symfony/symfony about ppl that mess up with these. It works by adding a simple *static* check in `vendor/autoload.php`. Note that `composer install` already yells, so it's already covered. Commits ------- e39f329 Enforce PHP version at runtime to be same or higher as at "composer update" time
2 parents f5bfc79 + e39f329 commit d737795

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/Flex.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ public function configureInstaller()
268268
return $backtrace;
269269
}
270270

271+
public function lockPlatform()
272+
{
273+
$this->lock->set('php', [
274+
'version' => $this->config->get('platform')['php'] ?? (PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION),
275+
]);
276+
}
277+
271278
public function configureProject(Event $event)
272279
{
273280
if (!$this->downloader->isEnabled()) {
@@ -383,6 +390,8 @@ public function update(Event $event = null, $operations = [])
383390

384391
public function install(Event $event = null)
385392
{
393+
$this->updateAutoloadFile();
394+
386395
$rootDir = $this->options->get('root-dir');
387396

388397
if (!file_exists("$rootDir/.env") && !file_exists("$rootDir/.env.local") && file_exists("$rootDir/.env.dist") && false === strpos(file_get_contents("$rootDir/.env.dist"), '.env.local')) {
@@ -653,6 +662,36 @@ public function generateFlexId()
653662
$this->updateComposerLock();
654663
}
655664

665+
private function updateAutoloadFile()
666+
{
667+
if (!$platform = $this->lock->get('php')['version'] ?? null) {
668+
return;
669+
}
670+
671+
$autoloadFile = $this->config->get('vendor-dir').'/autoload.php';
672+
673+
$code = file_get_contents($autoloadFile);
674+
$code = substr($code, \strlen("<?php\n"));
675+
676+
if (false !== strpos($code, 'PHP_VERSION_ID')) {
677+
return;
678+
}
679+
680+
$platform = preg_replace('/[^-+.~_\w]/', '', $platform);
681+
$version = sprintf('%d%02d%02d', ...explode('.', $platform.'.0.0'));
682+
683+
file_put_contents($autoloadFile, <<<EOPHP
684+
<?php
685+
686+
if (\PHP_VERSION_ID < $version) {
687+
echo sprintf("Fatal Error: composer.lock was created for PHP version $platform or higher but the current PHP version is %d.%d.%d.\\n", PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION);
688+
exit(1);
689+
}
690+
$code
691+
EOPHP
692+
);
693+
}
694+
656695
private function fetchRecipes(): array
657696
{
658697
if (!$this->downloader->isEnabled()) {
@@ -839,7 +878,7 @@ public static function getSubscribedEvents(): array
839878

840879
return [
841880
InstallerEvents::PRE_DEPENDENCIES_SOLVING => [['populateProvidersCacheDir', PHP_INT_MAX]],
842-
InstallerEvents::POST_DEPENDENCIES_SOLVING => [['populateFilesCacheDir', PHP_INT_MAX]],
881+
InstallerEvents::POST_DEPENDENCIES_SOLVING => [['populateFilesCacheDir', PHP_INT_MAX], ['lockPlatform']],
843882
PackageEvents::PRE_PACKAGE_INSTALL => [['populateFilesCacheDir', ~PHP_INT_MAX]],
844883
PackageEvents::PRE_PACKAGE_UPDATE => [['populateFilesCacheDir', ~PHP_INT_MAX]],
845884
PackageEvents::POST_PACKAGE_INSTALL => __CLASS__ === self::class ? [['record'], ['checkForUpdate']] : 'record',

0 commit comments

Comments
 (0)