Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions src/Turbo/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\UX\Turbo\Broadcaster\BroadcasterInterface;
use Symfony\UX\Turbo\Broadcaster\IdAccessor;
use Symfony\UX\Turbo\Broadcaster\ImuxBroadcaster;
use Symfony\UX\Turbo\Broadcaster\TwigBroadcaster;
use Symfony\UX\Turbo\Doctrine\BroadcastListener;
use Symfony\UX\Turbo\Request\RequestListener;
use Symfony\UX\Turbo\Twig\TwigExtension;

/*
Expand Down Expand Up @@ -55,5 +57,8 @@
])
->tag('doctrine.event_listener', ['event' => 'onFlush'])
->tag('doctrine.event_listener', ['event' => 'postFlush'])

->set('turbo.kernel.request_listener', RequestListener::class)
->tag('kernel.event_listener', ['event' => KernelEvents::REQUEST, 'priority' => 256])
;
};
28 changes: 28 additions & 0 deletions src/Turbo/src/Request/RequestListener.php
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);
}
}
6 changes: 0 additions & 6 deletions src/Turbo/src/TurboBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
Expand All @@ -25,11 +24,6 @@ final class TurboBundle extends Bundle
public const STREAM_FORMAT = 'turbo_stream';
public const STREAM_MEDIA_TYPE = 'text/vnd.turbo-stream.html';

public function boot(): void
{
(new Request())->setFormat(self::STREAM_FORMAT, self::STREAM_MEDIA_TYPE);
}

public function build(ContainerBuilder $container): void
{
parent::build($container);
Expand Down
54 changes: 54 additions & 0 deletions src/Turbo/tests/Request/RequestListenerTest.php
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 ?: '');
}
}
9 changes: 9 additions & 0 deletions src/Turbo/tests/app/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand Down Expand Up @@ -144,6 +145,7 @@ protected function configureRoutes(RoutingConfigurator $routes): void
$routes->add('artists', '/artists')->controller('kernel::artists');
$routes->add('artist', '/artists/{id}')->controller('kernel::artist');
$routes->add('artist_from_song', '/artistFromSong')->controller('kernel::artistFromSong');
$routes->add('turbo_request', '/turboRequest')->controller('kernel::turboRequest');
}

public function getProjectDir(): string
Expand Down Expand Up @@ -312,4 +314,11 @@ public function artistFromSong(Request $request, EntityManagerInterface $doctrin
'song' => $song,
]));
}

public function turboRequest(Request $request): Response
{
return new JsonResponse([
'preferred_format' => $request->getPreferredFormat(),
]);
}
}