conduit-innovation / gorilla-claw

WordPress action / filter runtime toolkit library, with monkey-patching capabilities.
MIT License
14 stars 0 forks source link

feature: intercept object access #57

Open talss89 opened 1 year ago

talss89 commented 1 year ago

It should be possible to intercept calls / accesses made by a handler to $this, without replacing the original handler.

This means we can register on_call type events that are fired when a filter / action handler tries to access it's own $this. We can then either return a new value to skip execution of the $this->some_method() call, or return null to allow the call to continue.

This is a huge rabbit hole, and will need some thought. In theory we could pre and post process any object accessor similar to how we inject().

Branch feature/intercept tracks this feature.

talss89 commented 1 year ago

May need to refactor HookProxy a bit for this. Detecting calls doesn't require a proxy, but detecting property access does.

Need to ensure HookProxy can proxy another HookProxy recursively too. Unsure if this works currently - not tested.

talss89 commented 1 year ago

Recursive HookProxy test now in branch, and is passing.

    public function testRecursiveProxy() {
        $obj = new MockClass(3);
        $proxy_1 = new HookProxy(function($input) {return $input . '-proxy_1';}, $obj);
        $proxy_2 = new HookProxy(function($input) {return $input . '-proxy_2' . '-' . $this->{'id'};}, $proxy_1);

        $this->assertEquals("test-proxy_2-3", $proxy_2->___cb("test"));
    }
}