goaop / framework

:gem: Go! AOP PHP - modern aspect-oriented framework for the new level of software development
go.aopphp.com
MIT License
1.66k stars 163 forks source link

[Feature] Add private properties interception #412

Closed lisachenko closed 5 years ago

lisachenko commented 5 years ago

This PR introduces an ability to intercept access to private properties in classes. We use closure binding in generated proxy child class and bind it to the parent scope so we gain a control over private properties of the class as well.

Here is an example of generated constructor for the Demo\Example\PropertyDemo proxy class:

    public function __construct()
    {
        $accessor = function(array &$propertyStorage, object $target) {
            $propertyStorage = [
                'publicProperty' => &$target->publicProperty,
                'protectedProperty' => &$target->protectedProperty,
                'privateProperty' => &$target->privateProperty,
                'indirectModificationCheck' => &$target->indirectModificationCheck
            ];
            unset(
                $target->publicProperty,
                $target->protectedProperty,
                $target->privateProperty,
                $target->indirectModificationCheck
            );
        };
        ($accessor->bindTo($this, parent::class))($this->__properties, $this);
        parent::__construct();
    }

As we store references to properties from given original object instance, we can read and write them via aspects without additional code logic.

Feature was requested in #411 by @jakzal

jakzal commented 5 years ago

This was quick! Thank you for your hard work! 🍺

With this PR, my code to set private's property value from an aspect works now:

            $setter = \Closure::bind(function ($object, $value) use ($field) {
                $object->$field = $value;
            }, null, \get_parent_class($fieldAccess->getThis()));
            $setter($fieldAccess->getThis(), 'some value');

Not sure if you expect the below code to work as well ('cause it doesn't):

            $fieldAccess->getField()->setAccessible(true);
            $fieldAccess->getField()->setValue($fieldAccess->getThis(), 'some value');
scrutinizer-notifier commented 5 years ago

The inspection completed: 1 updated code elements

jakzal commented 4 years ago

@lisachenko any plans to release this feature? :)

lisachenko commented 4 years ago

3.0.0-RC1 is ready to test, if it is ok, then 3.0.0 will be released soon