PHPackages                             theflowbyte/laravel-macro-attribute - 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. theflowbyte/laravel-macro-attribute

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

theflowbyte/laravel-macro-attribute
===================================

Simplifies binding macros to methods in Laravel using PHP 8 attributes.

v1.0.0(1y ago)23MITPHPPHP &gt;=8.0

Since Nov 24Pushed 1y ago1 watchersCompare

[ Source](https://github.com/TheFlowByte/laravel-macro-attribute)[ Packagist](https://packagist.org/packages/theflowbyte/laravel-macro-attribute)[ RSS](/packages/theflowbyte-laravel-macro-attribute/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependencies (3)Versions (2)Used By (0)

Laravel Macro Attribute
=======================

[](#laravel-macro-attribute)

[![Latest Version](https://camo.githubusercontent.com/491e739daefa28cc41a88ad6e15115f0963a1662501fbdaf29d72183948b2a4d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746865666c6f77627974652f6c61726176656c2d6d6163726f2d6174747269627574652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/theflowbyte/laravel-macro-attribute)

**Laravel Macro Attribute** simplifies binding macros to methods in Laravel using PHP 8 attributes. Define macros with the `#[Macro]` attribute, and they are registered for one or more target classes.

---

Features
--------

[](#features)

- **Simple Macro Registration**: Bind macros to one or multiple Laravel classes like `Collection`, `Request`, and others.
- **Powered by PHP 8 Attributes**: Clean and modern approach with minimal boilerplate.
- **Custom Macro Names**: Use the default method name or specify custom macro names.
- **Support for Multiple Classes**: A single macro can be registered for multiple target classes.

---

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

[](#installation)

Require the package via Composer:

```
composer require theflowbyte/laravel-macro-attribute
```

Usage
-----

[](#usage)

### Step 1: Define Macros with the `#[Macro]` Attribute

[](#step-1-define-macros-with-the-macro-attribute)

Create a class with static methods and use the `#[Macro]` attribute to bind them to one or more target classes.

```
namespace App\Helpers;

use TheFlowByte\MacroAttribute\Attributes\Macro;
use Illuminate\Http\Request;

class CrawlerHelper
{
    #[Macro(targetClass: Request::class)]
    public static function isCrawler(Request $request): bool
    {
        $userAgent = strtolower($request->header('User-Agent', ''));

        return str_contains($userAgent, 'bot');
    }

    #[Macro(targetClass: Request::class, macroName: 'denyCrawlerAccess')]
    public static function shouldBlockCrawler(Request $request): bool
    {
        $userAgent = strtolower($request->header('User-Agent', ''));

        $blockedBots = config('bots.blacklist', []);

        foreach ($blockedBots as $bot) {
            if (str_contains($userAgent, $bot)) {
                return true;
            }
        }

        return false;
    }
}
```

### Step 2: Register Classes with the `#[Macro]` Attribute

[](#step-2-register-classes-with-the-macro-attribute)

Register the classes containing the macros using MacroAttributeBinder:

```
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use TheFlowByte\MacroAttribute\Loaders\MacroAttributeLoader;
use App\Helpers\CrawlerHelper;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // Register the macros from the specified class
        MacroAttributeLoader::register([
            CrawlerHelper::class,
            // ...
        ]);
    }
}
```

### Step 3: Use the Macros

[](#step-3-use-the-macros)

Once registered, the macros are available on the target classes:

```
namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class BlockCrawlers
{
    public function handle(Request $request, Closure $next)
    {
        if ($request->isCrawler()) {
            logger()->debug('Request identified as coming from a crawler.');
        }

        if ($request->denyCrawlerAccess()) {
            return response('Access Denied for Crawlers.', 403);
        }

        return $next($request);
    }
}
```

### Advanced Features

[](#advanced-features)

**Support for Multiple Target Classes**

You can bind a single macro to multiple target classes by passing an array of class names to the targetClass parameter:

```
use TheFlowByte\MacroAttribute\Attributes\Macro;
use App\Models\Order;
use App\Models\Invoice;

class FinancialMacros
{
    #[Macro(targetClass: [Order::class, Invoice::class])]
    public static function calculateTotalWithTax(HasSubtotal $model, float $taxRate = 0.2): float
    {
        return $model->subtotal + ($model->subtotal * $taxRate);
    }
}
```

TODO
----

[](#todo)

### Features to Implement

[](#features-to-implement)

1. **Code Generator for IDE Autocomplete**
    - Create a standalone repository for a tool that generates PHPDoc annotations for registered macros.
    - Enhance IDE support for Laravel macros, providing autocomplete in PHPStorm, VSCode, etc.

Contributing
------------

[](#contributing)

Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas.

License
-------

[](#license)

The MIT License (MIT).

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

586d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/189529000?v=4)[TheFlowByte](/maintainers/TheFlowByte)[@TheFlowByte](https://github.com/TheFlowByte)

---

Top Contributors

[![TheFlowByte](https://avatars.githubusercontent.com/u/189529000?v=4)](https://github.com/TheFlowByte "TheFlowByte (2 commits)")

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/theflowbyte-laravel-macro-attribute/health.svg)

```
[![Health](https://phpackages.com/badges/theflowbyte-laravel-macro-attribute/health.svg)](https://phpackages.com/packages/theflowbyte-laravel-macro-attribute)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M345](/packages/psalm-plugin-laravel)[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M11](/packages/renatomarinho-laravel-page-speed)[illuminate/pagination

The Illuminate Pagination package.

12234.1M1.0k](/packages/illuminate-pagination)[illuminate/pipeline

The Illuminate Pipeline package.

9349.2M282](/packages/illuminate-pipeline)[illuminate/redis

The Illuminate Redis package.

8314.6M375](/packages/illuminate-redis)[illuminate/cookie

The Illuminate Cookie package.

244.6M137](/packages/illuminate-cookie)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
