Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using __call in RuntimeExtensionInterface #3631

Open
nlemoine opened this issue Jan 19, 2022 · 0 comments
Open

Using __call in RuntimeExtensionInterface #3631

nlemoine opened this issue Jan 19, 2022 · 0 comments

Comments

@nlemoine
Copy link

nlemoine commented Jan 19, 2022

Hi!

I'm facing an issue related to #3451

Consider the following example:

class SomeExtension extends AbstractExtension
{
    private array $filters = [
         'filter1',
         'filter2',
         'filter3',
         // and so on...
    ];

    public function getFilters(): array
    {
        $filters = [];
        foreach ($this->filters as $filter) {
            $filters[] = new TwigFilter(
                $filter,
                [SomeRuntime::class, $filter], // Don't work
                // ["SomeRuntime::$filter"], // Don't work either
                ['is_variadic' => true]
            );
        }

        return $filters;
    }
}

class SomeRuntime implements RuntimeExtensionInterface
{
    private Service $service;

    public function __construct(Service $service)
    {
        $this->service = $service;
    }

    public function __call($name, array $arguments = [])
    {
        $object = $this->service->create($arguments[0]);
        return $object->$name(...$arguments)
    }
}

Note that in my real world example, I make use of the ContainerRuntimeLoader to inject Service in my runtime but I don't think it's relevant here.

Sadly you can't use __call in a RuntimeExtensionInterface because of

if (!method_exists($callable[0], $callable[1])) {
// __call()
return [null, []];
}

Whereas this approach works fine if I use a regular AbstractExtension, not a RuntimeExtensionInterface extension.

It would be great that runtime extension handles this use case too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants