mehradsadeghi/laravel-decorator

该软件包已被废弃,不再维护。未建议替代软件包。

在不破坏现有代码库的情况下,装饰和扩展方法的功能。

v1.1.0 2020-07-23 04:50 UTC

This package is auto-updated.

Last update: 2021-05-23 06:44:20 UTC


README

在不破坏现有代码库的情况下,装饰和扩展方法的功能。

安装

$ composer require mehradsadeghi/laravel-decorator

用法

假设您有一个名为 Person 的类,其中有一个名为 getFullName 的方法,其输入和输出应进行装饰

class Person {

    public function makeFullName($firstName, $lastName)
    {
        return "$firstName $lastName";
    }
}

$person = new Person();
$person->makeFullName('mehrad', 'sadeghi'); // mehrad sadeghi

当使用 decorator 没有设置任何装饰时,makeFullName 方法的默认行为将保持不变

decorate([Person::class, 'makeFullName'], ['mehrad', 'sadeghi']); // mehrad sadeghi

为了装饰 makeFullName 方法

$decorator = function ($callable) {
    return function (...$params) use ($callable) {
    
        // decorating the inputs
        foreach($params as $key => $param) {
            $params[$key] = trim($param);
        }

        // real call to makeFullName method
        $output = app()->call($callable, $params);

        // decorating the output
        $output = strtoupper($output);

        return $output;
    };
};

decorator([Person::class, 'makeFullName'])->set($decorator);

注意,装饰器应该是一个有效的 PHP 可调用对象。因此,它可以是 Closure 或数组可调用对象,其定义如下

class PersonDecorator {

    public function decorateFullName($callable)
    {
        return function (...$params) use ($callable) {
        
            // decorating the inputs
            foreach($params as $key => $param) {
                $params[$key] = trim($param);
            }

            // real call to makeFullName method
            $output = app()->call($callable, $params);

            // decorating the output
            $output = strtoupper($output);

            return $output;
        };
    }
}

decorator([Person::class, 'makeFullName'])->set([PersonDecorator::class, 'decorateFullName']);

现在我们将装饰器分配给 makeFullName 方法。使用 decorate 辅助函数调用 makeFullName 将应用其装饰

decorate([Person::class, 'makeFullName'], ['  mehrad ', '     sadeghi ']); // MEHRAD SADEGHI

多个装饰器

您可以轻松地为方法设置多个装饰器

decorator([Person::class, 'makeFullName'])
        ->set(function($callable) {
            // decoration
        })
        ->set(function($callable) {
            // decoration
        });

或者

decorator([Person::class, 'makeFullName'])
    ->set([PersonDecorator::class, 'secondDecorator'])
    ->set([PersonDecorator::class, 'firstDecorator']);

忘记(移除)装饰器

您可以轻松地移除分配给可调用对象的装饰器。例如,假设我们有两个装饰器

class PersonDecorator {

    public function decorateInput($callable)
    {
        return function (...$params) use ($callable) {

            // decorating the inputs
            foreach($params as $key => $param) {
                $params[$key] = trim($param);
            }

            // real call to makeFullName method
            $output = app()->call($callable, $params);

            return $output;
        };
    }

    public function decorateOutput($callable)
    {
        return function (...$params) use ($callable) {

            // real call to makeFullName method
            $output = app()->call($callable, $params);

            // decorating the output
            $output = strtoupper($output);

            return $output;
        };
    }
}

decorator([Person::class, 'makeFullName'])
    ->set([PersonDecorator::class, 'decorateInput'])
    ->set([PersonDecorator::class, 'decorateOutput']);

调用 decorate 的输出将是

decorate([Person::class, 'makeFullName'], ['  mehrad ', '     sadeghi ']); // MEHRAD SADEGHI

然后为了移除 decorateOutput

decorator([Person::class, 'makeFullName'])
    ->forget([PersonDecorator::class, 'decorateOutput']);

调用 decorate 的输出将是

decorate([Person::class, 'makeFullName'], ['  mehrad ', '     sadeghi ']); // mehrad sadeghi

注意,要移除可调用对象的所有装饰,只需将 forget 参数留空

decorator([Person::class, 'makeFullName'])->forget();