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

feat(symfony): add getOperation Expression Language function on Mercure topics #5854

Conversation

vincentchalamon
Copy link
Contributor

@vincentchalamon vincentchalamon commented Sep 28, 2023

Q A
Branch? 3.3
Tickets N/A
License MIT
Doc PR TODO

Description

When using Mercure on a resource, it's possible to specify multiple topics using Symfony Expression Language:

#[ApiResource(
    mercure: [
        'topics' => [
            '@=iri(object)',
            '@=iri(object.getOwner()) ~ "/?topic=" ~ escape(iri(object))',
            '@=iri(object, '.UrlGeneratorInterface::ABS_PATH.')', // you can also change the reference type
            'https://example.com/books/1',
        ],
    ],
)]

(source: https://api-platform.com/docs/core/mercure/#dispatching-restrictive-updates-security-mode)

But the iri Expression Language function does not allow to send the second argument of IriConverter::getIriFromResource ($operation), which will just retrieve the first Get operation detected on the resource. While using multiple operations, it should be possible to specify which operation to use to generate the IRI.

Proposition

Considering the following resource with multiple operations:

#[ApiResource(
    operations: [
        new Get(uriTemplate: '/foos/{id}{._format}'),
        new Get(uriTemplate: '/foos/bars/{id}{._format}'),
        new Post(uriTemplate: '/foos{._format}'),
    ]
)]

On POST /foos request, I want to send a Mercure Update to both Get operations identified by their uriTemplate.

Introducing getOperation Expression Language function (currently only) available on Mercure topics feature:

#[ApiResource(
    // ...
    mercure: [
        'topics' => [
            '@=iri(object)',
            '@=iri(object, '.UrlGeneratorInterface::ABS_URL.', getOperation(object, "/foos/bars/{id}{._format}"))'
        ],
    ]
)]

Note: the object variable is required as getOperation first argument to retrieve the resource class. It's also possible to refer to a class name directly (e.g.: using DTO):

'@=iri(object, '.UrlGeneratorInterface::ABS_URL.', getOperation('.OutputDto::class.', "/a/custom/dtos/{id}{._format}"))'

@vincentchalamon vincentchalamon self-assigned this Sep 28, 2023
@vincentchalamon vincentchalamon force-pushed the feat/mercure-topics-item-uri-template branch 4 times, most recently from 1fe48f1 to e1a3a7b Compare October 5, 2023 09:46
@vincentchalamon vincentchalamon force-pushed the feat/mercure-topics-item-uri-template branch from e1a3a7b to 8c9fc05 Compare December 1, 2023 11:28
@vincentchalamon vincentchalamon force-pushed the feat/mercure-topics-item-uri-template branch 2 times, most recently from 061e38b to 867ef09 Compare December 18, 2023 14:37
@vincentchalamon vincentchalamon marked this pull request as ready for review December 18, 2023 14:42
@vincentchalamon vincentchalamon force-pushed the feat/mercure-topics-item-uri-template branch 2 times, most recently from 38e3039 to eb4c115 Compare February 2, 2024 14:14
@vincentchalamon
Copy link
Contributor Author

@soyuka is it ok to merge this one too?

@vincentchalamon vincentchalamon force-pushed the feat/mercure-topics-item-uri-template branch from eb4c115 to 74b576d Compare February 5, 2024 08:18
@vincentchalamon vincentchalamon force-pushed the feat/mercure-topics-item-uri-template branch from 74b576d to 2411ab5 Compare February 5, 2024 08:22
@soyuka soyuka merged commit 3d1428e into api-platform:main Mar 4, 2024
71 of 72 checks passed
@vincentchalamon vincentchalamon deleted the feat/mercure-topics-item-uri-template branch March 4, 2024 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants