PHP code example of mud-qadm / policy-discovery

1. Go to this page and download the library: Download mud-qadm/policy-discovery library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mud-qadm / policy-discovery example snippets


return [

    /*
    |--------------------------------------------------------------------------
    | Policy Base Directory
    |--------------------------------------------------------------------------
    | Root directory where all policy classes are located.
    */
    'policy_directory' => app_path('Policies'),

    /*
    |--------------------------------------------------------------------------
    | Policy Namespace
    |--------------------------------------------------------------------------
    | Base namespace used to resolve policy classes.
    */
    'policy_namespace' => 'App\\Policies',

    /*
    |--------------------------------------------------------------------------
    | Model Directories
    |--------------------------------------------------------------------------
    | Directories used for automatic model resolution when mapping policies.
    */
    'model_directories' => [
        app_path('Models'),
    ],

    /*
    |--------------------------------------------------------------------------
    | Recursive Scanning
    |--------------------------------------------------------------------------
    | When enabled, policies inside nested directories will be discovered.
    */
    'recursive' => true,

    /*
    |--------------------------------------------------------------------------
    | Cache Enabled
    |--------------------------------------------------------------------------
    | Enable or disable caching of discovered policy mappings.
    */
    'cache_enabled' => env('POLICY_DISCOVERY_CACHE', true),

    /*
    |--------------------------------------------------------------------------
    | Cache File Path
    |--------------------------------------------------------------------------
    | File used to store cached policy-model mappings.
    */
    'cache_file' => storage_path('policy-discovery/mapping.json'),

];



namespace App\Policies\Settings;

use App\Models\Settings\Role;
use App\Models\User;

class RolePolicy
{
    public function viewAny(User $user): bool { /* ... */ }
    public function view(User $user, Role $role): bool { /* ... */ }
    public function update(User $user, Role $role): bool { /* ... */ }
    public function delete(User $user, Role $role): bool { /* ... */ }
}

$user->can('view', $role);
Gate::allows('update', $role);

use MudQadm\PolicyDiscovery\Attributes\PolicyFor;

#[PolicyFor(User::class)]
class UserPolicy
{
    //
}
text
app/Policies/
├── UserPolicy.php
├── PostPolicy.php
└── CommentPolicy.php
text
app/
└── Policies/
    ├── Settings/
    │   ├── RolePolicy.php
    │   └── PermissionPolicy.php
    ├── HR/
    │   └── EmployeePolicy.php
    └── Finance/
        └── InvoicePolicy.php
bash
php artisan vendor:publish --tag=policy-discovery-config
bash
php artisan make:policy