Download the PHP package mud-qadm/policy-discovery without Composer

On this page you can find all versions of the php package mud-qadm/policy-discovery. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package policy-discovery

Laravel Policy Discovery

Latest Version on Packagist Total Downloads PHP Version Laravel Version License

Automatically discover and register Laravel policies recursively – even in deeply nested directories or modular domain structures.

📋 Table of Contents


🔥 The Problem

Laravel's native policy auto-discovery works perfectly for flat directory structures:

But real-world applications often grow beyond that. When you organise policies by domain or module:

Laravel does not recursively scan subdirectories. This forces you to manually register every policy using Gate::policy() – a tedious and error-prone process.


💡 Solution

This package recursively scans any number of policy directories, automatically maps policies to their corresponding models, and registers them with Laravel's Gate – without any manual intervention.


✨ Features

Feature Description
Recursive discovery Finds policies in any subdirectory under configured paths
Automatic model mapping Infers model from policy name or uses @model annotation
Modular support Perfect for DDD, modules, or domain-driven structures
Zero configuration Drop-in installation, works out of the box
Performance caching File-based cache to avoid filesystem scanning on every request
CLI tools List, validate, warmup, clear, rebuild, and optimise caches
Export mappings Export discovered policy–model mappings to JSON
Diagnostics Statistics and validation commands to ensure correct setup
Automatic policy detection Newly created policies are discovered automatically without manual registration

🧰 Requirements


📦 Installation

Install via Composer:

Laravel will automatically register the package's service provider thanks to Laravel's package discovery.

To publish the configuration file:

This will create config/policy-discovery.php where you can customise directories, caching behaviour, and naming conventions.


⚙️ Configuration

After publishing, you can modify the following options in config/policy-discovery.php:


🚀 Usage

Once installed, simply place your policy files anywhere under the configured directories.

Example

Policy file: app/Policies/Settings/RolePolicy.php

The package will automatically:

  1. Discover RolePolicy recursively.
  2. Map it to App\Models\Settings\Role (using convention: RolePolicyRole model).
  3. Register it with Laravel's Gate.

You can now use Laravel's authorisation methods as usual:

No additional service provider code or manual Gate::policy() calls are needed.


🛠️ Artisan Commands

Command Description
php artisan policy-discovery:list Display all discovered policy–model mappings
php artisan policy-discovery:check Validate that mapped models and policies exist and are loadable
php artisan policy-discovery:warmup Generate the cache without clearing existing cache
php artisan policy-discovery:clear-cache Delete the cached policy mappings
php artisan policy-discovery:rebuild Clear and regenerate the cache
php artisan policy-discovery:optimize Optimise the cached mapping structure (e.g., sort, deduplicate)
php artisan policy-discovery:stats Show statistics (total policies, models, directories scanned, cache age)
php artisan policy-discovery:export Export all discovered mappings as JSON (useful for debugging or CI)

⚙️ How It Works

  1. Scanning – On the first request (or when the cache is empty), the package recursively scans the configured directories for PHP files ending with *Policy.php.

  2. Model Resolution – For each discovered policy, the package determines the target model using the following strategies (in order):

    • PHP Attributes using #[PolicyFor(...)]
    • DocBlock annotations using @model
    • Naming conventions (RolePolicyRole)

🎯 Explicit Policy Mapping (Attributes)

You can explicitly define which model a policy belongs to using PHP Attributes:

This is especially useful when the policy name does not directly match the model name or when working with custom domain structures.

  1. Registration – Each discovered policy–model pair is automatically registered with Laravel's Gate using Gate::policy().

  2. Caching – Discovered mappings are cached to avoid unnecessary filesystem scans and improve performance.

  3. Automatic Refresh – When a new policy is generated using:

the package automatically refreshes its mappings, ensuring the newly created policy is available without requiring manual cache rebuilds.

You can customise the discovery paths, namespaces, and mapping behaviour through the configuration file.


🧠 Caching & Performance

⚠️ Policy Discovery automatically keeps its cache in sync with policy changes, so manual cache rebuilding is rarely required.


🔍 Troubleshooting

Issue Solution
Policy not discovered Ensure the file name ends with Policy.php, verify the configured discovery paths, and run php artisan policy-discovery:check.
Model mapped incorrectly Use the #[PolicyFor(...)] attribute or add an @model annotation.
Cache appears outdated Run php artisan policy-discovery:rebuild and verify cache directory permissions.
Performance issues during development Disable caching or clear the cache frequently while iterating.

For additional help, open an issue on GitHub.


📄 License

The MIT License (MIT). See the LICENSE file for details.


Created & maintained by Ahmed Sabry Hagrs


All versions of policy-discovery with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
illuminate/support Version ^10.0 || ^11.0 || ^12.0 || ^13.0
symfony/finder Version ^6.0 || ^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mud-qadm/policy-discovery contains the following files

Loading the files please wait ...