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 930fcf9250
commit 843daf633d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 32 deletions

View File

@ -11,6 +11,7 @@ namespace Flarum\Extension\Exception;
use Exception;
use Flarum\Extension\Extension;
use Flarum\Extension\ExtensionManager;
/**
* This exception is thrown when someone attempts to disable an extension
@ -30,18 +31,6 @@ class DependentExtensionsException extends Exception
$this->extension = $extension;
$this->dependent_extensions = $dependent_extensions;
parent::__construct($extension->getId().' could not be disabled, because it is a dependency of: '.implode(', ', $this->getDependentExtensionIds()));
}
/**
* 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);
parent::__construct($extension->getTitle().' could not be disabled, because it is a dependency of: '.implode(', ', ExtensionManager::pluckTitles($dependent_extensions)));
}
}

View File

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

View File

@ -11,6 +11,7 @@ namespace Flarum\Extension\Exception;
use Exception;
use Flarum\Extension\Extension;
use Flarum\Extension\ExtensionManager;
/**
* This exception is thrown when someone attempts to enable an extension
@ -30,18 +31,6 @@ class MissingDependenciesException extends Exception
$this->extension = $extension;
$this->missing_dependencies = $missing_dependencies;
parent::__construct($extension->getId().' could not be enabled, because it depends on: '.implode(', ', $this->getMissingDependencyIds()));
}
/**
* 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);
parent::__construct($extension->getTitle().' could not be enabled, because it depends on: '.implode(', ', ExtensionManager::pluckTitles($missing_dependencies)));
}
}

View File

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

View File

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

View File

@ -114,7 +114,7 @@ class ExtensionManager
}
$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]);
}
/**
* 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) {
return in_array($extension->getId(), self::EXTENSION_WHITELIST);
})->sortBy(function (Extension $extension) {
return $extension->composerJsonAttribute('extra.flarum-extension.title');
return $extension->getTitle();
})->mapWithKeys(function (Extension $extension) {
return [$extension->getId() => $extension];
});