From 81276d6d5af6f6533f98e1a4a42761cc57f6cff0 Mon Sep 17 00:00:00 2001 From: mcop1 <89011527+mcop1@users.noreply.github.com> Date: Tue, 17 Jan 2023 12:22:13 +0100 Subject: [PATCH] Revert "Pimcore\Tool\Console: Remove deprecated methods" (#14077) * Revert "Pimcore\Tool\Console: Remove deprecated methods (#14024)" This reverts commit 7d15654af6cd501ed3254b77c38f47e5527209ec. * Update lib/Tool/Console.php Co-authored-by: Sebastian Blank Co-authored-by: Sebastian Blank --- .../09_Upgrade_Notes/README.md | 1 - lib/Tool/Console.php | 141 ++++++++++++++++++ phpstan-baseline.neon | 5 + 3 files changed, 146 insertions(+), 1 deletion(-) diff --git a/doc/Development_Documentation/23_Installation_and_Upgrade/09_Upgrade_Notes/README.md b/doc/Development_Documentation/23_Installation_and_Upgrade/09_Upgrade_Notes/README.md index 5a228f26d97..305671aa475 100644 --- a/doc/Development_Documentation/23_Installation_and_Upgrade/09_Upgrade_Notes/README.md +++ b/doc/Development_Documentation/23_Installation_and_Upgrade/09_Upgrade_Notes/README.md @@ -101,7 +101,6 @@ Please make sure to set your preferred storage location ***before*** migration. - Removed BruteforceProtection - Removed PreAuthenticatedAdminToken - [Logger] Bumped `monolog/monolog` to [^3.2](https://github.com/Seldaek/monolog/blob/main/UPGRADE.md#300) and `symfony/monolog-bundle` to [^3.8](https://github.com/symfony/monolog-bundle/blob/master/CHANGELOG.md#380-2022-05-10) (which adds support for monolog v3). Please adapt your custom implementation accordingly eg. log records are now `LogRecord` Objects instead of array. -- `Pimcore\Tool\Console`: Deprecated methods `getSystemEnvironment()`, `execInBackground()` and `runPhpScriptInBackground()` have been removed, use `Symfony\Component\Process\Process` instead where possible. For long running background tasks (which should run even when parent process has exited), switch to a queue implementation. - [Ecommerce] The constructor of the indexing services (eg. `DefaultMysql`, `AbstractElasticSearch`) and related workers were changed to support the injection of monolog logger, please adapt your custom implementation. - [Bundles] - Removed support for loading bundles through `extensions.php`. diff --git a/lib/Tool/Console.php b/lib/Tool/Console.php index baee494ef7f..47577006b6b 100644 --- a/lib/Tool/Console.php +++ b/lib/Tool/Console.php @@ -24,8 +24,32 @@ final class Console { + private static ?string $systemEnvironment = null; + protected static array $executableCache = []; + /** + * @deprecated since v.6.9. + * + * @static + * + * @return string "windows" or "unix" + */ + public static function getSystemEnvironment(): string + { + if (self::$systemEnvironment == null) { + if (stripos(php_uname('s'), 'windows') !== false) { + self::$systemEnvironment = 'windows'; + } elseif (stripos(php_uname('s'), 'darwin') !== false) { + self::$systemEnvironment = 'darwin'; + } else { + self::$systemEnvironment = 'unix'; + } + } + + return self::$systemEnvironment; + } + /** * @return ($throwException is true ? string : string|false) * @@ -184,6 +208,123 @@ public static function runPhpScript(string $script, array $arguments = [], strin return $process->getOutput(); } + /** + * @param string $script + * @param array $arguments + * @param string|null $outputFile + * + * @return int + * + * @deprecated since v6.9. For long running background tasks switch to a queue implementation. + */ + public static function runPhpScriptInBackground(string $script, array $arguments = [], string $outputFile = null): int + { + $cmd = self::buildPhpScriptCmd($script, $arguments); + $process = new Process($cmd); + $commandLine = $process->getCommandLine(); + + return self::execInBackground($commandLine, $outputFile); + } + + /** + * @param string $cmd + * @param string|null $outputFile + * + * @return int + * + * @deprecated since v.6.9. Use Symfony\Component\Process\Process instead. For long running background tasks use queues. + * + * @static + */ + public static function execInBackground(string $cmd, string $outputFile = null): int + { + // windows systems + if (self::getSystemEnvironment() == 'windows') { + return self::execInBackgroundWindows($cmd, $outputFile); + } elseif (self::getSystemEnvironment() == 'darwin') { + return self::execInBackgroundUnix($cmd, $outputFile, false); + } else { + return self::execInBackgroundUnix($cmd, $outputFile); + } + } + + /** + * @param string $cmd + * @param ?string $outputFile + * @param bool $useNohup + * + * @return int + * + * @deprecated since v.6.9. For long running background tasks use queues. + * + * @static + */ + protected static function execInBackgroundUnix(string $cmd, ?string $outputFile, bool $useNohup = true): int + { + if (!$outputFile) { + $outputFile = '/dev/null'; + } + + $nice = (string) self::getExecutable('nice'); + if ($nice) { + $nice .= ' -n 19 '; + } + + if ($useNohup) { + $nohup = (string) self::getExecutable('nohup'); + if ($nohup) { + $nohup .= ' '; + } + } else { + $nohup = ''; + } + + /** + * mod_php seems to lose the environment variables if we do not set them manually before the child process is started + */ + if (strpos(php_sapi_name(), 'apache') !== false) { + foreach (['APP_ENV'] as $envVarName) { + if ($envValue = $_SERVER[$envVarName] ?? $_SERVER['REDIRECT_' . $envVarName] ?? null) { + putenv($envVarName . '='.$envValue); + } + } + } + + $commandWrapped = $nohup . $nice . $cmd . ' > '. $outputFile .' 2>&1 & echo $!'; + Logger::debug('Executing command `' . $commandWrapped . '´ on the current shell in background'); + $pid = shell_exec($commandWrapped); + + Logger::debug('Process started with PID ' . $pid); + + return (int)$pid; + } + + /** + * @param string $cmd + * @param string $outputFile + * + * @return int + * + * @deprecated since v.6.9. For long-running background tasks use queues. + * + * @static + */ + protected static function execInBackgroundWindows(string $cmd, string $outputFile): int + { + if (!$outputFile) { + $outputFile = 'NUL'; + } + + $commandWrapped = 'cmd /c ' . $cmd . ' > '. $outputFile . ' 2>&1'; + Logger::debug('Executing command `' . $commandWrapped . '´ on the current shell in background'); + + $WshShell = new \COM('WScript.Shell'); + $WshShell->Run($commandWrapped, 0, false); + Logger::debug('Process started - returning the PID is not supported on Windows Systems'); + + return 0; + } + /** * @param string[]|string $cmd * diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 6e17fd2bf1d..e3fa42ea528 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -105,6 +105,11 @@ parameters: count: 1 path: lib/Targeting/ConditionMatcher.php + - + message: "#^Call to an undefined method COM\\:\\:Run\\(\\)\\.$#" + count: 1 + path: lib/Tool/Console.php + - message: "#^Dead catch \\- Throwable is never thrown in the try block\\.$#" count: 1