1
0
mirror of https://github.com/flarum/core.git synced 2025-10-22 20:26:15 +02:00

Basic Extension Dependency Support (#2188)

- Don't enable an extension if its dependencies are not enabled
- Don't disable an extension if its dependencies are not disabled
This commit is contained in:
Alexander Skvortsov
2020-10-02 17:54:28 -04:00
committed by GitHub
parent 0a6c5217c1
commit 84d14f485a
8 changed files with 279 additions and 13 deletions

View File

@@ -0,0 +1,47 @@
<?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\Extension;
/**
* This exception is thrown when someone attempts to disable an extension
* that other enabled extensions depend on.
*/
class DependentExtensionsException extends Exception
{
public $extension;
public $dependent_extensions;
/**
* @param $extension: The extension we are attempting to disable.
* @param $dependent_extensions: Enabled Flarum extensions that depend on this extension.
*/
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);
}
}

View File

@@ -0,0 +1,34 @@
<?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\Foundation\ErrorHandling\HandledError;
class DependentExtensionsExceptionHandler
{
public function handle(DependentExtensionsException $e): HandledError
{
return (new HandledError(
$e,
'dependent_extensions',
409
))->withDetails($this->errorDetails($e));
}
protected function errorDetails(DependentExtensionsException $e): array
{
return [
[
'extension' => $e->extension->getId(),
'extensions' => $e->getDependentExtensionIds(),
]
];
}
}

View File

@@ -0,0 +1,47 @@
<?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\Extension;
/**
* This exception is thrown when someone attempts to enable an extension
* whose Flarum extension dependencies are not all enabled.
*/
class MissingDependenciesException extends Exception
{
public $extension;
public $missing_dependencies;
/**
* @param $extension: The extension we are attempting to enable.
* @param $missing_dependencies: Extensions that this extension depends on, and are not enabled.
*/
public function __construct(Extension $extension, array $missing_dependencies = null)
{
$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);
}
}

View File

@@ -0,0 +1,34 @@
<?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\Foundation\ErrorHandling\HandledError;
class MissingDependenciesExceptionHandler
{
public function handle(MissingDependenciesException $e): HandledError
{
return (new HandledError(
$e,
'missing_dependencies',
409
))->withDetails($this->errorDetails($e));
}
protected function errorDetails(MissingDependenciesException $e): array
{
return [
[
'extension' => $e->extension->getId(),
'extensions' => $e->getMissingDependencyIds(),
]
];
}
}