承接 theflowbyte/laravel-macro-attribute 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

theflowbyte/laravel-macro-attribute

最新稳定版本:v1.0.0

Composer 安装命令:

composer require theflowbyte/laravel-macro-attribute

包简介

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

README 文档

README

Latest Version

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

  • 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

Require the package via Composer:

composer require theflowbyte/laravel-macro-attribute

Usage

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

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

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

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

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

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

License

The MIT License (MIT).

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-24