Skip to content

Commit 07fdb47

Browse files
committed
Return event output in PHP SDK
1 parent e2a23f8 commit 07fdb47

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

sdk/php/src/ServerSentEventGenerator.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function sendHeaders(): void
7171
}
7272

7373
/**
74-
* Merges HTML fragments into the DOM.
74+
* Merges HTML fragments into the DOM and returns the resulting output.
7575
*
7676
* @param array{
7777
* selector?: string|null,
@@ -81,67 +81,71 @@ public function sendHeaders(): void
8181
* retryDuration?: int|null,
8282
* } $options
8383
*/
84-
public function mergeFragments(string $fragments, array $options = []): void
84+
public function mergeFragments(string $fragments, array $options = []): string
8585
{
86-
$this->sendEvent(new MergeFragments($fragments, $options));
86+
return $this->sendEvent(new MergeFragments($fragments, $options));
8787
}
8888

8989
/**
90-
* Removes HTML fragments from the DOM.
90+
* Removes HTML fragments from the DOM and returns the resulting output.
9191
*
9292
* @param array{
9393
* eventId?: string|null,
9494
* retryDuration?: int|null,
9595
* } $options
9696
*/
97-
public function removeFragments(string $selector, array $options = []): void
97+
public function removeFragments(string $selector, array $options = []): string
9898
{
99-
$this->sendEvent(new RemoveFragments($selector, $options));
99+
return $this->sendEvent(new RemoveFragments($selector, $options));
100100
}
101101

102102
/**
103-
* Merges signals into the signals.
103+
* Merges signals and returns the resulting output.
104104
*/
105-
public function mergeSignals(array|string $signals, array $options = []): void
105+
public function mergeSignals(array|string $signals, array $options = []): string
106106
{
107-
$this->sendEvent(new MergeSignals($signals, $options));
107+
return $this->sendEvent(new MergeSignals($signals, $options));
108108
}
109109

110110
/**
111-
* Removes signal paths from the signals.
111+
* Removes signal paths and returns the resulting output.
112112
*/
113-
public function removeSignals(array $paths, array $options = []): void
113+
public function removeSignals(array $paths, array $options = []): string
114114
{
115-
$this->sendEvent(new RemoveSignals($paths, $options));
115+
return $this->sendEvent(new RemoveSignals($paths, $options));
116116
}
117117

118118
/**
119-
* Executes JavaScript in the browser.
119+
* Executes JavaScript in the browser and returns the resulting output.
120120
*/
121-
public function executeScript(string $script, array $options = []): void
121+
public function executeScript(string $script, array $options = []): string
122122
{
123-
$this->sendEvent(new ExecuteScript($script, $options));
123+
return $this->sendEvent(new ExecuteScript($script, $options));
124124
}
125125

126126
/**
127-
* Redirects the browser by setting the location to the provided URI.
127+
* Redirects the browser by setting the location to the provided URI and returns the resulting output.
128128
*/
129-
public function location(string $uri, array $options = []): void
129+
public function location(string $uri, array $options = []): string
130130
{
131-
$script = 'setTimeout(() => window.location = "' . $uri . '")';
132-
$this->executeScript($script, $options);
131+
$script = "setTimeout(() => window.location = '$uri')";
132+
133+
return $this->executeScript($script, $options);
133134
}
134135

135136
/**
136-
* Sends an event and flushes the output buffer.
137+
* Sends an event, flushes the output buffer and returns the resulting output.
137138
*/
138-
protected function sendEvent(EventInterface $event): void
139+
protected function sendEvent(EventInterface $event): string
139140
{
140-
echo $event->getOutput();
141+
$output = $event->getOutput();
142+
echo $output;
141143

142144
if (ob_get_contents()) {
143145
ob_end_flush();
144146
}
145147
flush();
148+
149+
return $output;
146150
}
147151
}

0 commit comments

Comments
 (0)