Skip to content

Commit

Permalink
Use extension names instead of IDs when erroring on enable/disable re…
Browse files Browse the repository at this point in the history
…qs (#2563)
  • Loading branch information
dsevillamartin committed Jan 29, 2021
1 parent 930fcf9 commit 843daf6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 32 deletions.
15 changes: 2 additions & 13 deletions src/Extension/Exception/DependentExtensionsException.php
Expand Up @@ -11,6 +11,7 @@

use Exception;
use Flarum\Extension\Extension;
use Flarum\Extension\ExtensionManager;

/**
* This exception is thrown when someone attempts to disable an extension
Expand All @@ -30,18 +31,6 @@ public function __construct(Extension $extension, array $dependent_extensions)
$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)));
}
}
Expand Up @@ -9,6 +9,7 @@

namespace Flarum\Extension\Exception;

use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\ErrorHandling\HandledError;

class DependentExtensionsExceptionHandler
Expand All @@ -26,8 +27,8 @@ protected function errorDetails(DependentExtensionsException $e): array
{
return [
[
'extension' => $e->extension->getId(),
'extensions' => $e->getDependentExtensionIds(),
'extension' => $e->extension->getTitle(),
'extensions' => ExtensionManager::pluckTitles($e->dependent_extensions),
]
];
}
Expand Down
15 changes: 2 additions & 13 deletions src/Extension/Exception/MissingDependenciesException.php
Expand Up @@ -11,6 +11,7 @@

use Exception;
use Flarum\Extension\Extension;
use Flarum\Extension\ExtensionManager;

/**
* This exception is thrown when someone attempts to enable an extension
Expand All @@ -30,18 +31,6 @@ public function __construct(Extension $extension, array $missing_dependencies =
$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)));
}
}
Expand Up @@ -9,6 +9,7 @@

namespace Flarum\Extension\Exception;

use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\ErrorHandling\HandledError;

class MissingDependenciesExceptionHandler
Expand All @@ -26,8 +27,8 @@ protected function errorDetails(MissingDependenciesException $e): array
{
return [
[
'extension' => $e->extension->getId(),
'extensions' => $e->getMissingDependencyIds(),
'extension' => $e->extension->getTitle(),
'extensions' => ExtensionManager::pluckTitles($e->missing_dependencies),
]
];
}
Expand Down
8 changes: 8 additions & 0 deletions src/Extension/Extension.php
Expand Up @@ -278,6 +278,14 @@ public function getId()
return $this->id;
}

/**
* @return string
*/
public function getTitle()
{
return $this->composerJsonAttribute('extra.flarum-extension.title');
}

/**
* @return string
*/
Expand Down
15 changes: 14 additions & 1 deletion src/Extension/ExtensionManager.php
Expand Up @@ -114,7 +114,7 @@ public function getExtensions()
}

$this->extensions = $extensions->sortBy(function ($extension, $name) {
return $extension->composerJsonAttribute('extra.flarum-extension.title');
return $extension->getTitle();
});
}

Expand Down Expand Up @@ -369,4 +369,17 @@ public function isEnabled($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);
}
}
2 changes: 1 addition & 1 deletion src/Install/Steps/EnableBundledExtensions.php
Expand Up @@ -111,7 +111,7 @@ private function loadExtensions()
})->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];
});
Expand Down

0 comments on commit 843daf6

Please sign in to comment.