PHPackages                             skylence/laravel-interceptor - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. skylence/laravel-interceptor

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

skylence/laravel-interceptor
============================

A compiled proxy-based plugin system for Laravel, inspired by Magento 2 but optimized for performance

v1.0.0(3mo ago)00[5 PRs](https://github.com/skylence-be/laravel-interceptor/pulls)MITPHPPHP ^8.2CI passing

Since Jan 23Pushed 1mo agoCompare

[ Source](https://github.com/skylence-be/laravel-interceptor)[ Packagist](https://packagist.org/packages/skylence/laravel-interceptor)[ GitHub Sponsors](https://github.com/skylence)[ RSS](/packages/skylence-laravel-interceptor/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (12)Versions (9)Used By (0)

Laravel Interceptor
===================

[](#laravel-interceptor)

A compiled proxy-based plugin system for Laravel, inspired by Magento 2's interceptor pattern.

What is an Interceptor?
-----------------------

[](#what-is-an-interceptor)

An interceptor allows you to **modify, extend, or completely override** the behavior of any class method **without modifying the original source code**. This is achieved by generating proxy classes that wrap the original classes and intercept method calls.

### Use Cases

[](#use-cases)

- **Validation** - Validate input before a method executes
- **Logging &amp; Auditing** - Log method calls, parameters, and results
- **Caching** - Cache method results transparently
- **Authorization** - Check permissions before executing methods
- **Event Dispatching** - Trigger events before/after method execution
- **Error Handling** - Wrap methods with try/catch logic
- **Performance Monitoring** - Measure execution time
- **Data Transformation** - Modify input arguments or return values
- **Feature Flags** - Conditionally modify behavior
- **Third-party Package Extension** - Extend vendor code without forking

### How It Works

[](#how-it-works)

 ```
flowchart TB
    subgraph build["BUILD TIME (artisan)"]
        scan["Scan #[Plugin]attributes"] --> chain["Build chainmap + cache"] --> generate["Generate proxyclasses"]
    end

    subgraph runtime["RUNTIME"]
        resolve["Container resolves"] --> proxy["Returns Proxy"] --> execute["Executes plugins"]
    end

    build --> runtime
```

      Loading 1. At **build time**, plugins are scanned and proxy classes are generated
2. At **runtime**, the container returns the proxy instead of the original class
3. When a method is called, the proxy executes: before → around → original → after

Features
--------

[](#features)

- **Before plugins** - Modify method arguments before execution
- **Around plugins** - Wrap method execution with custom logic
- **After plugins** - Modify return values after execution
- **Compiled proxies** - Zero runtime overhead after compilation
- **HMAC-signed cache** - Tamper-proof cache files
- **Circular dependency detection** - Prevents infinite loops

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 11+

Installation
------------

[](#installation)

```
composer require skylence/laravel-interceptor
```

Publish the configuration file:

```
php artisan vendor:publish --tag=interceptor-config
```

Quick Start
-----------

[](#quick-start)

### 1. Create a Plugin

[](#1-create-a-plugin)

Create a plugin class with the `#[Plugin]` attribute:

```
