[0.12] Moved Plugin Scopes To Trait, Added Caching And Added TestCase #588
Conversation
| protected static function booted(): void { | ||
| $pluginScopes = Plugin::getScopesFor(self::class); | ||
| foreach($pluginScopes as $pluginScope) { | ||
| static::addGlobalScope(new $pluginScope); | ||
| } | ||
| } |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
Here we can add all other plugin Caches, e.g. PluginHooks
| "Tests\\": "tests/", | ||
| "App\\Plugins\\": "tests/assets/Plugins/" |
There was a problem hiding this comment.
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.
| 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'), | ||
| ), |
There was a problem hiding this comment.
Added the scope plugin to have a plugin that provides a scope.
| 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(); | ||
| } |
There was a problem hiding this comment.
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
|
The failing tests are from patchAttributes which should be fixed in #585 |
|
👍 |
Uh oh!
There was an error while loading. Please reload this page.