PHPackages                             imanghafoori/laravel-decorator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. imanghafoori/laravel-decorator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

imanghafoori/laravel-decorator
==============================

A package to easily decorate your function calls.

v1.1.0(2y ago)13913.0k↓35%3[1 PRs](https://github.com/imanghafoori1/laravel-decorator/pulls)MITPHPPHP 7.4.\*|8.0.\*|8.1.\*|8.2.\*|8.3.\*|8.4.\*

Since Jan 9Pushed 2y ago5 watchersCompare

[ Source](https://github.com/imanghafoori1/laravel-decorator)[ Packagist](https://packagist.org/packages/imanghafoori/laravel-decorator)[ Docs](https://github.com/imanghafoori1/laravel-decorator)[ RSS](/packages/imanghafoori-laravel-decorator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (4)Versions (14)Used By (0)

🎄 Laravel Decorator
===================

[](#christmas_tree-laravel-decorator)

### Decorator pattern in Laravel apps

[](#decorator-pattern-in-laravel-apps)

[![Quality Score](https://camo.githubusercontent.com/8a15ca733d54466242907be73347f6f857f5b04b259f6acb168ada9d3b924316/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f696d616e676861666f6f7269312f6c61726176656c2d6465636f7261746f722e7376673f7374796c653d726f756e642d737175617265)](https://scrutinizer-ci.com/g/imanghafoori1/laravel-decorator)[![StyleCI](https://camo.githubusercontent.com/194bacd96605f9b7698621634ffa97e6a1c7cc2b434a7901b540b4cf78e0f42b/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3136343639393337312f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/164699371)[![Software License](https://camo.githubusercontent.com/d681f8298dd4e83c020388da14ca0ae0cad96f76342ae9108f2816e19b2b8daa/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d726f756e642d737175617265)](LICENSE.md)[![Check Imports](https://github.com/imanghafoori1/laravel-decorator/actions/workflows/check_imports.yml/badge.svg?branch=master)](https://github.com/imanghafoori1/laravel-decorator/actions/workflows/check_imports.yml)[![Latest Stable Version](https://camo.githubusercontent.com/6d5aba313934dd3330d600c503e894a9b12f1a2245ca23cffd22d8531b899983/68747470733a2f2f706f7365722e707567782e6f72672f696d616e676861666f6f72692f6c61726176656c2d6465636f7261746f722f762f737461626c65)](https://packagist.org/packages/imanghafoori/laravel-decorator)[![Code Coverage](https://camo.githubusercontent.com/18b73dd9a10809e6bd281d407e759f4cfdc46befd56a3b34e3ab448895fdc49a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f696d616e676861666f6f7269312f6c61726176656c2d6465636f7261746f722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/imanghafoori1/laravel-decorator/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/848cce53b5a596297140a9b434552783ea027ce10a467ecd4fb0b1c3ee185412/68747470733a2f2f706f7365722e707567782e6f72672f696d616e676861666f6f72692f6c61726176656c2d6465636f7261746f722f646f776e6c6f616473)](https://packagist.org/packages/imanghafoori/laravel-decorator)

**Made with ❤️ for smart clean coders**

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

[](#a-try-to-port-decorator-feature-from-python-language-to-laravel-framework)

[![python-and-prey](https://user-images.githubusercontent.com/6961695/51078481-a2ad9300-16ca-11e9-8bf2-1d4ed214e030.jpg)](https://user-images.githubusercontent.com/6961695/51078481-a2ad9300-16ca-11e9-8bf2-1d4ed214e030.jpg)

### 🚚 Installation:

[](#truck-installation)

```
composer require imanghafoori/laravel-decorator

```

### What is a `"Decorator"` ❓

[](#what-is-a-decorator-question)

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 ?!

[](#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:

[](#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 `UserController`which 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):

```
