Skip to content

Commit

Permalink
Register OctaneReloadPermissions listener for Laravel Octane
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Apr 18, 2023
1 parent 21a295b commit 4cbfc19
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
8 changes: 7 additions & 1 deletion phpstan-baseline.neon
Expand Up @@ -4,4 +4,10 @@ parameters:
# PHPStan can't understand what's going on in context of Role class using Builder `when`
message: "#^Call to an undefined method Spatie\\\\Permission\\\\Models\\\\Role::getRoleClass\\(\\).$#"
count: 1
path: src\Traits\HasPermissions.php
path: src/Traits/HasPermissions.php

-
# Laravel Octane is not available for PHPStan
message: "#^Class Laravel\\\\Octane\\\\Events\\\\[a-zA-Z]+ not found\\.$#"
count: 1
path: src/PermissionServiceProvider.php
13 changes: 13 additions & 0 deletions src/Listeners/OctaneReloadPermissions.php
@@ -0,0 +1,13 @@
<?php

namespace Spatie\Permission\Listeners;

use Spatie\Permission\PermissionRegistrar;

class OctaneReloadPermissions
{
public function handle($event): void
{
$event->sandbox->make(PermissionRegistrar::class)->clearPermissionsCollection();
}
}
15 changes: 15 additions & 0 deletions src/PermissionServiceProvider.php
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\Permission;

use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Routing\Route;
Expand All @@ -12,6 +13,7 @@
use Illuminate\View\Compilers\BladeCompiler;
use Spatie\Permission\Contracts\Permission as PermissionContract;
use Spatie\Permission\Contracts\Role as RoleContract;
use Spatie\Permission\Listeners\OctaneReloadPermissions;

class PermissionServiceProvider extends ServiceProvider
{
Expand All @@ -25,6 +27,8 @@ public function boot()

$this->registerModelBindings();

$this->registerOctaneListener();

$this->callAfterResolving(Gate::class, function (Gate $gate, Application $app) {
if ($this->app['config']->get('permission.register_permission_check_method')) {
/** @var PermissionRegistrar $permissionLoader */
Expand Down Expand Up @@ -86,6 +90,17 @@ protected function registerCommands(): void
]);
}

protected function registerOctaneListener(): void
{
if ($this->app->runningInConsole() || ! $this->app['config']->get('octane.listeners')) {
return;
}

$dispatcher = $this->app[Dispatcher::class];

$dispatcher->listen(\Laravel\Octane\Events\OperationTerminated::class, OctaneReloadPermissions::class);
}

protected function registerModelBindings(): void
{
$this->app->bind(PermissionContract::class, fn ($app) => $app->make($app->config['permission.models.permission']));
Expand Down

0 comments on commit 4cbfc19

Please sign in to comment.