SyliusResourceBundle icon indicating copy to clipboard operation
SyliusResourceBundle copied to clipboard

ResourceController: Dispatch event with data before giving it to `render()`

Open loevgaard opened this issue 3 years ago • 1 comments
trafficstars

Here is the showAction from the ResourceController:

public function showAction(Request $request): Response
{
    $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);

    $this->isGrantedOr403($configuration, ResourceActions::SHOW);
    $resource = $this->findOr404($configuration);

    $event = $this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource);
    $eventResponse = $event->getResponse();
    if (null !== $eventResponse) {
        return $eventResponse;
    }

    if ($configuration->isHtmlRequest()) {
        return $this->render($configuration->getTemplate(ResourceActions::SHOW . '.html'), [
            'configuration' => $configuration,
            'metadata' => $this->metadata,
            'resource' => $resource,
            $this->metadata->getName() => $resource,
        ]);
    }

    return $this->createRestView($configuration, $resource);
}

What if it looked something like this:

public function showAction(Request $request): Response
{
    $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);

    $this->isGrantedOr403($configuration, ResourceActions::SHOW);
    $resource = $this->findOr404($configuration);

    $event = $this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource);
    $eventResponse = $event->getResponse();
    if (null !== $eventResponse) {
        return $eventResponse;
    }

    $data = [
        'configuration' => $configuration,
        'metadata' => $this->metadata,
        'resource' => $resource,
        $this->metadata->getName() => $resource,
    ];

    $event = $this->eventDispatcher->dispatch(..., $data);

    if ($configuration->isHtmlRequest()) {
        return $this->render($configuration->getTemplate(ResourceActions::SHOW . '.html'), $event->getData());
    }

    return $this->createRestView($configuration, $resource);
}

This would allow users to provide more data to the template without having to override ResourceController or using Twig functions to get what they need.

loevgaard avatar Mar 24 '22 11:03 loevgaard

What version of Aleph are you using? I think this particular issue was fixed since 0.4.5-alpha1

gsnewmark avatar May 03 '18 09:05 gsnewmark

It looks like this was fixed in https://github.com/ztellman/aleph/pull/337, since 0.4.5-alpha1 as @gsnewmark says.

This makes aleph client unusable on JVM 9/10 which I consider serious. I can use :jvm-opts ["--add-modules" "java.xml.bind”] option in project.clj, but I would like the bug to be fixed

I don't think this was your intent, but these comments come across as demanding. Something like this could have been a better way to state your issue:

When upgrading my JVM to 10.0.1 (or even 9.0.1) I get an error about the javax.xml.bind.DatatypeConverter class being missing. [Add stack trace here] Adding :jvm-opts ["--add-modules" "java.xml.bind”] to my project.clj adds that class back, but if it's possible to update aleph to not depend on this module, that would be easier to use.

danielcompton avatar May 03 '18 09:05 danielcompton

I am using 0.4.5-alpha6

lyriccoder avatar May 04 '18 12:05 lyriccoder

Based on your stacktrace it's problem in http-kit, not Aleph :thinking:

Caused by:
java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter, compiling:(org/httpkit/client.clj:1:1) 

(source here is org/httpkit/client.clj)

Do you have both these libs on your classpath?

gsnewmark avatar May 04 '18 12:05 gsnewmark

Sorry for this thread. httpkit was in project.clj You can close the ticket

lyriccoder avatar Jun 01 '18 15:06 lyriccoder

@lyriccoder http-kit 2.3.0 resolves the JDK 9 issue.

tirkarthi avatar Jun 05 '18 12:06 tirkarthi