Skip to content

Enforce PHP version at runtime to be same or higher as at "composer update" time #576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/Flex.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ public function configureInstaller()
return $backtrace;
}

public function lockPlatform()
{
$this->lock->set('php', [
'version' => $this->config->get('platform')['php'] ?? (PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION),
]);
}

public function configureProject(Event $event)
{
if (!$this->downloader->isEnabled()) {
Expand Down Expand Up @@ -383,6 +390,8 @@ public function update(Event $event = null, $operations = [])

public function install(Event $event = null)
{
$this->updateAutoloadFile();

$rootDir = $this->options->get('root-dir');

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')) {
Expand Down Expand Up @@ -653,6 +662,36 @@ public function generateFlexId()
$this->updateComposerLock();
}

private function updateAutoloadFile()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be hooked into POST_AUTOLOAD_DUMP instead so that it is still applied when using dump-autoload ?

{
if (!$platform = $this->lock->get('php')['version'] ?? null) {
return;
}

$autoloadFile = $this->config->get('vendor-dir').'/autoload.php';

$code = file_get_contents($autoloadFile);
$code = substr($code, \strlen("<?php\n"));

if (false !== strpos($code, 'PHP_VERSION_ID')) {
return;
}

$platform = preg_replace('/[^-+.~_\w]/', '', $platform);
$version = sprintf('%d%02d%02d', ...explode('.', $platform.'.0.0'));

file_put_contents($autoloadFile, <<<EOPHP
<?php

if (\PHP_VERSION_ID < $version) {
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);
exit(1);
}
$code
EOPHP
);
}

private function fetchRecipes(): array
{
if (!$this->downloader->isEnabled()) {
Expand Down Expand Up @@ -839,7 +878,7 @@ public static function getSubscribedEvents(): array

return [
InstallerEvents::PRE_DEPENDENCIES_SOLVING => [['populateProvidersCacheDir', PHP_INT_MAX]],
InstallerEvents::POST_DEPENDENCIES_SOLVING => [['populateFilesCacheDir', PHP_INT_MAX]],
InstallerEvents::POST_DEPENDENCIES_SOLVING => [['populateFilesCacheDir', PHP_INT_MAX], ['lockPlatform']],
PackageEvents::PRE_PACKAGE_INSTALL => [['populateFilesCacheDir', ~PHP_INT_MAX]],
PackageEvents::PRE_PACKAGE_UPDATE => [['populateFilesCacheDir', ~PHP_INT_MAX]],
PackageEvents::POST_PACKAGE_INSTALL => __CLASS__ === self::class ? [['record'], ['checkForUpdate']] : 'record',
Expand Down