chore: cleanup

This commit is contained in:
Björn Fromme
2026-03-16 11:59:12 +01:00
parent 61447ced4f
commit 502b121e43
10 changed files with 5 additions and 333 deletions
+3 -102
View File
@@ -1,115 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Htmx;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Trait providing HTMX-specific functionality for controllers.
*
* This trait provides methods to handle HTMX requests and responses, including
* conditional rendering based on HTMX headers, HTMX-specific redirects, and
* multi-block rendering capabilities. It simplifies the integration of HTMX
* functionality into Symfony controllers by providing common patterns for
* detecting HTMX requests and generating appropriate responses.
* Trait providing HTMX Out-of-Band swap functionality for controllers.
*/
trait HxTrait
{
/**
* Determines if the current request is an HTMX request.
*
* Checks the HX-Request header to identify if the request was made via HTMX.
* This allows controllers to provide different responses for HTMX vs regular
* HTTP requests, enabling progressive enhancement patterns.
*
* @param Request $request The current HTTP request
*
* @return bool True if the request is an HTMX request, false otherwise
*/
public function isHxRequest(Request $request): bool
{
return 'true' === $request->headers->get('HX-Request');
}
/**
* Renders a template with HTMX-aware block selection.
*
* Renders either a specific template block or the full template based on
* whether the request is an HTMX request. When an HTMX request is detected
* and a block is specified, only that block is rendered. Otherwise, the
* full template is rendered. This enables efficient partial page updates
* for HTMX requests while maintaining full page rendering for regular requests.
*
* @param Request $request The current HTTP request
* @param string $template The template name to render
* @param array $parameters Template parameters to pass to the view
* @param string|null $block The specific block to render for HTMX requests
*
* @return Response The rendered response
*/
public function hxRender(
Request $request,
string $template,
array $parameters = [],
?string $block = null,
): Response {
if (null !== $block && true === $this->isHxRequest($request)) {
return $this->renderBlock($template, $block, $parameters);
}
return $this->render($template, $parameters);
}
/**
* Performs an HTMX-aware redirect.
*
* Returns an HTMX-specific redirect response when the request is an HTMX
* request, or a standard redirect response for regular HTTP requests.
* HTMX redirects are handled differently by the client, allowing for
* smoother user experiences in single-page application contexts.
*
* @param Request $request The current HTTP request
* @param string $url The URL to redirect to
*
* @return Response Either an HxRedirectResponse or RedirectResponse
*/
public function hxRedirect(Request $request, string $url): Response
{
if (true === $this->isHxRequest($request)) {
return new HxRedirectResponse($url);
}
return new RedirectResponse($url);
}
/**
* Renders multiple template blocks in a single response.
*
* Combines multiple template blocks into a single response content. This is
* useful for HTMX requests that need to update multiple parts of the page
* simultaneously. Each block in the array should contain 'template', 'block',
* and 'parameters' keys to define what to render.
*
* @param array $blocks Array of block definitions, each containing:
* - 'template': The template name
* - 'block': The block name to render
* - 'parameters': Template parameters
*
* @return Response Response containing all rendered blocks concatenated
*/
public function hxRenderBlocks(array $blocks): Response
{
$content = '';
foreach ($blocks as $block) {
$content .= $this->renderBlockView($block['template'], $block['block'], $block['parameters']);
}
return new Response($content);
}
/**
* Renders multiple Twig blocks for an HTMX Out-of-Band swap response.
*