Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Jan 29, 2021
Merged
15 changes: 2 additions & 13 deletions src/Extension/Exception/DependentExtensionsException.php
Original file line number Diff line number Diff line change
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)));
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)));
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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