Skip to content

Commit

Permalink
Merge pull request #1391 from beheist/dev-bh-fix-flow-booting-with-di…
Browse files Browse the repository at this point in the history
…ffering-php-versions

BUGFIX: Flow CLI command warns of mismatching php version
  • Loading branch information
albe committed May 29, 2019
2 parents 2501208 + 1598292 commit ed39e54
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Neos.Flow/Classes/Core/Booting/Scripts.php
Expand Up @@ -25,6 +25,7 @@
use Neos\Flow\Error\Debugger;
use Neos\Flow\Error\ErrorHandler;
use Neos\Flow\Error\ProductionExceptionHandler;
use Neos\Flow\Exception;
use Neos\Flow\Log\Logger;
use Neos\Flow\Log\LoggerFactory;
use Neos\Flow\Log\SystemLoggerInterface;
Expand Down Expand Up @@ -700,6 +701,7 @@ protected static function buildSubprocessCommand($commandIdentifier, array $sett
/**
* @param array $settings The Neos.Flow settings
* @return string A command line command for PHP, which can be extended and then exec()uted
* @throws Exception
*/
public static function buildPhpCommand(array $settings)
{
Expand All @@ -712,6 +714,8 @@ public static function buildPhpCommand(array $settings)
$subRequestEnvironmentVariables = array_merge($subRequestEnvironmentVariables, $settings['core']['subRequestEnvironmentVariables']);
}

self::ensureCLISubrequestsUseCurrentlyRunningPhpBinary($settings['core']['phpBinaryPathAndFilename']);

$command = '';
foreach ($subRequestEnvironmentVariables as $argumentKey => $argumentValue) {
if (DIRECTORY_SEPARATOR === '/') {
Expand All @@ -736,9 +740,73 @@ public static function buildPhpCommand(array $settings)
$command .= ' -c ' . escapeshellarg($useIniFile);
}

self::ensureWebSubrequestsUseCurrentlyRunningPhpVersion($command);

return $command;
}

/**
* Compares the realpath of the configured PHP binary (if any) with the one flow was called with in a CLI request.
* This avoids config errors where users forget to set Neos.Flow.core.phpBinaryPathAndFilename in CLI.
*
* @param string phpBinaryPathAndFilename
* @throws Exception
*/
protected static function ensureCLISubrequestsUseCurrentlyRunningPhpBinary($phpBinaryPathAndFilename)
{
// Do nothing for non-CLI requests
if (PHP_SAPI !== 'cli') {
return;
}

// Resolve any symlinks that the configured php might be pointing to
$configuredPhpBinaryPathAndFilename = realpath($phpBinaryPathAndFilename);

// if the configured PHP binary is empty here, the file does not exist. We ignore that here because it is checked later in the script.
if ($configuredPhpBinaryPathAndFilename === false || strlen($configuredPhpBinaryPathAndFilename) === 0) {
return;
}

if (strcmp(PHP_BINARY, $configuredPhpBinaryPathAndFilename) !== 0) {
throw new Exception(sprintf('You are running the Flow CLI with a PHP binary different from the one Flow is configured to use internally. ' .
'Flow has been run with "%s", while the PHP version Flow is configured to use for subrequests is "%s". Make sure to configure Flow to ' .
'use the same PHP binary by setting the "Neos.Flow.core.phpBinaryPathAndFilename" configuration option to "%s". Flush the ' .
'caches by removing the folder Data/Temporary before running ./flow again.',
PHP_BINARY, $configuredPhpBinaryPathAndFilename, PHP_BINARY), 1536303119);
}
}

/**
* Compares the actual version of the configured PHP binary (if any) with the one flow was called with in a non-CLI request.
* This avoids config errors where users forget to set Neos.Flow.core.phpBinaryPathAndFilename in connection with a web
* server.
*
* @param string $phpCommand the completely build php string that is used to execute subrequests
* @throws Exception
*/
protected static function ensureWebSubrequestsUseCurrentlyRunningPhpVersion($phpCommand)
{
// Do nothing for CLI requests
if (PHP_SAPI === 'cli') {
return;
}

exec($phpCommand . ' -r "echo PHP_VERSION;"', $output, $result);

if ($result !== 0) {
return;
}

$configuredPHPVersion = $output[0];
if (array_slice(explode('.', $configuredPHPVersion), 0, 2) !== array_slice(explode('.', PHP_VERSION), 0, 2)) {
throw new Exception(sprintf('You are executing Neos/Flow with a PHP version different from the one Flow is configured to use internally. ' .
'Flow is running with with PHP "%s", while the PHP version Flow is configured to use for subrequests is "%s". Make sure to configure Flow to ' .
'use the same PHP version by setting the "Neos.Flow.core.phpBinaryPathAndFilename" configuration option to a PHP-CLI binary of the version ' .
'%s. Flush the caches by removing the folder Data/Temporary before executing Flow/Neos again.',
PHP_VERSION, $configuredPHPVersion, PHP_VERSION), 1536563428);
}
}

/**
* Check if the old fallback classloader should be used.
*
Expand Down

0 comments on commit ed39e54

Please sign in to comment.