diff --git a/phpBB/includes/acp/acp_extensions.php b/phpBB/includes/acp/acp_extensions.php index 70fb763d8d..ba68158fd7 100644 --- a/phpBB/includes/acp/acp_extensions.php +++ b/phpBB/includes/acp/acp_extensions.php @@ -172,13 +172,11 @@ class acp_extensions /** @var \phpbb\composer\manager $composer_manager */ $composer_manager = $phpbb_container->get('ext.composer.manager'); - $this->request->enable_super_globals(); $managed_packages = []; if ($composer_manager->check_requirements()) { $managed_packages = $composer_manager->get_managed_packages(); } - $this->request->disable_super_globals(); $this->list_enabled_exts($phpbb_extension_manager, $managed_packages); $this->list_disabled_exts($phpbb_extension_manager, $managed_packages); @@ -475,9 +473,7 @@ class acp_extensions try { - $this->request->enable_super_globals(); $composer_manager->install((array) $extension, $composer_io); - $this->request->disable_super_globals(); } catch (\phpbb\exception\runtime_exception $e) { @@ -514,9 +510,7 @@ class acp_extensions try { - $this->request->enable_super_globals(); $composer_manager->remove((array) $extension, $composer_io); - $this->request->disable_super_globals(); } catch (\phpbb\exception\runtime_exception $e) { @@ -553,9 +547,7 @@ class acp_extensions try { - $this->request->enable_super_globals(); $composer_manager->update((array) $extension, $composer_io); - $this->request->disable_super_globals(); } catch (\phpbb\exception\runtime_exception $e) { @@ -592,9 +584,7 @@ class acp_extensions try { - $this->request->enable_super_globals(); $composer_manager->start_managing($extension, $composer_io); - $this->request->disable_super_globals(); } catch (\phpbb\exception\runtime_exception $e) { @@ -684,10 +674,8 @@ class acp_extensions $start = $this->request->variable('start', 0); $base_url = $this->u_action; - $this->request->enable_super_globals(); $available_extensions = $manager->get_available_packages(); $managed_packages = $manager->get_managed_packages(); - $this->request->disable_super_globals(); $extensions = array_slice($available_extensions, $start, 20); diff --git a/phpBB/phpbb/composer/installer.php b/phpBB/phpbb/composer/installer.php index f6f2f1c83a..a5153d5f05 100644 --- a/phpBB/phpbb/composer/installer.php +++ b/phpBB/phpbb/composer/installer.php @@ -27,6 +27,7 @@ use phpbb\composer\io\null_io; use phpbb\config\config; use phpbb\exception\runtime_exception; use phpbb\filesystem\filesystem; +use phpbb\request\request; use Seld\JsonLint\ParsingException; /** @@ -76,12 +77,18 @@ class installer */ private $ext_json_file_backup; + /** + * @var request phpBB request object + */ + private $request; + /** * @param string $root_path phpBB root path * @param filesystem $filesystem Filesystem object + * @param request $request phpBB request object * @param config $config Config object */ - public function __construct($root_path, filesystem $filesystem, config $config = null) + public function __construct($root_path, filesystem $filesystem, request $request, config $config = null) { if ($config) { @@ -99,6 +106,7 @@ class installer } $this->root_path = $root_path; + $this->request = $request; putenv('COMPOSER_HOME=' . $filesystem->realpath($root_path) . '/store/composer'); } @@ -115,19 +123,9 @@ class installer */ public function install(array $packages, $whitelist, IOInterface $io = null) { - // The composer installers works with a path relative to the current directory - $this->move_to_root(); - - try - { + $this->wrap(function() use ($packages, $whitelist, $io) { $this->do_install($packages, $whitelist, $io); - $this->restore_cwd(); - } - catch (runtime_exception $e) - { - $this->restore_cwd(); - throw $e; - } + }); } /** @@ -178,7 +176,6 @@ class installer catch (\Exception $e) { $this->restore_ext_json_file(); - $this->restore_cwd(); throw new runtime_exception('COMPOSER_CANNOT_INSTALL', [], $e); } @@ -186,7 +183,6 @@ class installer if ($result !== 0) { $this->restore_ext_json_file(); - $this->restore_cwd(); throw new runtime_exception($io->get_composer_error(), []); } @@ -203,21 +199,9 @@ class installer */ public function get_installed_packages($types) { - // The composer installers works with a path relative to the current directory - $this->move_to_root(); - - try - { - $result = $this->do_get_installed_packages($types); - $this->restore_cwd(); - } - catch (runtime_exception $e) - { - $this->restore_cwd(); - throw $e; - } - - return $result; + return $this->wrap(function() use ($types) { + return $this->do_get_installed_packages($types); + }); } /** @@ -275,21 +259,9 @@ class installer */ public function get_available_packages($type) { - // The composer installers works with a path relative to the current directory - $this->move_to_root(); - - try - { - $result = $this->do_get_available_packages($type); - $this->restore_cwd(); - } - catch (runtime_exception $e) - { - $this->restore_cwd(); - throw $e; - } - - return $result; + return $this->wrap(function() use ($type) { + return $this->do_get_available_packages($type); + }); } /** @@ -704,4 +676,34 @@ class installer $this->original_cwd = null; } } + + /** + * Wraps a callable in order to adjust the context needed by composer + * + * @param callable $callable + * + * @return mixed + */ + protected function wrap(callable $callable) + { + // The composer installers works with a path relative to the current directory + $this->move_to_root(); + + // The composer installers uses some super globals + $super_globals = $this->request->super_globals_disabled(); + $this->request->enable_super_globals(); + + try + { + return $callable(); + } + finally + { + $this->restore_cwd(); + + if ($super_globals) { + $this->request->disable_super_globals(); + } + } + } }