[ticket/11150] Better pre/post action handling, restore ext.json in case of err

PHPBB3-11150
This commit is contained in:
Tristan Darricau 2015-09-14 16:20:33 +02:00 committed by Tristan Darricau
parent 51916def9c
commit d6618397bf
No known key found for this signature in database
GPG key ID: 817043C2E29DB881
5 changed files with 243 additions and 41 deletions

View file

@ -38,6 +38,11 @@ class extension_manager extends manager
*/ */
protected $filesystem; protected $filesystem;
/**
* @var array
*/
private $enabled_extensions;
/** /**
* @param installer $installer Installer object * @param installer $installer Installer object
* @param driver_interface $cache Cache object * @param driver_interface $cache Cache object
@ -57,29 +62,112 @@ class extension_manager extends manager
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function install(array $packages, IOInterface $io = null) public function pre_install(array $packages, IOInterface $io = null)
{ {
$packages = $this->normalize_version($packages);
$already_managed = array_intersect(array_keys($this->get_managed_packages()), array_keys($packages));
if (count($already_managed) !== 0)
{
throw new runtime_exception($this->exception_prefix, 'ALREADY_INSTALLED', [implode('|', $already_managed)]);
}
$installed_manually = array_intersect(array_keys($this->extension_manager->all_available()), array_keys($packages)); $installed_manually = array_intersect(array_keys($this->extension_manager->all_available()), array_keys($packages));
if (count($installed_manually) !== 0) if (count($installed_manually) !== 0)
{ {
throw new runtime_exception($this->exception_prefix, 'ALREADY_INSTALLED_MANUALLY', [implode('|', array_keys($installed_manually))]); throw new runtime_exception($this->exception_prefix, 'ALREADY_INSTALLED_MANUALLY', [implode('|', array_keys($installed_manually))]);
} }
$this->do_install($packages, $io);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function start_managing($package) protected function pre_update(array $packages, IOInterface $io = null)
{
$io->writeError('DISABLING_EXTENSIONS', true, 1);
$this->enabled_extensions = [];
foreach ($packages as $package)
{
try
{
if ($this->extension_manager->is_enabled($package))
{
$this->enabled_extensions[] = $package;
$this->extension_manager->disable($package);
}
}
catch (\phpbb\exception\runtime_exception $e)
{
$io->writeError([$e->getMessage(), $e->get_parameters()], true, 4);
}
catch (\Exception $e)
{
$io->writeError($e->getMessage(), true, 4);
}
}
}
/**
* {@inheritdoc}
*/
protected function post_update(array $packages, IOInterface $io = null)
{
$io->writeError('ENABLING_EXTENSIONS', true, 1);
foreach ($this->enabled_extensions as $package)
{
try
{
$this->extension_manager->enable($package);
}
catch (\phpbb\exception\runtime_exception $e)
{
$io->writeError([$e->getMessage(), $e->get_parameters()], true, 4);
}
catch (\Exception $e)
{
$io->writeError($e->getMessage(), true, 4);
}
}
}
/**
* {@inheritdoc}
*/
public function remove(array $packages, IOInterface $io = null)
{
$packages = $this->normalize_version($packages);
$not_installed = array_diff(array_keys($packages), array_keys($this->extension_manager->all_available()));
if (count($not_installed) !== 0)
{
throw new runtime_exception($this->exception_prefix, 'NOT_INSTALLED', [implode('|', array_keys($not_installed))]);
}
parent::remove($packages, $io);
}
/**
* {@inheritdoc}
*/
public function pre_remove(array $packages, IOInterface $io = null)
{
$io->writeError('DISABLING_EXTENSIONS', true, 1);
foreach ($packages as $package)
{
try
{
if ($this->extension_manager->is_enabled($package))
{
$this->extension_manager->disable($package);
}
}
catch (\phpbb\exception\runtime_exception $e)
{
$io->writeError([$e->getMessage(), $e->get_parameters()], true, 4);
}
catch (\Exception $e)
{
$io->writeError($e->getMessage(), true, 4);
}
}
}
/**
* {@inheritdoc}
*/
public function start_managing($package, $io)
{ {
if (!$this->extension_manager->is_available($package)) if (!$this->extension_manager->is_available($package))
{ {
@ -95,6 +183,7 @@ class extension_manager extends manager
if ($this->extension_manager->is_enabled($package)) if ($this->extension_manager->is_enabled($package))
{ {
$enabled = true; $enabled = true;
$io->writeError('DISABLING_EXTENSION', true, 1);
$this->extension_manager->disable($package); $this->extension_manager->disable($package);
} }
@ -129,7 +218,8 @@ class extension_manager extends manager
{ {
try try
{ {
$this->extension_manager->enable($package); $io->writeError('ENABLING_EXTENSION', true, 1);
$this->extension_manager->enabling($package);
} }
catch (\Exception $e) catch (\Exception $e)
{ {

View file

@ -60,10 +60,15 @@ class installer
protected $root_path; protected $root_path;
/** /**
* @var string Store the original working directory in case it has been changed through move_to_root() * @var string Stores the original working directory in case it has been changed through move_to_root()
*/ */
private $original_cwd; private $original_cwd;
/**
* @var array Stores the content of the ext json file before generate_ext_json_file() overrides it
*/
private $ext_json_file_backup;
/** /**
* @param \phpbb\config\config $config Config object * @param \phpbb\config\config $config Config object
* @param string $root_path phpBB root path * @param string $root_path phpBB root path
@ -157,12 +162,14 @@ class installer
} }
catch (\Exception $e) catch (\Exception $e)
{ {
$this->restore_ext_json_file();
$this->restore_cwd(); $this->restore_cwd();
throw new runtime_exception('COMPOSER_CANNOT_INSTALL', [], $e); throw new runtime_exception('COMPOSER_CANNOT_INSTALL', [], $e);
} }
if ($result !== 0) if ($result !== 0)
{ {
$this->restore_ext_json_file();
$this->restore_cwd(); $this->restore_cwd();
throw new runtime_exception($io->get_composer_error(), []); throw new runtime_exception($io->get_composer_error(), []);
} }
@ -444,8 +451,31 @@ class installer
], ],
]; ];
$this->ext_json_file_backup = null;
$json_file = new JsonFile($this->get_composer_ext_json_filename()); $json_file = new JsonFile($this->get_composer_ext_json_filename());
$ext_json_file_backup = $json_file->read();
$json_file->write($ext_json_data); $json_file->write($ext_json_data);
$this->ext_json_file_backup = $ext_json_file_backup;
}
/**
* Restore the json file overridden by generate_ext_json_file()
*/
protected function restore_ext_json_file()
{
if ($this->ext_json_file_backup)
{
try
{
$json_file = new JsonFile($this->get_composer_ext_json_filename());
$json_file->write($this->ext_json_file_backup);
}
catch (\Exception $e)
{
}
$this->ext_json_file_backup = null;
}
} }
/** /**

View file

@ -37,10 +37,22 @@ trait translate_composer_trait
$translated_messages = []; $translated_messages = [];
foreach ($messages as $message) foreach ($messages as $message)
{
$level = 0;
if (is_array($message))
{
$lang_key = $message[0];
$parameters = $message[1];
if (count($message) > 2)
{
$level = $message[2];
}
}
else
{ {
$lang_key = $message; $lang_key = $message;
$parameters = []; $parameters = [];
$level = 0; }
$message = trim($this->strip_format($message), "\n\r"); $message = trim($this->strip_format($message), "\n\r");
@ -50,10 +62,6 @@ trait translate_composer_trait
$lang_key = 'COMPOSER_DELETING'; $lang_key = 'COMPOSER_DELETING';
$parameters = [$elements[1]]; $parameters = [$elements[1]];
} }
else
{
dump('WRITE | ' . $message);
}
//$translated_message = $this->language->lang_array($lang_key, $parameters); //$translated_message = $this->language->lang_array($lang_key, $parameters);
$translated_message = call_user_func_array([$this->language, 'lang'], array_merge((array)$lang_key, $parameters)); $translated_message = call_user_func_array([$this->language, 'lang'], array_merge((array)$lang_key, $parameters));
@ -89,10 +97,22 @@ trait translate_composer_trait
$translated_messages = []; $translated_messages = [];
foreach ($messages as $message) foreach ($messages as $message)
{
$level = 0;
if (is_array($message))
{
$lang_key = $message[0];
$parameters = $message[1];
if (count($message) > 2)
{
$level = $message[2];
}
}
else
{ {
$lang_key = $message; $lang_key = $message;
$parameters = []; $parameters = [];
$level = 0; }
$message = trim($this->strip_format($message), "\n\r"); $message = trim($this->strip_format($message), "\n\r");
@ -165,13 +185,8 @@ trait translate_composer_trait
{ {
continue; continue;
} }
else
{
dump('WRITE ERROR | ' . $message);
}
//$translated_message = $this->language->lang_array($lang_key, $parameters); $translated_message = $this->language->lang_array($lang_key, $parameters);
$translated_message = call_user_func_array([$this->language, 'lang'], array_merge((array)$lang_key, $parameters));
switch ($level) switch ($level)
{ {

View file

@ -84,25 +84,40 @@ class manager implements manager_interface
throw new runtime_exception($this->exception_prefix, 'ALREADY_INSTALLED', [implode('|', $already_managed)]); throw new runtime_exception($this->exception_prefix, 'ALREADY_INSTALLED', [implode('|', $already_managed)]);
} }
$this->do_install($packages, $io); $this->pre_install($packages, $io);
}
/**
* Really install the packages.
*
* @param array $packages Packages to install.
* @param IOInterface $io IO object used for the output
*/
protected function do_install($packages, IOInterface $io = null)
{
$managed_packages = array_merge($this->get_all_managed_packages(), $packages); $managed_packages = array_merge($this->get_all_managed_packages(), $packages);
ksort($managed_packages); ksort($managed_packages);
$this->installer->install($managed_packages, array_keys($packages), $io); $this->installer->install($managed_packages, array_keys($packages), $io);
$this->post_install($packages, $io);
$this->managed_packages = null; $this->managed_packages = null;
} }
/**
* Hook called before installing the packages
*
* @param array $packages Packages to update.
* Each entry may be a name or an array associating a version constraint to a name
* @param IOInterface $io IO object used for the output
*/
protected function pre_install(array $packages, IOInterface $io = null)
{
}
/**
* Hook called after installing the packages
*
* @param array $packages Packages to update.
* Each entry may be a name or an array associating a version constraint to a name
* @param IOInterface $io IO object used for the output
*/
protected function post_install(array $packages, IOInterface $io = null)
{
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -110,17 +125,42 @@ class manager implements manager_interface
{ {
$packages = $this->normalize_version($packages); $packages = $this->normalize_version($packages);
// TODO: if the extension is already enabled, we should disabled and re-enable it
$not_managed = array_diff_key($packages, $this->get_managed_packages()); $not_managed = array_diff_key($packages, $this->get_managed_packages());
if (count($not_managed) !== 0) if (count($not_managed) !== 0)
{ {
throw new runtime_exception($this->exception_prefix, 'NOT_MANAGED', [implode('|', array_keys($not_managed))]); throw new runtime_exception($this->exception_prefix, 'NOT_MANAGED', [implode('|', array_keys($not_managed))]);
} }
$this->pre_update($packages, $io);
$managed_packages = array_merge($this->get_all_managed_packages(), $packages); $managed_packages = array_merge($this->get_all_managed_packages(), $packages);
ksort($managed_packages); ksort($managed_packages);
$this->installer->install($managed_packages, array_keys($packages), $io); $this->installer->install($managed_packages, array_keys($packages), $io);
$this->post_update($packages, $io);
}
/**
* Hook called before updating the packages
*
* @param array $packages Packages to update.
* Each entry may be a name or an array associating a version constraint to a name
* @param IOInterface $io IO object used for the output
*/
protected function pre_update(array $packages, IOInterface $io = null)
{
}
/**
* Hook called after updating the packages
*
* @param array $packages Packages to update.
* Each entry may be a name or an array associating a version constraint to a name
* @param IOInterface $io IO object used for the output
*/
protected function post_update(array $packages, IOInterface $io = null)
{
} }
/** /**
@ -137,14 +177,40 @@ class manager implements manager_interface
throw new runtime_exception($this->exception_prefix, 'NOT_MANAGED', [implode('|', array_keys($not_managed))]); throw new runtime_exception($this->exception_prefix, 'NOT_MANAGED', [implode('|', array_keys($not_managed))]);
} }
$this->pre_remove($packages, $io);
$managed_packages = array_diff_key($this->get_all_managed_packages(), $packages); $managed_packages = array_diff_key($this->get_all_managed_packages(), $packages);
ksort($managed_packages); ksort($managed_packages);
$this->installer->install($managed_packages, array_keys($packages), $io); $this->installer->install($managed_packages, array_keys($packages), $io);
$this->post_remove($packages, $io);
$this->managed_packages = null; $this->managed_packages = null;
} }
/**
* Hook called before removing the packages
*
* @param array $packages Packages to update.
* Each entry may be a name or an array associating a version constraint to a name
* @param IOInterface $io IO object used for the output
*/
protected function pre_remove(array $packages, IOInterface $io = null)
{
}
/**
* Hook called after removing the packages
*
* @param array $packages Packages to update.
* Each entry may be a name or an array associating a version constraint to a name
* @param IOInterface $io IO object used for the output
*/
protected function post_remove(array $packages, IOInterface $io = null)
{
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -200,7 +266,7 @@ class manager implements manager_interface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function start_managing($package) public function start_managing($package, $io)
{ {
throw new \phpbb\exception\runtime_exception('COMPOSER_UNSUPPORTED_OPERATION', (array) $this->package_type); throw new \phpbb\exception\runtime_exception('COMPOSER_UNSUPPORTED_OPERATION', (array) $this->package_type);
} }

View file

@ -84,8 +84,9 @@ interface manager_interface
* Remove a package installed manually and reinstall it using composer. * Remove a package installed manually and reinstall it using composer.
* *
* @param string $package Package to manage * @param string $package Package to manage
* @param IOInterface $io IO object used for the output
* *
* @throws runtime_exception * @throws runtime_exception
*/ */
public function start_managing($package); public function start_managing($package, $io);
} }