-
-
Notifications
You must be signed in to change notification settings - Fork 364
[Turbo] Add Helper/TurboStream::append() et al. methods #2196
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,97 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Turbo\Helper; | ||
|
||
/** | ||
* @see https://turbo.hotwired.dev/reference/streams | ||
*/ | ||
final class TurboStream | ||
{ | ||
/** | ||
* Appends to the element(s) designated by the target CSS selector. | ||
*/ | ||
public static function append(string $target, string $html): string | ||
{ | ||
return self::wrap('append', $target, $html); | ||
} | ||
|
||
/** | ||
* Prepends to the element(s) designated by the target CSS selector. | ||
*/ | ||
public static function prepend(string $target, string $html): string | ||
{ | ||
return self::wrap('prepend', $target, $html); | ||
} | ||
|
||
/** | ||
* Replaces the element(s) designated by the target CSS selector. | ||
*/ | ||
public static function replace(string $target, string $html, bool $morph = false): string | ||
{ | ||
return self::wrap('replace', $target, $html, $morph ? ' method="morph"' : ''); | ||
} | ||
|
||
/** | ||
* Updates the content of the element(s) designated by the target CSS selector. | ||
*/ | ||
public static function update(string $target, string $html, bool $morph = false): string | ||
{ | ||
return self::wrap('update', $target, $html, $morph ? ' method="morph"' : ''); | ||
} | ||
|
||
/** | ||
* Removes the element(s) designated by the target CSS selector. | ||
*/ | ||
public static function remove(string $target): string | ||
{ | ||
return \sprintf('<turbo-stream action="remove" targets="%s"></turbo-stream>', htmlspecialchars($target)); | ||
} | ||
|
||
/** | ||
* Inserts before the element(s) designated by the target CSS selector. | ||
*/ | ||
public static function before(string $target, string $html): string | ||
{ | ||
return self::wrap('before', $target, $html); | ||
} | ||
|
||
/** | ||
* Inserts after the element(s) designated by the target CSS selector. | ||
*/ | ||
public static function after(string $target, string $html): string | ||
{ | ||
return self::wrap('after', $target, $html); | ||
} | ||
|
||
/** | ||
* Initiates a Page Refresh to render new content with morphing. | ||
* | ||
* @see Initiates a Page Refresh to render new content with morphing. | ||
*/ | ||
public static function refresh(?string $requestId = null): string | ||
{ | ||
if (null === $requestId) { | ||
return '<turbo-stream action="refresh"></turbo-stream>'; | ||
} | ||
|
||
return \sprintf('<turbo-stream action="refresh" request-id="%s"></turbo-stream>', htmlspecialchars($requestId)); | ||
} | ||
|
||
private static function wrap(string $action, string $target, string $html, string $attr = ''): string | ||
{ | ||
return \sprintf(<<<EOHTML | ||
<turbo-stream action="%s" targets="%s"%s> | ||
<template>%s</template> | ||
</turbo-stream> | ||
EOHTML, $action, htmlspecialchars($target), $attr, $html); | ||
} | ||
} |
This file contains hidden or 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,107 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Turbo; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\UX\Turbo\Helper\TurboStream; | ||
|
||
class TurboStreamResponse extends Response | ||
{ | ||
public function __construct(?string $content = '', int $status = 200, array $headers = []) | ||
{ | ||
parent::__construct($content, $status, $headers); | ||
|
||
if (!$this->headers->has('Content-Type')) { | ||
$this->headers->set('Content-Type', TurboBundle::STREAM_MEDIA_TYPE); | ||
} | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function append(string $target, string $html): static | ||
{ | ||
$this->setContent($this->getContent().TurboStream::append($target, $html)); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function prepend(string $target, string $html): static | ||
{ | ||
$this->setContent($this->getContent().TurboStream::prepend($target, $html)); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function replace(string $target, string $html, bool $morph = false): static | ||
{ | ||
$this->setContent($this->getContent().TurboStream::replace($target, $html, $morph)); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function update(string $target, string $html, bool $morph = false): static | ||
{ | ||
$this->setContent($this->getContent().TurboStream::update($target, $html, $morph)); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function remove(string $target): static | ||
{ | ||
$this->setContent($this->getContent().TurboStream::remove($target)); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function before(string $target, string $html): static | ||
{ | ||
$this->setContent($this->getContent().TurboStream::before($target, $html)); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function after(string $target, string $html): static | ||
{ | ||
$this->setContent($this->getContent().TurboStream::after($target, $html)); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return $this | ||
*/ | ||
public function refresh(?string $requestId = null): static | ||
{ | ||
$this->setContent($this->getContent().TurboStream::refresh($requestId)); | ||
|
||
return $this; | ||
} | ||
} |
This file contains hidden or 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,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Turbo\Tests\Helper; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\UX\Turbo\Helper\TurboStream; | ||
|
||
class TurboStreamTest extends TestCase | ||
{ | ||
/** | ||
* @testWith ["append"] | ||
* ["prepend"] | ||
* ["replace"] | ||
* ["update"] | ||
* ["before"] | ||
* ["after"] | ||
*/ | ||
public function testStream(string $action): void | ||
{ | ||
$this->assertSame(<<<EOHTML | ||
<turbo-stream action="{$action}" targets="some["selector"]"> | ||
<template><div>content</div></template> | ||
</turbo-stream> | ||
EOHTML, | ||
TurboStream::$action('some["selector"]', '<div>content</div>') | ||
); | ||
} | ||
|
||
/** | ||
* @testWith ["replace"] | ||
* ["update"] | ||
*/ | ||
public function testStreamMorph(string $action): void | ||
{ | ||
$this->assertSame(<<<EOHTML | ||
<turbo-stream action="{$action}" targets="some["selector"]" method="morph"> | ||
<template><div>content</div></template> | ||
</turbo-stream> | ||
EOHTML, | ||
TurboStream::$action('some["selector"]', '<div>content</div>', morph: true) | ||
); | ||
} | ||
|
||
public function testRemove(): void | ||
{ | ||
$this->assertSame(<<<EOHTML | ||
<turbo-stream action="remove" targets="some["selector"]"></turbo-stream> | ||
EOHTML, | ||
TurboStream::remove('some["selector"]') | ||
); | ||
} | ||
|
||
public function testRefreshWithoutId(): void | ||
{ | ||
$this->assertSame(<<<EOHTML | ||
<turbo-stream action="refresh"></turbo-stream> | ||
EOHTML, | ||
TurboStream::refresh() | ||
); | ||
} | ||
|
||
public function testRefreshWithId(): void | ||
{ | ||
$this->assertSame(<<<EOHTML | ||
<turbo-stream action="refresh" request-id="a"b"></turbo-stream> | ||
EOHTML, | ||
TurboStream::refresh('a"b') | ||
); | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note the "s" to targets, and the docblock comments: we support only CSS selectors, not pure IDs (CSS selectors are more capable anyway).