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

Resolve classes if callable has no object-context #19

Open
filecage opened this issue Jul 1, 2019 · 0 comments
Open

Resolve classes if callable has no object-context #19

filecage opened this issue Jul 1, 2019 · 0 comments
Milestone

Comments

@filecage
Copy link
Owner

filecage commented Jul 1, 2019

It is unsupported to pass a callable to invoke() that is missing the object context:

if (is_array($callable) && count($callable) === 2) {
// todo: Creator should resolve the object if we know the class name
if (!is_object($callable[0])) {
throw new Unresolvable('Unable to handle invokation of object-context callable: no object given on callable index 0');
}

Creator should resolve the object (if it's non-static) and then call the invokable instead of throwing an exception.

Example test case that should pass:

class Foo {
    function __construct($bar) {
        $this->bar = $bar;
    }
}

class BarTransformer {
    function transform ($bar) {
        return strtoupper($bar);
    }
}

class AnyClass {
    static function bax (Foo $foo, BarTransformer $barTransformer) {
        return $barTransformer->transform($foo->bar);
    }

    function __construct(Foo $foo) {
        $this->foo = $foo;
    }

    function bar (BarTransformer $barTransformer) {
        return $barTransformer->transform($this->foo->bar);
    }
}

$creator = new Creator();
$creator->registerClassResource(new Foo('hello world'));

echo $creator->invoke(['AnyClass', 'bax']);
echo $creator->invoke(['AnyClass', 'bar']);

Should output

HELLO WORLD

twice, the first invocation should not register a class resource as the method is only called statically.

@filecage filecage added this to the 2.x milestone Dec 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant