Download the PHP package jcesarbueno/laravel-strategy without Composer
On this page you can find all versions of the php package jcesarbueno/laravel-strategy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jcesarbueno/laravel-strategy
More information about jcesarbueno/laravel-strategy
Files in jcesarbueno/laravel-strategy
Package laravel-strategy
Short Description 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.
License MIT
Informations about the package laravel-strategy
π― 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:
βοΈ How It Works
Run the following Artisan command:
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:
Then, you can use the SendNotificationFactory to get the desired implementation:
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.
Then, you can choose which pipeline to use in each implementation using the function getPipelines():
And another implementation can have a different pipeline:
Now you just call the Pipeline after creating the strategy:
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.
Just chain the Pipelines to filter the desired data.
π 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.