Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Turbo] add options to EventSource Mercure
  • Loading branch information
ytilotti committed Apr 25, 2024
commit f4c918e59820fc7f8da57f9f5120c91b1bac4db6
4 changes: 4 additions & 0 deletions src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public function __construct(HubInterface $hub, StimulusHelper|StimulusTwigExtens
$this->stimulusHelper = $stimulus;
}

/**
* @param string|object $topic
* @param array<string,mixed> $eventSourceOptions
*/
public function renderTurboStreamListen(Environment $env, $topic, array $eventSourceOptions = []): string
{
if (\is_object($topic)) {
Expand Down
3 changes: 2 additions & 1 deletion src/Turbo/src/Twig/TurboStreamListenRendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface TurboStreamListenRendererInterface
{
/**
* @param string|object $topic
* @param array<string,mixed> $eventSourceOptions
*/
public function renderTurboStreamListen(Environment $env, $topic, array $eventSourceOptions): string;
public function renderTurboStreamListen(Environment $env, $topic, array $eventSourceOptions = []): string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cannot add parameter on an interface, even with default values, without breaking existing implementation.

interface FooInterface
{
    public function bar(string $a, array $eventSourceOptions = []): string;
}

class AppFoo implements FooInterface
{
    // existing method matching previous interface
    public function bar(string $a): string
    {
        return $a;
    }
}

This throws a PHP Fatal error as a the concrete implementation is not compatible anymore (see here).

Fatal error: Declaration of AppFoo::bar(string $a): string must be compatible with FooInterface::bar(string $a, array $eventSourceOptions = []): string in php-wasm run script on line 11

So to make this change in a BC way, you need:

  • to comment the new argument in the interface
  • to use func_get_args to fetch the value in the concrete class

You can see here a recent example of Symfony PR with similar BC layer: symfony/symfony#54531

Poke me if you need a hand here!

}
1 change: 1 addition & 0 deletions src/Turbo/src/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function getFunctions(): iterable

/**
* @param object|string $topic
* @param array<string,mixed> $eventSourceOptions
*/
public function turboStreamListen(Environment $env, $topic, ?string $transport = null, array $eventSourceOptions = []): string
{
Expand Down