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

Would you consider supporting DI for the presenters? #52

Open
judgej opened this issue Dec 5, 2022 · 0 comments
Open

Would you consider supporting DI for the presenters? #52

judgej opened this issue Dec 5, 2022 · 0 comments

Comments

@judgej
Copy link

judgej commented Dec 5, 2022

With many other presenter packages out there, I keep coming back to this one for its simplicity and flexibility (others seem to limit themselves to much more restricted use-cases).

Anyway, tried adding a service to a presenter constructor, since the service contains functionality used across a number of the presenter methods:

public function __construct($entity, protected OrderService $orderService)
{
    parent::__construct($entity);
}

This did not work, as the additional parameter was not being set in PresentableTrait:

$this->presenterInstance = new $this->presenter($this);

So this tried this in PresentableTrait instead, and it worked a treat:

$this->presenterInstance = app()->make($this->presenter, ['entity' => $this]); 

Now my order presenter class has $this->orderService at its disposal.

Now, I know I can extend the trait with this fix (or something similar - this is first draft to check viability) for myself, but is this something that could be useful for this package? It should be possible for it to work across all the supported Laravel versions.

Having to include $entity to pass up to the parent is, unfortunately, a little messy.

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

No branches or pull requests

1 participant