All Projects → imanghafoori1 → laravel-decorator

imanghafoori1 / laravel-decorator

Licence: MIT license
Easily decorate your method calls with laravel-decorator package

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to laravel-decorator

design-patterns-php
All Design Patterns Samples in PHP
Stars: ✭ 43 (-65.6%)
Mutual labels:  design-patterns, decorator-pattern
oh-my-design-patterns
🎨 Record the articles and code I wrote while learning design patterns
Stars: ✭ 33 (-73.6%)
Mutual labels:  design-patterns, decorator
csharp-design-patterns-for-humans
Design Patterns for Humans™ - An ultra-simplified explanation - C# Examples
Stars: ✭ 1,086 (+768.8%)
Mutual labels:  design-patterns, decorator-pattern
discord-nestjs
👾 NestJS package for discord.js
Stars: ✭ 173 (+38.4%)
Mutual labels:  decorator
design-patterns-cpp14
🙏 Design patterns implemented in C++14
Stars: ✭ 35 (-72%)
Mutual labels:  design-patterns
betterdocs
📚 Web version of https://github.com/khusnetdinov/ruby.fundamental repo - Fundamental programming with ruby examples and references. It covers threads, SOLID principles, design patterns, data structures, algorithms. Books for reading.
Stars: ✭ 25 (-80%)
Mutual labels:  design-patterns
laravel-vue-component
A Blade directive to ease up Vue workflow for Laravel projects
Stars: ✭ 19 (-84.8%)
Mutual labels:  laravel-5-package
laravel-sms
Laravel 贴合实际需求同时满足多种通道的短信发送组件
Stars: ✭ 67 (-46.4%)
Mutual labels:  laravel-5-package
typescript-retry-decorator
lightweight typescript retry decorator with 0 dependency.
Stars: ✭ 50 (-60%)
Mutual labels:  decorator
sweetalert
Laravel 5 Package for SweetAlert2. Use this package to easily show sweetalert2 prompts in your laravel app.
Stars: ✭ 28 (-77.6%)
Mutual labels:  laravel-5-package
goodcode
A curated collection of annotated code examples from prominent open-source projects
Stars: ✭ 184 (+47.2%)
Mutual labels:  design-patterns
Design-Patterns-In-Swift-CN
设计模式(Swift 5.0 实现)- 中文版
Stars: ✭ 290 (+132%)
Mutual labels:  design-patterns
swe
Examples and exercises used during Software Engineering course
Stars: ✭ 18 (-85.6%)
Mutual labels:  design-patterns
express-mvc-pattern
Example nodejs using express implementation design pattern using mvc architecture.
Stars: ✭ 52 (-58.4%)
Mutual labels:  design-patterns
JavaScript-design-patterns
Examples of popular design patterns in JavaScript
Stars: ✭ 41 (-67.2%)
Mutual labels:  design-patterns
Design-Patterns
Design Patterns
Stars: ✭ 29 (-76.8%)
Mutual labels:  design-patterns
HeadFirstDesignPatternsSwift
An implementation of examples from "Head First Design Patterns", written in Swift.
Stars: ✭ 20 (-84%)
Mutual labels:  design-patterns
design-patterns
Grokking design patterns in python
Stars: ✭ 24 (-80.8%)
Mutual labels:  design-patterns
uml-diagram-for-kotlin-design-pattern-examples
UML diagram list of GoF design pattern examples written in Kotlin.
Stars: ✭ 23 (-81.6%)
Mutual labels:  design-patterns
Java-design-patterns
Java Design patterns.
Stars: ✭ 49 (-60.8%)
Mutual labels:  design-patterns

🎄 Laravel Decorator

Decorator pattern in laravel apps

Build Status Quality Score StyleCI Software License Latest Stable Version Code Coverage Total Downloads

Made with ❤️ for smart clean coders

A try to port "decorator" feature from python language to laravel framework.

python-and-prey

🚚 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 it's 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 an other "callable" (as it's only argument, like a snake swallows an other 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 modifing the existing code.

It smells like Open-closed Principle Yeah ?! 👃

You want to keep the responsibilities seperate. In this case caching should not be in a repository or controller but in it's 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 rememnber 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 it's name :
\Decorator::decorate('class@method, 'myDecoratorName'); // (second)

How to call a method with it's decorators ?

image

Decorate Facades :

Decorating Facade Methods

First, you should extend the Imanghafoori\Decorator\DecoratableFacade class (instead of the laravel base Facade).

image

Now You Can Apply Decorators in your ServiceProvider's boot method:

image

then if you call your facade as normal you get decorated results.

image

⚠️ 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 willing.

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 login with any password in local environment only.


💎 Authorization and ACL is now very easy with hey-man package !!!


🍌 Reward me a banana 🍌

so that I will have energy to start the next package for you.

  • Dodge Coin: DJEZr6GJ4Vx37LGF3zSng711AFZzmJTouN

  • LiteCoin: ltc1q82gnjkend684c5hvprg95fnja0ktjdfrhcu4c4

  • BitCoin: bc1q53dys3jkv0h4vhl88yqhqzyujvk35x8wad7uf9

  • Ripple: rJwrb2v1TR6rAHRWwcYvNZxjDN2bYpYXhZ

  • Etherium: 0xa4898246820bbC8f677A97C2B73e6DBB9510151e


Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].