jcesarbueno/laravel-strategy
Composer 安装命令:
composer require jcesarbueno/laravel-strategy
包简介
A Laravel package for generating Strategy Design Pattern, along with Factory and Chain of Responsibility support.This package provides an Artisan command to quickly scaffold strategies, keeping your Laravel project well-structured.
README 文档
README
🎯 Laravel Strategy Package
A Laravel package for generating Strategy Design Pattern, along with Factory and Chain of Responsibility support.
This package provides an Artisan command to quickly scaffold strategies, keeping your Laravel project well-structured.
🚀 Features
- ✅ Generates Strategy Pattern classes
- ✅ Creates a Factory for handling strategy instances
- ✅ Supports Chain of Responsibility (Pipelines)
- ✅ Keeps Strategies organized in a dedicated folder
- ✅ Fully tested with PestPHP & PHPStan for static analysis
📦 Installation
Require the package via Composer:
composer require jcesarbueno/laravel-strategy:^1.0
⚙️ How It Works
Run the following Artisan command:
php artisan make:strategy SendNotification
You will be prompted with interactive questions:
1️⃣ Which methods should it have? (Enter method names one by one, press Enter on an empty line to finish)
2️⃣ Which concrete implementations should it have? (Enter class names one by one, press Enter on an empty line to finish)
3️⃣ Do you want to create Pipelines (Chain of Responsibility) for the Strategy? (Answer yes or no)
📁 Generated Structure
For example, if you create a SendNotification strategy with method send(), and implementations ApiEvent, SlackEvent and EmailEvent, the package will generate:
app/Strategies/SendNotification/ │── Contracts/ │ └── SendNotificationContract.php │── Factories/ │ └── SendNotificationFactory.php │── Pipelines/ │ └── SendNotificationPipeline.php (if selected) │── Implementations/ │ ├── ApiNotification.php │ ├── SlackNotification.php │ ├── EmailNotification.php
Then, you can use the SendNotificationFactory to get the desired implementation:
use App\Strategies\SendNotification\Factories\SendNotificationFactory; use App\Models\Customer; $notificationType = Customer::find(1)->notification_type; // Choose the implementation in runtime $sendNotification = SendNotificationFactory::make($notificationType); $sendNotification->send();
You can also use the prebuilt Pipeline to handle the chain of responsibility:
You can create more than one pipeline for the same strategy, each one with a different responsibility. Just copy the SendNotificationPipeline and change the name.
namespace App\Strategies\SendNotification\Pipelines; use Closure; class EnsureNotificationTextIsNotEmpty { public function handle($customer, Closure $next) { if (empty($customer->event->text)) { throw new \Exception('Notification text cannot be empty'); } return $next($customer); } }
namespace App\Strategies\SendNotification\Pipelines; use Closure; class EnsureCustomerHasEmail { public function handle($customer, Closure $next) { if (empty($customer->email)) { throw new \Exception('Customer must have an email'); } return $next($customer); } }
Then, you can choose which pipeline to use in each implementation using the function getPipelines():
public function getPipelines(): array { return [ EnsureNotificationTextIsNotEmpty::class, EnsureCustomerHasEmail::class, ]; }
And another implementation can have a different pipeline:
public function getPipelines(): array { return [ EnsureNotificationTextIsNotEmpty::class, ]; }
Now you just call the Pipeline after creating the strategy:
use App\Strategies\SendNotification\Factories\SendNotificationFactory; use App\Models\Customer; use Illuminate\Support\Facades\Pipeline; $customer = Customer::find(1); $sendNotification = SendNotificationFactory::make($customer->notification_type); Pipeline::send($customer) ->through($sendNotification->getPipelines()) ->then(function ($customer) use ($sendNotification) { $sendNotification->send(); });
Other Usages for Pipelines
You can use the Pipeline to filter the data before sending it to the strategy, or to handle exceptions in a more organized way.
namespace App\Strategies\SendNotification\Pipelines; use Illuminate\Support\Collection; use Closure; class FilterNotSendedEvents { public function handle(Collection $events, Closure $next) { $events->filter(function ($event) { return $event->sended === false; }); return $next($events); } }
Just chain the Pipelines to filter the desired data.
use App\Strategies\SendNotification\Factories\SendNotificationFactory; use App\Models\Customer; use Illuminate\Support\Facades\Pipeline; $customer = Customer::with('events')->find(1); $sendNotification = SendNotificationFactory::make($customer->notification_type); $filteredEvents = Pipeline::send($customer->events) ->through($sendNotification->getPipelines()) ->thenReturn(); $sendNotification->send($filteredEvents);
📝 License
This package is open-source software licensed under the MIT license.
🧑💻 Author
This package was created by Júlio César Bueno, Laravel developer since 2023.
jcesarbueno/laravel-strategy 适用场景与选型建议
jcesarbueno/laravel-strategy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「pattern」 「design」 「laravel」 「strategy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jcesarbueno/laravel-strategy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jcesarbueno/laravel-strategy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jcesarbueno/laravel-strategy 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package for the Repository Design Pattern.
Symfony bundle for broadway/broadway.
Inbox pattern process implementation for your Laravel Applications
A PHP console tool to generate Markdown documentation from your docblocks and types hints using templates.
A library for simple pattern matching.
Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-02