auraphp / Aura.Di

Dependency Injection System
MIT License
349 stars 63 forks source link

Feature: mutations #176

Closed frederikbosch closed 5 years ago

frederikbosch commented 5 years ago

Adds a new feature. Allows class to be mutated after initialization. It can be seen as an abstracter method to modify the object after initialization compared to setters.

$di->mutations['Vendor\Package\Example'][] = $di->lazyNew(ExampleMutation::class);

class ExampleMutation implements MutationInterface
{
    public function __invoke(object $object): object
    {
        $object->setFoo('mutated');
        return $object;
    }
}
jakejohns commented 5 years ago

This is related to #147 , right?

frederikbosch commented 5 years ago

Right

frederikbosch commented 5 years ago

It can be used for all kinds of cases.

  1. It can structure code by to moving all $di->setters into one place.
  2. It allows complexer setters, e.g. $object->setMethod($arg1, $arg2);.
  3. It allows conditional setters, without instantiating other objects before construction of the object.
  4. It allows async decentralized modification of the object.

The first three are indeed also possible through a factory. The latter is the provider functionality I requested in #147.

harikt commented 5 years ago

Hey @frederikbosch ,

I think you want to merge 4.x branch to yours. Else the same commits are showing in the diff which makes things hard for review.

frederikbosch commented 5 years ago

I will rebase and update the PR.

frederikbosch commented 5 years ago

Done the rebase.

frederikbosch commented 5 years ago

@harikt I added some more docs and changed mutate in __invoke. Hopefully things are a little more clear now.

harikt commented 5 years ago

I am good with the changes.

@frederikbosch I hope you ok with waiting a bit for anyone else who love to review code.

frederikbosch commented 5 years ago

Sure, who would you like to review the code? Maybe we can add them as reviewers in this PR.

frederikbosch commented 5 years ago

@harikt Is there anything I can do to get this merged?

harikt commented 5 years ago

No. Let us merge it.