-
-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor #2550 [Turbo] Add stream format with request listener (aleho)
This PR was merged into the 2.x branch. Discussion ---------- [Turbo] Add stream format with request listener | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | Fix #2549 | License | MIT BundleInterface::boot() is only called when booting a worker and not on every request. So on FrankenPHP for example Turbo requests would never return a stream when checking with `if ($request->getPreferredFormat() === TurboBundle::STREAM_FORMAT`. This replaces `TurboBundle::boot()` with a request listener to register the format on every request. Commits ------- 91b8268 [Turbo] Add stream format with request listener
- Loading branch information
Showing
5 changed files
with
96 additions
and
6 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
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,28 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Turbo\Request; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\UX\Turbo\TurboBundle; | ||
|
||
/** | ||
* Registers the Turbo request format for all requests. | ||
* | ||
* @author Alexander Hofbauer <[email protected]> | ||
*/ | ||
final class RequestListener | ||
{ | ||
public function __invoke(): void | ||
{ | ||
(new Request())->setFormat(TurboBundle::STREAM_FORMAT, TurboBundle::STREAM_MEDIA_TYPE); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Turbo\Tests\Request; | ||
|
||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Symfony\UX\Turbo\TurboBundle; | ||
|
||
/** | ||
* Tests the Turbo request listener. | ||
* | ||
* @author Alexander Hofbauer <[email protected]> | ||
*/ | ||
class RequestListenerTest extends WebTestCase | ||
{ | ||
public function testAddsTurboRequestFormat(): void | ||
{ | ||
$client = static::createClient(server: [ | ||
'HTTP_ACCEPT' => 'text/vnd.turbo-stream.html, text/html, application/xhtml+xml', | ||
]); | ||
|
||
// simulate worker mode | ||
$client->disableReboot(); | ||
|
||
// request twice to test if listener is always called | ||
$this->assertPreferredFormat(); | ||
$this->assertPreferredFormat(); | ||
} | ||
|
||
private function assertPreferredFormat(): void | ||
{ | ||
/** @var KernelBrowser $client */ | ||
$client = static::getClient(); | ||
|
||
$client->request('POST', '/turboRequest'); | ||
|
||
$response = $client->getResponse()->getContent(); | ||
|
||
$expectedJson = json_encode([ | ||
'preferred_format' => TurboBundle::STREAM_FORMAT, | ||
]); | ||
|
||
$this->assertJsonStringEqualsJsonString($expectedJson ?: '', $response ?: ''); | ||
} | ||
} |
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