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

[ExpressionLanguage] Add function service #1867

Open
seb-jean opened this issue May 16, 2024 · 11 comments
Open

[ExpressionLanguage] Add function service #1867

seb-jean opened this issue May 16, 2024 · 11 comments
Labels
question Further information is requested Turbo

Comments

@seb-jean
Copy link
Contributor

Symfony version(s) affected

7.0.7

Description

I have an error :

The function "service" does not exist around position 1 for expression `service('App\Service\Generator').generate('songs_by_artist_' ~ entity.getId())`.

How to reproduce

I have the following entity:

<?php

namespace App\Entity;

use App\Repository\BookRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\UX\Turbo\Attribute\Broadcast;

#[ORM\Entity(repositoryClass: BookRepository::class)]
#[Broadcast(topics: ["@=service('App\\Service\\Generator').generate('songs_by_artist_' ~ entity.getId())", 'books'], template: 'broadcast/Book.stream.html.twig', private: true)]
class Book
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $title = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): static
    {
        $this->title = $title;

        return $this;
    }
}

But when I create a Book entity, I get the following error:

The function "service" does not exist around position 1 for expression `service('App\Service\Generator').generate('songs_by_artist_' ~ entity.getId())`.

In my case, $this->functions is enum and constant : https://github.com/symfony/symfony/blob/7.1/src/Symfony/Component/ExpressionLanguage/Parser.php#L242

Possible Solution

No response

Additional Context

No response

@seb-jean seb-jean added the Bug Bug Fix label May 16, 2024
@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label May 16, 2024
@Kocal
Copy link
Contributor

Kocal commented May 28, 2024

I think this is more something related to https://github.com/symfony/ux

@xabbuh xabbuh transferred this issue from symfony/symfony May 28, 2024
@smnandre smnandre added question Further information is requested Turbo and removed Bug Bug Fix Status: Needs Review Needs to be reviewed labels May 28, 2024
@smnandre
Copy link
Collaborator

This has never been possible as i see it .. I believe expression language is used there to compose topic names based on the enttity props.

Or am i missing something ?

@seb-jean
Copy link
Contributor Author

Yes but I saw in the documentation below that it was possible to use the service function in expression languages: https://symfony.com/doc/current/service_container/expression_language.html

@smnandre
Copy link
Collaborator

... in the service container:)

@Kocal
Copy link
Contributor

Kocal commented May 28, 2024

Yes, but the ExpressionLanguage used in the service container's configuration is configured to access the Symfony Container (or at least something to fetch services from), which was never the case here with Symfony\UX\Turbo\Attribute\Broadcast attribute.

Maybe you can achieve what you want by tweaking the Symfony Container by using some Compiler Pass, or maybe you can go for an easier solution by moving your logic into a (static?) method in your entity.

@seb-jean
Copy link
Contributor Author

So, won't fix?

@Kocal
Copy link
Contributor

Kocal commented May 28, 2024

Maybe we can find another alternative, instead of passing the whole container, we can inject a service that could be configured under broadcard.topic_generator (or smth like that)?

@smnandre
Copy link
Collaborator

So, won't fix?

Maybe let's use another word than fix ... as this is a request for a feature (that has never existed in the first place, right ? 😅)

Could you give you the final usage you're looking after, so maybe we can suggest other ways to achieve it ?

@seb-jean
Copy link
Contributor Author

seb-jean commented May 29, 2024

Maybe we can find another alternative, instead of passing the whole container, we can inject a service that could be configured under broadcard.topic_generator (or smth like that)?

Yes, why not :).

Maybe let's use another word than fix ... as this is a request for a feature (that has never existed in the first place, right ? 😅)

Yes, it's true, you're right, sorry 😄.

Could you give you the final usage you're looking after, so maybe we can suggest other ways to achieve it ?

Currently, when I put:

<div id="book_{{ book.id }}" {{ turbo_stream_listen('book_detail_' ~ book.id) }}></div>

it becomes:

<div id="book_7" data-controller="symfony--ux-turbo--mercure-turbo-stream" data-symfony--ux-turbo--mercure-turbo-stream-topic-value="book_detail_7" data-symfony--ux-turbo--mercure-turbo-stream-hub-value="http://127.0.0.1:56215/.well-known/mercure"></div>

But with a generator, this will hash/encode the data-symfony--ux-turbo--mercure-turbo-stream-topic-value. It will then be more secure.

This line

<div id="book_{{ book.id }}" {{ turbo_stream_listen(('book_detail_' ~ book.id)|mercure_topic) }}></div>

will then become:

<div id="book_7" data-controller="symfony--ux-turbo--mercure-turbo-stream" data-symfony--ux-turbo--mercure-turbo-stream-topic-value="AiLvziwlkB5M9UpYKRyiHvDs/BK7t4T6B7CR48ropyA=" data-symfony--ux-turbo--mercure-turbo-stream-hub-value="http://127.0.0.1:56215/.well-known/mercure"></div>

@smnandre
Copy link
Collaborator

I like the idea, should it be something to add here or in the Mercure bundle ?

@seb-jean
Copy link
Contributor Author

You might as well do it directly in MercureBundle. But is there a downside?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Turbo
Projects
None yet
Development

No branches or pull requests

4 participants