ascetik/callapsule

A way to encapsulate callables

v0.4.0 2023-11-16 18:22 UTC

This package is auto-updated.

Last update: 2024-05-16 19:42:04 UTC


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.