thephpleague/tactician

[RFC] Chained Inflector

Closed this issue · 4 comments

Would you be interested in adding a chained inflector?

It might be useful when you for example use 3rd-party bundle with registered command handler based on HandleInflector, but your project is configured with another inflector (e.g. InvokeInflector).

So having the chain inflector will solve this issue.

As in, looking for the exception then trying the next inflector in a list?

I thought about something like this

class ChainInflector implements MethodNameInflector
{
    public function __construct(MethodNameInflector ...$inflectors)
    {
        $this->inflectors = $inflectors;
    }

    public function inflect($command, $commandHandler)
    {
        foreach ($this->inflector as $inflector) {
            $methodName = $inflector->inflect($command, $commandHandler);

            if (method_exists($commandHandler, $methodName)) {
                return $methodName;
            }
        }

        return null;
    }
}

This has come up a couple times before, so I'm okay with it provided it comes with a really good docblock explaining the usecase and if it throws an exception instead of returning null. :)

This one never materialized, so I'm going to close the issue for now. It's still a fair idea and I want to keep it in mind with Tactician2 and some kind of router system baked into core but I'm probably not going to do anything with 1.x unless there's a working PR :)

Thanks a lot for opening the issue though and much ❤️ for using Tactician :)