imanghafoori/laravel-decorator
Composer 安装命令:
composer require imanghafoori/laravel-decorator
包简介
A package to easily decorate your function calls.
README 文档
README
Decorator pattern in Laravel apps
Made with ❤️ for smart clean coders
A try to port "decorator" feature from python language to laravel framework.
🚚 Installation:
composer require imanghafoori/laravel-decorator
What is a "Decorator" ❓
A decorator is callable which wraps around the original decorated callable, in order to form a new callable composed of the previous two.
Like a python snake swallowing a deer whole and wraps around its body !
After that the snake becomes capable to eat and digest grasses 🌿 because it has a deer inside it.
Technically, A "Decorator":
1- Is a "callable"
2- which takes another "callable" (as it's only argument, like a snake swallows another snake)
3- and returns a new "callable" (which internally calls the original callable, putting some code before and after it.)
What?!??! ???! (0_o)
What can be considered as a "callable" within laravel ?!
Long story short, anything that can be called (invoked) with App::call(); or call_user_func()
like: 'MyClass@myMethod' or a closure, [UserRepo::class, 'find']
Cache Like a Pro:
Caching DB queries is always a need, but it is always annoying to add more code to the existing code. It will become more messy, we may break the current code, after all it adds a layer of fog. Yeah?
Imagine that you have a UserControllerwhich calls a UserRepo@find to get a $user .
Then after a while you decide to put a cache layer between those two classes for obvious reasons.
According to SOLID principles, you shouldn't put the caching code logic neither in your controller nor your UserRepo. But somewhere in between.
In other words, you want to add a new feature (caching in this case) without modifying the existing code.
It smells like Open-closed Principle Yeah?! 👃
You want to keep the responsibilities separate. In this case caching should not be in a repository or controller but in its own class.
It smells like Single Responsibility Principle yeah?! 👃
class UserRepository { function find($uid) { return User::find($uid); } } class MadUsersController extends Controller { function show ($madUserId) { $madUser = app()->call('UserRepository@find', ['id' => $madUserId]); } }
ok now there is no cache, going on. it is a direct call.
With the help of laravel-decorator built-in cache decorator, you can go to AppServiceProvider.php (or any other service provider):
<?php use Imanghafoori\Decorator\Decorators\DecoratorFactory; class AppServiceProvider extends ServiceProvider { public function boot() { $keyMaker = function ($madId) { return 'mad_user_key_' . $madId; }; $time = 10; $decorator = DecoratorFactory::cache($keyMaker, $time); \Decorator::decorate('UserRepository@find', $decorator); } }
You will get cached results from your calls, in your UserController without touching it!
but remember to change:
app()->call('UserRepository@find', ... // to : app('decorator')->call('UserRepository@find', ...
Define Your Own Decorators:
public function boot () { \Decorator::define('myDecoratorName1', 'SomeClass@someMethod'); // or \Decorator::define('myDecoratorName2', function ($callable) { return function (...) use ($callable){ ... } }); }
Then you can use this name (myDecoratorName) to decorate methods.
How to decorate a method?
// You may set multiple decorators on a single method... \Decorator::decorate('class@method, 'someClass@someOtherDecorator'); // (first) // or reference the decorator by its name : \Decorator::decorate('class@method, 'myDecoratorName'); // (second)
How to call a method with its decorators?
Decorate Facades:
Decorating Facade Methods
First you should extend the Imanghafoori\Decorator\DecoratableFacade class (instead of the laravel base Facade).
Now You Can Apply Decorators in your ServiceProvider's boot method:
Then if you call your facade as normal you get decorated results.
⚠️ Warning:
With great power, comes great responsibilities.
Remember not to violate the Liskoves Substitution Principle when you decorate something.
For example a method call which returns int|null should not unexpectedly return a string after being decorated.
$result = app('decorate')->call(...
Since the users of the method should be ready for type of value they get back.
But if you return only int and your decorator causes the null value to be filtered out. that's ok.
⭐ Your Stars Make Us Do More ⭐
As always if you found this package useful, and you want to encourage us to maintain and work on it, Please press the star button to declare your willingness.
More packages from the author:
💎 A minimal yet powerful package to give you opportunity to refactor your controllers.
💎 A minimal yet powerful package to give a better structure and caching opportunity for your laravel apps.
💎 It allows you log in with any password in local environment only.
💎 Authorization and ACL is now very easy with hey-man package!
imanghafoori/laravel-decorator 适用场景与选型建议
imanghafoori/laravel-decorator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.79k 次下载、GitHub Stars 达 139, 最近一次更新时间为 2019 年 01 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「design patterns」 「decorator pattern」 「php decorator pattern」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 imanghafoori/laravel-decorator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 imanghafoori/laravel-decorator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 imanghafoori/laravel-decorator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides some common user interface and design patterns.
A Laravel package for the Repository Design Pattern.
Symfony bundle for broadway/broadway.
A PHP console tool to generate Markdown documentation from your docblocks and types hints using templates.
Seo behavior for Yii2 models and meta tags helper with patterns using possibility
Serie of tools based on traits that simplify the use of singletons, multitons and other class instances controllers.
统计信息
- 总下载量: 13.79k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 139
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 12
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-10




