-
-
Notifications
You must be signed in to change notification settings - Fork 204
Simple Plugin system #1045
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
Open
jtgrimes
wants to merge
17
commits into
Kovah:2.x
Choose a base branch
from
jtgrimes:plugins
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Simple Plugin system #1045
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8b02f6c
Events thrown for create, update, delete links
c4582cf
Move archive handling from event listener to plugin
35eb15e
Plugins running through plugin manager
9268adc
Use reflection to register plugin listeners
6601473
Use reflection to register plugin listeners
1624bd7
set default plugin list so existing installs don't blow up
96a6655
Remove unnecessary use statement in app/Helper/PluginManager.php
jtgrimes e259a95
Add return type to registerPlugins in app/Helper/PluginManager.php
jtgrimes df4e963
Improve exception message in app/Helper/PluginManager.php
jtgrimes a7bd9f6
add return type to getParameterTypesFor app/Helper/PluginManager.php
jtgrimes 94e4606
remove comment from empty constructors
e0d5c1f
Add PluginException, expand test coverage for all the ways a plugin c…
5050697
document LinkDeleted event and only throw on successful deletion
1f8a5ee
correct config key
11934e9
minor code cleanup
07fcd8c
minor code cleanup
ffbbb14
Merge branch '2.x' into plugins
Kovah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,16 @@ | ||
| <?php | ||
|
|
||
| namespace App\Events; | ||
|
|
||
| use App\Models\Link; | ||
| use Illuminate\Foundation\Events\Dispatchable; | ||
| use Illuminate\Queue\SerializesModels; | ||
|
|
||
| class LinkCreated | ||
| { | ||
| use Dispatchable, SerializesModels; | ||
|
|
||
| public function __construct(public Link $link) | ||
| { | ||
| } | ||
| } |
This file contains hidden or 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,19 @@ | ||
| <?php | ||
|
|
||
| namespace App\Events; | ||
|
|
||
| use Illuminate\Foundation\Events\Dispatchable; | ||
| use Illuminate\Queue\SerializesModels; | ||
|
|
||
| /* | ||
| * Because this event is dispatched after the link is deleted, only the ID is available | ||
| * rather than the entire Link model (as found in LinkCreated and LinkUpdated) | ||
| */ | ||
| class LinkDeleted | ||
| { | ||
| use Dispatchable, SerializesModels; | ||
|
|
||
| public function __construct(public int $link_id) | ||
| { | ||
| } | ||
| } |
This file contains hidden or 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,16 @@ | ||
| <?php | ||
|
|
||
| namespace App\Events; | ||
|
|
||
| use App\Models\Link; | ||
| use Illuminate\Foundation\Events\Dispatchable; | ||
| use Illuminate\Queue\SerializesModels; | ||
|
|
||
| class LinkUpdated | ||
| { | ||
| use Dispatchable, SerializesModels; | ||
|
|
||
| public function __construct(public Link $link) | ||
| { | ||
| } | ||
| } |
This file contains hidden or 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,37 @@ | ||
| <?php | ||
|
|
||
| namespace App\Exceptions; | ||
|
|
||
| use Exception; | ||
|
|
||
| class PluginException extends Exception | ||
| { | ||
| public static function tooManyParameters(object|string $class): self | ||
| { | ||
| $className = self::resolveClassName($class); | ||
| return new static("{$className}->handle() should contain exactly 1 parameter"); | ||
| } | ||
|
|
||
| public static function noParameters(object|string $class): self | ||
| { | ||
| $className = self::resolveClassName($class); | ||
| return new static("{$className}->handle() does not specify any events to listen for"); | ||
| } | ||
|
|
||
| public static function noHandleFunction(object|string $class): self | ||
| { | ||
| $className = self::resolveClassName($class); | ||
| return new static("required function {$className}->handle() does not exist"); | ||
| } | ||
|
|
||
| public static function pluginClassNotFound(object|string $class): self | ||
| { | ||
| $className = self::resolveClassName($class); | ||
| return new static("plugin {$className} not found"); | ||
| } | ||
|
|
||
| private static function resolveClassName(object|string $class) | ||
| { | ||
| return (is_object($class)) ? $class->getName() : $class; | ||
| } | ||
| } |
This file contains hidden or 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,53 @@ | ||
| <?php | ||
|
|
||
| namespace App\Helper; | ||
|
|
||
| use App\Exceptions\PluginException; | ||
| use App\Plugins\NewLinkToWaybackMachine; | ||
| use Illuminate\Support\Facades\Config; | ||
| use Illuminate\Support\Facades\Event; | ||
| use Illuminate\Support\Reflector; | ||
| use ReflectionClass; | ||
| use ReflectionException; | ||
|
|
||
| class PluginManager | ||
| { | ||
| /** | ||
| * @throws PluginException | ||
| */ | ||
| public function registerPlugins(): void | ||
| { | ||
| $plugins = Config::get('linkace.plugins', [NewLinkToWaybackMachine::class]); | ||
| foreach ($plugins as $plugin) { | ||
| $parameterTypes = $this->getParameterTypesFor($plugin); | ||
| foreach ($parameterTypes as $event) { | ||
| Event::listen($event, $plugin); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @throws PluginException | ||
| */ | ||
| private function getParameterTypesFor(object|string $pluginClass): array | ||
| { | ||
| try { | ||
| $reflectionClass = new ReflectionClass($pluginClass); | ||
| } catch (ReflectionException) { | ||
| throw PluginException::pluginClassNotFound($pluginClass); | ||
| } | ||
| try { | ||
| $reflectionMethod = $reflectionClass->getMethod('handle'); | ||
| } catch (ReflectionException) { | ||
| throw PluginException::noHandleFunction($pluginClass); | ||
| } | ||
| $parameters = $reflectionMethod->getParameters(); | ||
| if (count($parameters) > 1) { | ||
| throw PluginException::tooManyParameters($pluginClass); | ||
| } | ||
| if (count($parameters) < 1) { | ||
| throw PluginException::noParameters($pluginClass); | ||
| } | ||
| return Reflector::getParameterClassNames($parameters[0]); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,31 @@ | ||
| <?php | ||
|
|
||
| namespace App\Plugins; | ||
|
|
||
| use App\Enums\ModelAttribute; | ||
| use App\Events\LinkCreated; | ||
| use App\Jobs\SaveLinkToWaybackmachine; | ||
|
|
||
| class NewLinkToWaybackMachine | ||
| { | ||
| /** | ||
| * Dispatch the SaveLinkToWaybackmachine job, if Internet Archive backups | ||
| * are enabled. | ||
| * If the link is private, private Internet Archive backups must be enabled | ||
| * too. | ||
| */ | ||
| public function handle(LinkCreated $event) : void | ||
| { | ||
| if (usersettings('archive_backups_enabled') === false) { | ||
| return; | ||
| } | ||
|
|
||
| if ($event->link->visibility === ModelAttribute::VISIBILITY_PRIVATE | ||
| && usersettings('archive_private_backups_enabled') === false | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| SaveLinkToWaybackmachine::dispatchAfterResponse($event->link); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -37,4 +37,7 @@ | |
| 'g:i A', | ||
| ], | ||
| ], | ||
| 'plugins' => [ | ||
| \App\Plugins\NewLinkToWaybackMachine::class, | ||
| ], | ||
| ]; | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.