diff --git a/src/Illuminate/Foundation/ComposerScripts.php b/src/Illuminate/Foundation/ComposerScripts.php index 1c600f3df964..172cab3d1745 100644 --- a/src/Illuminate/Foundation/ComposerScripts.php +++ b/src/Illuminate/Foundation/ComposerScripts.php @@ -49,6 +49,8 @@ public static function postAutoloadDump(Event $event) { require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php'; + static::setDeferredInstallerHookEnvironmentVariable(); + static::clearCompiled(); } @@ -113,4 +115,24 @@ protected static function clearCompiled() @unlink($packagesPath); } } + + /** + * Ensure deferred installer hooks are inherited by Composer child processes. + * + * @return void + */ + protected static function setDeferredInstallerHookEnvironmentVariable() + { + $variable = 'LARAVEL_INSTALLER_DEFER_HOOKS'; + + if (getenv($variable) !== false) { + return; + } + + if (array_key_exists($variable, $_ENV)) { + putenv($variable.'='.$_ENV[$variable]); + } elseif (array_key_exists($variable, $_SERVER)) { + putenv($variable.'='.$_SERVER[$variable]); + } + } } diff --git a/tests/Foundation/FoundationComposerScriptsTest.php b/tests/Foundation/FoundationComposerScriptsTest.php new file mode 100644 index 000000000000..d126f7280818 --- /dev/null +++ b/tests/Foundation/FoundationComposerScriptsTest.php @@ -0,0 +1,55 @@ +assertSame('1', getenv('LARAVEL_INSTALLER_DEFER_HOOKS')); + } + + public function testItSetsDeferredInstallerHookEnvironmentVariableFromServer(): void + { + $_SERVER['LARAVEL_INSTALLER_DEFER_HOOKS'] = '1'; + putenv('LARAVEL_INSTALLER_DEFER_HOOKS'); + + ComposerScriptsTestProxy::setDeferredInstallerHookEnvironmentVariable(); + + $this->assertSame('1', getenv('LARAVEL_INSTALLER_DEFER_HOOKS')); + } + + public function testItDoesNotOverwriteExistingDeferredInstallerHookEnvironmentVariable(): void + { + $_ENV['LARAVEL_INSTALLER_DEFER_HOOKS'] = '1'; + putenv('LARAVEL_INSTALLER_DEFER_HOOKS=0'); + + ComposerScriptsTestProxy::setDeferredInstallerHookEnvironmentVariable(); + + $this->assertSame('0', getenv('LARAVEL_INSTALLER_DEFER_HOOKS')); + } +} + +class ComposerScriptsTestProxy extends ComposerScripts +{ + public static function setDeferredInstallerHookEnvironmentVariable(): void + { + parent::setDeferredInstallerHookEnvironmentVariable(); + } +}