code-distortion/di-caller

一个PHP包,用于调用可调用的、回调函数或钩子,通过依赖注入解决它们的参数

0.1.0 2024-09-11 14:36 UTC

This package is auto-updated.

Last update: 2024-09-11 15:05:37 UTC


README

Latest Version on Packagist PHP Version GitHub Workflow Status Buy The World a Tree Contributor Covenant

code-distortion/di-caller是一个PHP包,用于调用可调用的、回调函数或钩子,使用依赖注入来解决它们的参数。

此包在您提供一组可能的参数,但不知道回调函数实际上需要哪些参数时非常有用。

例如,当您正在开发一个允许调用者注册回调的包时。您可以使用此包来调用这些回调,传递您包想要提供的相关参数。

它不使用像框架中使用的依赖容器。它单独为每个可调用项解决参数。它不会存储它们供其他可调用项稍后使用。

目录

安装

通过composer安装此包

composer require code-distortion/di-caller

用法

使用此包有三个步骤

  • 创建一个DICaller实例,并将其传递一个可调用项
  • 通过依赖注入注册您想要使可调用项可用的参数
  • 使用->call()执行可调用项

您可以通过类型注册参数

use CodeDistortion\DICaller\DICaller;

$callable = fn(Request $request, User $user) => "hello $user->name ({$request->getIp()})";

$user = new User();
$request = new Request();
$shoppingCart = new ShoppingCart();

$result = DICaller::new($callable)
    ->registerByType($user)         // <<<
    ->registerByType($request)      // <<<
    ->registerByType($shoppingCart) // <<<
    ->call(); // 'hello Bob (192.168.1.1)'

您可以通过名称注册参数

use CodeDistortion\DICaller\DICaller;

$callable = fn($param1, $param2) => "$param1 $param2";

$result = DICaller::new($callable)
    ->registerByName('param1', 'hello') // <<<
    ->registerByName('param2', 'world') // <<<
    ->call(); // 'hello world'

您可以通过位置(从0开始)注册参数

use CodeDistortion\DICaller\DICaller;

$callable = fn($param1, $param2) => "$param1 $param2";

$result = DICaller::new($callable)
    ->registerByPosition(0, 'hello') // <<<
    ->registerByPosition(1, 'world') // <<<
    ->call(); // 'hello world'

联合类型受到支持

use CodeDistortion\DICaller\DICaller;

$callable = fn(int|float $param1) => $param1; // <<<

$result = DICaller::new($callable)
    ->registerByType(1.1) // <<<
    ->call(); // 1.1

注意:联合类型可能不会按照签名中编写的顺序处理,因为DICaller使用反射,这可能会改变顺序。

交集类型受到支持

use CodeDistortion\DICaller\DICaller;

$callable = fn(ParentClass&ChildClass $param1) => $param1; // <<<

$result = DICaller::new($callable)
    ->registerByType(new ChildClass()) // <<<
    ->call(); // ChildClass

可变参数受到支持,但被视为普通参数

use CodeDistortion\DICaller\DICaller;

$callable = fn(int $param1, int ...$param2) => func_get_args(); // <<<

$result = DICaller::new($callable)
    ->registerByName('param1', 1)
    ->registerByName('param2', 2)
    ->call(); // [1, 2]

验证

您可以使用->isCallable()检查可调用项实际上是否是callable,并且参数在调用->call()之前解决

use CodeDistortion\DICaller\DICaller;

$callable = fn($param1, $param2) => "$param1 $param2";

$caller = DICaller::new($callable);
if ($caller->isCallable()) { // false
    $result = $caller->call();
}

您可以使用->callIfPossible()在一步中检查和调用它

use CodeDistortion\DICaller\DICaller;

$callable = fn($param1, $param2) => "$param1 $param2";

$result = DICaller::new($callable)->callIfPossible(); // null

注意:如果调用不可行,这将返回null,这与返回null的成功调用无法区分。

异常

如果可调用项实际上不是callable,则在调用->call()时抛出DICallerInvalidCallableException

use CodeDistortion\DICaller\DICaller;

$callable = 'not a callable';

$result = DICaller::new($callable)->call(); // throws DICallerInvalidCallableException

如果参数无法解决,则在调用->call()时抛出DICallerUnresolvableParametersException

use CodeDistortion\DICaller\DICaller;

$callable = fn($param1, $param2) => "$param1 $param2";

$result = DICaller::new($callable)->call(); // throws DICallerUnresolvableParametersException

测试此包

  • 克隆此包:git clone https://github.com/code-distortion/di-caller.git .
  • 运行composer install来安装依赖项
  • 运行测试:composer test

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

SemVer

此库使用SemVer 2.0.0版本控制。这意味着对X的更改表示破坏性更改:0.0.X0.X.yX.y.z。当此库更改为版本1.0.0、2.0.0等时,并不表示它必然是一个值得注意的版本,它只是表示更改是破坏性的。

实物书

此包是实物书。如果您在生产环境中使用它,那么我们要求您为世界买一棵树以感谢我们的工作。通过为实物书森林做出贡献,您将为当地家庭创造就业机会,并恢复野生动物栖息地。

贡献

请参阅CONTRIBUTING获取详细信息。

行为准则

请参阅CODE_OF_CONDUCT获取详细信息。

安全

如果您发现任何与安全相关的问题,请发送电子邮件至tim@code-distortion.net,而不是使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。请参阅许可证文件获取更多信息。