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

fix: circular dependencies disable all involved extensions #3785

Merged
merged 2 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/core/locale/core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ core:

# These translations are displayed as error messages.
error:
circular_dependencies_message: "Circular dependencies detected: {extensions}. Aborting. Please disable one of the extensions and try again."
dependent_extensions_message: "Cannot disable {extension} until the following dependent extensions are disabled: {extensions}"
extension_initialiation_failed_message: "{extension} failed to initialize, check the browser console for further information."
generic_message: "Oops! Something went wrong. Please reload the page and try again."
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Extension\Exception;

use Exception;
use Flarum\Extension\ExtensionManager;

class CircularDependenciesException extends Exception
{
public $circular_dependencies;

public function __construct(array $circularDependencies)
{
$this->circular_dependencies = $circularDependencies;

parent::__construct('Circular dependencies detected: '.implode(', ', ExtensionManager::pluckTitles($circularDependencies)).' - aborting. Please fix this by disabling the extensions that are causing the circular dependencies.');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Extension\Exception;

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

class CircularDependenciesExceptionHandler
{
public function handle(CircularDependenciesException $e): HandledError
{
return (new HandledError(
$e,
'circular_dependencies',
409
))->withDetails($this->errorDetails($e));
}

protected function errorDetails(CircularDependenciesException $e): array
{
return [[
'extensions' => ExtensionManager::pluckTitles($e->circular_dependencies),
]];
}
}
19 changes: 18 additions & 1 deletion framework/core/src/Extension/ExtensionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Flarum\Extension\Event\Enabled;
use Flarum\Extension\Event\Enabling;
use Flarum\Extension\Event\Uninstalled;
use Flarum\Extension\Exception\CircularDependenciesException;
use Flarum\Foundation\Paths;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
Expand Down Expand Up @@ -157,6 +158,13 @@ public function getExtensions()
return $this->extensions;
}

public function getExtensionsById(array $ids): Collection
{
return $this->getExtensions()->filter(function (Extension $extension) use ($ids) {
return in_array($extension->getId(), $ids);
});
}

/**
* Loads an Extension with all information.
*
Expand Down Expand Up @@ -397,10 +405,19 @@ public function getEnabled()
* Persist the currently enabled extensions.
*
* @param array $enabledExtensions
* @throws CircularDependenciesException
*/
protected function setEnabledExtensions(array $enabledExtensions)
{
$sortedEnabled = static::resolveExtensionOrder($enabledExtensions)['valid'];
$resolved = static::resolveExtensionOrder($enabledExtensions);

if (! empty($resolved['circularDependencies'])) {
throw new Exception\CircularDependenciesException(
$this->getExtensionsById($resolved['circularDependencies'])->values()->all()
);
}

$sortedEnabled = $resolved['valid'];

$sortedEnabledIds = array_map(function (Extension $extension) {
return $extension->getId();
Expand Down
1 change: 1 addition & 0 deletions framework/core/src/Foundation/ErrorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function register()
return [
IlluminateValidationException::class => Handling\ExceptionHandler\IlluminateValidationExceptionHandler::class,
ValidationException::class => Handling\ExceptionHandler\ValidationExceptionHandler::class,
ExtensionException\CircularDependenciesException::class => ExtensionException\CircularDependenciesExceptionHandler::class,
ExtensionException\DependentExtensionsException::class => ExtensionException\DependentExtensionsExceptionHandler::class,
ExtensionException\MissingDependenciesException::class => ExtensionException\MissingDependenciesExceptionHandler::class,
];
Expand Down