-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(provider): Add ability to register Gate policies using contract
You service provider can register policies using HasPolicies interface.
- Loading branch information
Showing
35 changed files
with
137 additions
and
31 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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Contracts; | ||
|
||
interface HasPolicies | ||
{ | ||
/** | ||
* @return array<string, class-string> | ||
*/ | ||
public function policies(): array; | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LaraStrict\Providers\Pipes; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Auth\Access\Gate; | ||
use LaraStrict\Contracts\AppServiceProviderPipeContract; | ||
use LaraStrict\Contracts\HasPolicies; | ||
use LaraStrict\Entities\AppServiceProviderEntity; | ||
|
||
class RegisterProviderPoliciesPipe implements AppServiceProviderPipeContract | ||
{ | ||
public function __construct( | ||
private readonly Gate $gate | ||
) { | ||
} | ||
|
||
public function handle(AppServiceProviderEntity $appServiceProvider, Closure $next): void | ||
{ | ||
if ($appServiceProvider->serviceProvider instanceof HasPolicies) { | ||
foreach ($appServiceProvider->serviceProvider->policies() as $class => $policy) { | ||
$this->gate->policy($class, $policy); | ||
} | ||
} | ||
|
||
$next($appServiceProvider); | ||
} | ||
} |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions
11
tests/Feature/Providers/Pipes/LoadProviderRoutesPipe/WithAll/WithAllServiceProvider.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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Providers\Pipes\LoadProviderRoutesPipe\WithAll; | ||
|
||
use Tests\LaraStrict\Feature\Providers\Pipes\LoadProviderRoutesPipe\WithCustom\WithCustomServiceProvider; | ||
|
||
class WithAllServiceProvider extends WithCustomServiceProvider | ||
{ | ||
} |
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
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
18 changes: 18 additions & 0 deletions
18
tests/Feature/Providers/Pipes/RegisterProviderPoliciesPipe/PoliciesServiceProvider.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,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Providers\Pipes\RegisterProviderPoliciesPipe; | ||
|
||
use LaraStrict\Contracts\HasPolicies; | ||
use LaraStrict\Providers\AbstractServiceProvider; | ||
|
||
class PoliciesServiceProvider extends AbstractServiceProvider implements HasPolicies | ||
{ | ||
public function policies(): array | ||
{ | ||
return [ | ||
Test::class => TestPolicy::class, | ||
]; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...Feature/Providers/Pipes/RegisterProviderPoliciesPipe/RegisterProviderPoliciesPipeTest.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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Providers\Pipes\RegisterProviderPoliciesPipe; | ||
|
||
use Illuminate\Contracts\Auth\Access\Gate; | ||
use Tests\LaraStrict\Feature\TestCase; | ||
|
||
class RegisterProviderPoliciesPipeTest extends TestCase | ||
{ | ||
public function test(): void | ||
{ | ||
$this->app->register(PoliciesServiceProvider::class, true); | ||
|
||
/** @var Gate $gate */ | ||
$gate = $this->app->make(Gate::class); | ||
|
||
$policy = $gate->getPolicyFor(Test::class); | ||
$this->assertInstanceOf(TestPolicy::class, $policy); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/Feature/Providers/Pipes/RegisterProviderPoliciesPipe/Test.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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Providers\Pipes\RegisterProviderPoliciesPipe; | ||
|
||
class Test | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/Feature/Providers/Pipes/RegisterProviderPoliciesPipe/TestPolicy.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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Feature\Providers\Pipes\RegisterProviderPoliciesPipe; | ||
|
||
class TestPolicy | ||
{ | ||
} |
11 changes: 0 additions & 11 deletions
11
tests/Feature/Providers/WithAll/WithAllServiceProvider.php
This file was deleted.
Oops, something went wrong.
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