-
-
Notifications
You must be signed in to change notification settings - Fork 836
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
0a6c521
commit 84d14f4
Showing
8 changed files
with
279 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Extension/Exception/DependentExtensionsExceptionHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(), | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Extension/Exception/MissingDependenciesExceptionHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(), | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.