ascetik/callapsule 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

ascetik/callapsule

最新稳定版本:v0.4.0

Composer 安装命令:

composer require ascetik/callapsule

包简介

A way to encapsulate callables

README 文档

README

A way to encapsulate a callable

Purpose

This package is made to encapsulate a callable of any type, providing the ability to handle them the same way. I needed this kind of package for routing or a service manager, any task to defer, to encapsulate callables correctly...

Release notes

v0.4.0

  • CallableType abstract getReflection() method.

Usage

Use the CallWrapper factory static methods to build a CallableType :

class Foo
{
    public function bar(){}
    public function __invoke(){}
    public static function biz(){}
}

// To wrap a Closure
$closureWrapper = CallWrapper::wrap(fn(string $name) => 'hello ' . $name);
// or
$closureWrapper = CallWrapper::wrapClosure(fn(string $name) => 'hello ' . $name);

// to wrap an instance and a method to call
$methodWrapper = CallWrapper::wrap([new Foo(), 'bar']);
// or
$methodWrapper = CallWrapper::wrapMethod(new Foo(), 'bar');

// to wrap a static method
$staticWrapper = CallWrapper::wrap([Foo::class, 'biz']);
// or
$staticWrapper = CallWrapper::wrapStatic(Foo::class, 'biz');

// To wrap an invokable class
$invokable = CallWrapper::wrap(new Foo());
// or
$invokable = CallWrapper::wrapInvokable(new Foo());

The use of this factory is the best choice to build and use a CallableType. Some checks make direct instanciation unavailable due to private constructors.

A CallableType is able to run the callable it holds. For external needs, it is able to give its callable back as is :

$callable = $closureWrapper->callable();
echo call_user_func($callable,' John'); // prints "hello John"

// or use it directly
echo $closureWrapper->apply(['name' => 'John']); // same result

CallableType methods

  • CallableType::apply(?iterable): mixed : execute callable and return the result
  • CallableType::action(): callable : return wrapped callable type-hinted as callable
  • CallableType::getCallable(): object : return wrapped callable fully type-hinted

Implementations

I could enumerate 4 use cases for a callable wrap :

  • a Closure
  • a class method, with an instance and the method to run
  • a static class method, with a class-string instead of an instance
  • an instance with __invoke magic method

If you can find more, feel free to use CallableType abstraction.

ClosureCall

Holds a Closure. Direct instanciation (public constructor)

No Errors/Exceptions thrown

InvokableCall

Holds an invokable instance. Static factory method for checks.

Throws an UninvokableClassException if the given instance does not implement __invoke() method.

MethodCall

Holds an instance and a method. Static factory method for checks.

Throws a MethodNotImplementedException if given method is not implemented.

getCallable method returns a ClassMethod instance with a get() method returning the callable array.

StaticCall

Holds a class name and a method name. Static factory for checks.

Throws a ClassNotFoundException if given class does not exist. Throws a MethodNotImplementedException if given method is not implemented.

getCallable method returns a ClassMethod instance with a get() method returning the callable array.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-10