generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from tonysm/multiple-turbo-stream-responses
Can build multiple turbo stream responses in a single response
- Loading branch information
Showing
4 changed files
with
98 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Tonysm\TurboLaravel\Http; | ||
|
||
use Illuminate\Contracts\Support\Responsable; | ||
use Illuminate\Support\Collection; | ||
|
||
class MultiplePendingTurboStreamResponse implements Responsable | ||
{ | ||
/** @var Collection|PendingTurboStreamResponse[] */ | ||
private Collection $pendingStreams; | ||
|
||
/** | ||
* @param Collection $pendingStreams | ||
*/ | ||
public function __construct($pendingStreams) | ||
{ | ||
$this->pendingStreams = collect($pendingStreams); | ||
} | ||
|
||
/** | ||
* @param array|Collection $pendingStreams | ||
* | ||
* @return self | ||
*/ | ||
public static function forStreams($pendingStreams): self | ||
{ | ||
return new self($pendingStreams); | ||
} | ||
|
||
/** | ||
* Create an HTTP response that represents the object. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
*/ | ||
public function toResponse($request) | ||
{ | ||
return TurboResponseFactory::makeStream( | ||
$this->pendingStreams | ||
->map(function (PendingTurboStreamResponse $pendingStream) { | ||
return $pendingStream->render(); | ||
}) | ||
->implode(PHP_EOL) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters