Use extension names instead of IDs when erroring on enable/disable reqs (#2563)

This commit is contained in:
David Sevilla Martín 2021-01-28 19:41:04 -05:00 committed by GitHub
parent 9ff85d661e
commit e03504a7f2
7 changed files with 33 additions and 32 deletions

View File

@ -11,6 +11,7 @@ namespace Flarum\Extension\Exception;
use Exception; use Exception;
use Flarum\Extension\Extension; use Flarum\Extension\Extension;
use Flarum\Extension\ExtensionManager;
/** /**
* This exception is thrown when someone attempts to disable an extension * This exception is thrown when someone attempts to disable an extension
@ -30,18 +31,6 @@ class DependentExtensionsException extends Exception
$this->extension = $extension; $this->extension = $extension;
$this->dependent_extensions = $dependent_extensions; $this->dependent_extensions = $dependent_extensions;
parent::__construct($extension->getId().' could not be disabled, because it is a dependency of: '.implode(', ', $this->getDependentExtensionIds())); parent::__construct($extension->getTitle().' could not be disabled, because it is a dependency of: '.implode(', ', ExtensionManager::pluckTitles($dependent_extensions)));
}
/**
* Get array of IDs for extensions that depend on this extension.
*
* @return array
*/
public function getDependentExtensionIds()
{
return array_map(function (Extension $extension) {
return $extension->getId();
}, $this->dependent_extensions);
} }
} }

View File

@ -9,6 +9,7 @@
namespace Flarum\Extension\Exception; namespace Flarum\Extension\Exception;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\ErrorHandling\HandledError; use Flarum\Foundation\ErrorHandling\HandledError;
class DependentExtensionsExceptionHandler class DependentExtensionsExceptionHandler
@ -26,8 +27,8 @@ class DependentExtensionsExceptionHandler
{ {
return [ return [
[ [
'extension' => $e->extension->getId(), 'extension' => $e->extension->getTitle(),
'extensions' => $e->getDependentExtensionIds(), 'extensions' => ExtensionManager::pluckTitles($e->dependent_extensions),
] ]
]; ];
} }

View File

@ -11,6 +11,7 @@ namespace Flarum\Extension\Exception;
use Exception; use Exception;
use Flarum\Extension\Extension; use Flarum\Extension\Extension;
use Flarum\Extension\ExtensionManager;
/** /**
* This exception is thrown when someone attempts to enable an extension * This exception is thrown when someone attempts to enable an extension
@ -30,18 +31,6 @@ class MissingDependenciesException extends Exception
$this->extension = $extension; $this->extension = $extension;
$this->missing_dependencies = $missing_dependencies; $this->missing_dependencies = $missing_dependencies;
parent::__construct($extension->getId().' could not be enabled, because it depends on: '.implode(', ', $this->getMissingDependencyIds())); parent::__construct($extension->getTitle().' could not be enabled, because it depends on: '.implode(', ', ExtensionManager::pluckTitles($missing_dependencies)));
}
/**
* Get array of IDs for missing (disabled) extensions that this extension depends on.
*
* @return array
*/
public function getMissingDependencyIds()
{
return array_map(function (Extension $extension) {
return $extension->getId();
}, $this->missing_dependencies);
} }
} }

View File

@ -9,6 +9,7 @@
namespace Flarum\Extension\Exception; namespace Flarum\Extension\Exception;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\ErrorHandling\HandledError; use Flarum\Foundation\ErrorHandling\HandledError;
class MissingDependenciesExceptionHandler class MissingDependenciesExceptionHandler
@ -26,8 +27,8 @@ class MissingDependenciesExceptionHandler
{ {
return [ return [
[ [
'extension' => $e->extension->getId(), 'extension' => $e->extension->getTitle(),
'extensions' => $e->getMissingDependencyIds(), 'extensions' => ExtensionManager::pluckTitles($e->missing_dependencies),
] ]
]; ];
} }

View File

@ -278,6 +278,14 @@ class Extension implements Arrayable
return $this->id; return $this->id;
} }
/**
* @return string
*/
public function getTitle()
{
return $this->composerJsonAttribute('extra.flarum-extension.title');
}
/** /**
* @return string * @return string
*/ */

View File

@ -114,7 +114,7 @@ class ExtensionManager
} }
$this->extensions = $extensions->sortBy(function ($extension, $name) { $this->extensions = $extensions->sortBy(function ($extension, $name) {
return $extension->composerJsonAttribute('extra.flarum-extension.title'); return $extension->getTitle();
}); });
} }
@ -369,4 +369,17 @@ class ExtensionManager
return isset($enabled[$extension]); return isset($enabled[$extension]);
} }
/**
* Returns the titles of the extensions passed.
*
* @param array $exts
* @return string[]
*/
public static function pluckTitles(array $exts)
{
return array_map(function (Extension $extension) {
return $extension->getTitle();
}, $exts);
}
} }

View File

@ -111,7 +111,7 @@ class EnableBundledExtensions implements Step
})->filter(function (Extension $extension) { })->filter(function (Extension $extension) {
return in_array($extension->getId(), self::EXTENSION_WHITELIST); return in_array($extension->getId(), self::EXTENSION_WHITELIST);
})->sortBy(function (Extension $extension) { })->sortBy(function (Extension $extension) {
return $extension->composerJsonAttribute('extra.flarum-extension.title'); return $extension->getTitle();
})->mapWithKeys(function (Extension $extension) { })->mapWithKeys(function (Extension $extension) {
return [$extension->getId() => $extension]; return [$extension->getId() => $extension];
}); });