Skip to content

[0.12] Moved Plugin Scopes To Trait, Added Caching And Added TestCase #588

Merged
Severino merged 8 commits into0.12-feat-accesspointsfrom
0.12-fix-plugin-scopes-trait-and-cache
Oct 30, 2025
Merged

[0.12] Moved Plugin Scopes To Trait, Added Caching And Added TestCase #588
Severino merged 8 commits into0.12-feat-accesspointsfrom
0.12-fix-plugin-scopes-trait-and-cache

Conversation

@Severino
Copy link
Contributor

@Severino Severino commented Oct 29, 2025

Comment on lines -101 to -106
protected static function booted(): void {
$pluginScopes = Plugin::getScopesFor(self::class);
foreach($pluginScopes as $pluginScope) {
static::addGlobalScope(new $pluginScope);
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was moved to HasPluginScopes.php then we can add the trait to every model we want in the future.

if(!array_key_exists($on, $scopes)) {
$scopes[$on] = [];
public function getScopes(): array {
return Cache::rememberForever($this->getScopeCacheKey(), function() {
Copy link
Contributor Author

@Severino Severino Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the Cache to make access faster without the need to parse the info.xml on every request.
It will be invalidated on install, update, uninstall and remove of the respective plugin.

}

public function clearCache(): void {
Cache::forget($this->getScopeCacheKey());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we can add all other plugin Caches, e.g. PluginHooks

Comment on lines +54 to +55
"Tests\\": "tests/",
"App\\Plugins\\": "tests/assets/Plugins/"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In tests we use this directory. Which was not detected by the auto loader when adding the scopes to the test plugins.
With this the tests should run without problems.

Comment on lines +38 to +47
2 => array(
'id' => 3,
'name' => 'ScopePlugin',
'version' => '3.2.0',
'uuid' => '123e4567-e89b-12d3-a456-426614174004',
'update_available' => null,
'installed_at' => null,
'created_at' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-08-01 08:00:00', 'UTC'),
'updated_at' => Carbon::createFromFormat('Y-m-d H:i:s', '2020-08-01 08:00:00', 'UTC'),
),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the scope plugin to have a plugin that provides a scope.

Comment on lines +96 to +108
public static function rebootModel($modelClass) {
// Clear global scopes before rebooting
$reflection = new \ReflectionClass($modelClass);

// Clear global scopes
$scopesProperty = $reflection->getProperty('globalScopes');
$scopesProperty->setAccessible(true);
$scopesProperty->setValue(null, []);

// Flush event listeners and reboot
$modelClass::flushEventListeners();
$modelClass::boot();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow PHPUnit behaves a little bit different than PHP in production. In Production the whole program is booted from ground up. In the unit test it remains booted during the process. So when installing a plugin, it would not be registered as a new boot, breaking the scope logic.

Calling this with the model class reboots the model class manually

@Severino
Copy link
Contributor Author

The failing tests are from patchAttributes which should be fixed in #585

@v1r0x
Copy link
Member

v1r0x commented Oct 30, 2025

👍

@Severino Severino merged commit a393b78 into 0.12-feat-accesspoints Oct 30, 2025
0 of 8 checks passed
@Severino Severino deleted the 0.12-fix-plugin-scopes-trait-and-cache branch October 30, 2025 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants