GithubHelp home page GithubHelp logo

decorator-laravel's Introduction

Decorator Laravel

测试覆盖率

Decorate your function/method by using middlewares in Laravel.

Installation via Composer

composer require istianxin/decorator-laravel

Quickstart

Define Middleware

You can define a middleware similar to Laravel Middleware. It should be noticed that the first parameter of th handle method is not \Illuminate\Http\Request, but an array the parameters of the method/function to be decorated. Except for this, everything is the same as Laravel Middleware.

Here is an example:

class MultiplicationMiddleware
{
    public function handle($data, $next, $factor = 1)
    {
        return $next($data) * $factor;
    }
}

Callback And Parameters

Except for middleware, decorator accepts two other parameters, callback and parameters, which are also the first two parameters of app()->call().

Callback is a callable, which can be invoked by app()->call() and call_user_func_array(), such as: DummyClass@method, a closure, [Dummyclass::class,staticMethod], [new DummyClass(), method], DummyClass::staticMethod.

Parameters is an array of parameters of the callbale.

Example

Single Middleware

$class = new class {
    public function add($a, $b)
    {
        return $a + $b;
    }
};
$a = 1;
$b = 2;
$factor = 3;

$decorator = new Decorator();
// classname with parameter
$middleware = MultiplicationMiddleware::class . ':' . $factor;
$result = $decorator->setCallback([$class, 'add'])
    ->setMiddleware($middleware)
    ->setParameters([$a, $b])
    ->decorate();
echo $result; // 9

Multiple Middleware

$decorator->setMiddleware([
    $object_middleware1,
    middleware2::class,
    middleware3:class:param1,param2,
    $closure_middleware4
]);

or

$decorator->setMiddleware([
    $object_middleware1,
    middleware2::class
])->appendMiddleware([
    middleware3:class:param1,param2,
    $closure_middleware4
])

See more TestCase.

decorator-laravel's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.