nette / application

🏆 A full-stack component-based MVC kernel for PHP that helps you write powerful and modern web applications. Write less, have cleaner code and your work will bring you joy.
https://doc.nette.org/application
Other
409 stars 105 forks source link

exception in application: pass reference to previous presenter #317

Closed dakur closed 1 year ago

dakur commented 1 year ago

I need separate handling of bad request of presenters that implement some interface. To check this in error presenter, I need instance of the original presenter therefore this PR. It can be probably achieved also via reflection, but this it seems cleaner to me.

todo:

mabar commented 1 year ago

So, you don't need an instance?

class ErrorPresenter extends Presenter 
{
    public function __construct(private readonly IPresenterFactory $presenterFactory) {}

    public function actionDefault(Throwable $exception, Request|null $request): void
    {
        $presenterName = $request?->getPresenterName() ?? null;
        if ($presenterName !== null) {
            $presenterClass = $this->presenterFactory->getPresenterClass($presenterName);
            dump(is_a($presenterClass, CheckedInterface::class, true));
        }
    }

}
dg commented 1 year ago

Isn't it easier to inject an Application into the errorpresenter?

dakur commented 1 year ago

So, you don't need an instance?

No. Even though your solution work, it seems strange to me to work with presenter factory inside of presenter. Extracting the presenter router<->class name translation into separate single responsibility service would probably solve that. Does it make sense?

Isn't it easier to inject an Application into the errorpresenter?

What would I do then? I can see getRequests() there but still you have to call $presenterFactory->getPresenterClass(). Not to mention going into injection loop..

My proposal seems quite straightforward to me, is there any problem with it?

dakur commented 1 year ago

Thank you!