定制 magdicom/hooks 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

magdicom/hooks

Composer 安装命令:

composer require magdicom/hooks

包简介

A lightweight PHP action hooks package.

README 文档

README

Latest Version on Packagist Tests Total Downloads

Inspired by WordPress, action hooks are functions that you define to be triggered in specific places of your code, it helps you maintain an organized code by slicing giant code blocks into separated files and/or classes.

Installation

You can install the package via composer:

composer require magdicom/hooks

Usage

Quick Start

use Magdicom\Hooks;

$hooks = new Hooks();

# Register our functions
$hooks->register("Greetings", function($vars){
    return "Hi There,";
}, 1);

$hooks->register("Greetings", function($vars){
    return "This is the second line of greetings!";
}, 2);

# Later we run it
echo $hooks->all("Greetings")->toString("<br>");

The above example will output

Hi There,
This is the second line of greetings!

Output

When you call any of all, first or last methods, the corresponding hook functions will be executed and their output will be saved in a special property to be exported later using toString or toArray methods.

Callbacks

Closure

$hooks->register("Callback", function($vars) {
    return "Closure";
});

Function Name

function simple_function_name($vars){
    //
}

$hooks->register("Callback", "simple_function_name");

Object Method

class FooBar {
    public function methodName($vars){
        //
    }
}

$object = new FooBar;

$hooks->register("Callback", [$object, 'methodName']);

or

$hooks->register("Callback", [(new FooBar), 'methodName']);

Static Method

class FooBar {
    public static function staticMethodName($vars){
        //
    }
}

$hooks->register("Callback", ['FooBar', 'staticMethodName']);

in case this is not a static method, an object will be created and the provided method will be called.

Parameters

With each of hook callback functions execution an array of parameters could be passed to it to help it perform the required action.

Global Parameters

Global parameters could be defined using setParameter and setParameters methods and these parameters will be available across all hook points and callbacks.

Scoped parameters

The opposite of global parameters, the scoped parameters only available for the specified action hook point, and could be provided as the second argument of all, first and last methods.

When you provide it as an array, the values of scoped parameters will temporarily replace (similar key entries) global parameters and passed to the register methods' callback as a merged array.

When the parameter provided as a class object it will be accessible from the register method as the first argument, and the global parameters will be accessible via the second argument.

class FooBarBaz {
    public $id;

    public function __construct(int $id){
        $this->id = $id;
    }
}

$hooks = new Hooks();

$hooks->setParameters([
    "name" => "Bar",
]);

$hooks->register("ParameterAsObject", function ($fooBarBaz, $params) {
    return [$fooBarBaz->id, $params['name']];
});

echo $hooks->all("ParameterAsObject", (new FooBarBaz(100))->toString("\n");

// Output will be
100
Bar

Priority

When you need to ensure that certain hook functions should be executed in sequence order, here it comes $priority which is the 3rd and last argument of register method.

Methods

__construct

$hooks = new Hooks(?array $parameters);

The class constructor method will optionally accept a name, value pair array.

register

$hooks->register(string $hookName, array|callable $callback, ?int $priority): self

Register all your hook functions via this method:

  • $hookName this can be anything you want, its like a group name where all other related action hook functions will be attached to.
  • $callback only accepts callable functions.
  • $priority (optional) used to sort callbacks before being executed.

all

$hooks->all(string $hookName, array|object|null $parameters): self

Will execute all callback functions of the specified hook name, by default it will return the output as string, check output section for more options.

  • $hookName the hook name you want to execute its callback functions.
  • $parameters optional key, value pair array (or object) that you want to provide for all callback functions related to the same hook point.

Please Note: parameters provided via this method will be available only in the scope of the specified hook point, to specify global parameters use setParameter, setParameters methods instead.

first

$hooks->first(string $hookName, array|object|null $parameters): self

Similar to all method in every aspect with the exception that only the first callback (after sorting) will be executed.

last

$hooks->last(string $hookName, array|object|null $parameters): self

Similar to all method in every aspect with the exception that only the last callback (after sorting) will be executed.

toArray

$hooks->toArray(): array

Will return output of the last executed hook name functions as an array.

toString

$hooks->toString(?string $separator): string

Will return output of the last executed hook name functions as one string.

  • $separator could be used to separate the output as you need (e.g: "\n", "<br>").

setParameter

$hooks->setParameter(string $name, mixed $value): self

Use this method to define a parameter that will be accessible from any hook function.

  • $name name of the parameter.
  • $value value of the parameter could be string, array or even an object.

P.S: if the parameter already defined then its old value will be replaced by the value provided here.

setParameters

$hooks->setParameters(array $parameters): self

Same as setParameter but here it accepts a name, value pair array as its only argument.

setSourceFile

$hooks->setSourceFile(?string $path): self

Used in conjunction with the debug method.

debug

$hooks->debug(callable|null $callback): self

To enable the debug feature you need to call this method by providing a callback function, this function should accept a single argument that will be the debug info/message.

$hooks->debug(function($message){
    // Will print debug message(s)
    echo $message . PHP_EOL;
});

$hooks->setSourceFile("/path/to/file/filename.php");

$hooks->register("Greetings", "FooBar::log");

$hooks->all("Greetings");

will output

+ Added Source File: /path/to/file/filename.php
+ Hook Point: Greetings, New Callback Defined:
        -- Source: /path/to/file/filename.php
        -- Callback: FooBar::log
        -- Priority: 1
+ Hook Point: Greetings, Callback Functions Sorted!
+ Hook Point: Greetings, Output Generated For All Callback Functions!

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

magdicom/hooks 适用场景与选型建议

magdicom/hooks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 185 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 12 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「hooks」 「magdicom」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 magdicom/hooks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 magdicom/hooks 我们能提供哪些服务?
定制开发 / 二次开发

基于 magdicom/hooks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 185
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-12-29