Skip to content

Instantly share code, notes, and snippets.

@alkemann
Created April 7, 2022 16:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alkemann/33e794db99319019f6a01deb38cb0d35 to your computer and use it in GitHub Desktop.
Save alkemann/33e794db99319019f6a01deb38cb0d35 to your computer and use it in GitHub Desktop.
<?php
namespace alkemann\htmx;
class HtmxDetails
{
protected $is_htmx_request;
protected $is_boosted;
protected $current_url;
protected $prompt;
protected $history_restore_request;
protected $target;
protected $trigger;
protected $trigger_name;
protected $triggering_event;
/**
* @param Closure $header callable that takes in a name (of http header) and a default value
*/
public function __construct(\Closure $header)
{
$this->is_htmx_request = $header('HX-Request', false);
$this->is_boosted = $header('HX-Boosted', false);
$this->current_url = $header('HX-Current-URL', null);
$this->prompt = $header('HX-Prompt', null);
$this->history_restore_request = $header('HX-History-Restore-Request', false);
$this->target = $header('HX-Target', null);
$this->trigger = $header('HX-Trigger', null);
$this->trigger_name = $header('HX-Trigger-Name', null);
$this->triggering_event = json_decode($header('Triggering-Event', '[]'), true);
}
public function is(): bool
{
return $this->is_htmx_request;
}
/**
* True if the request came from an element with the hx-boost attribute.
*/
public function boosted(): bool
{
return $this->is_boosted;
}
// The current URL of the browser, or null for non-htmx requests.
public function current_url(): string | null
{
return $this->current_url;
}
// The user response to hx-prompt if it was used, or null.
public function prompt(): string | null
{
return $this->prompt;
}
// True if the request is for history restoration after a miss in the local history cache.
public function history_restore_request(): bool
{
return $this->history_restore_request;
}
//The id of the target element if it exists, or null.
public function target(): string | null
{
return $this->target;
}
// The id of the triggered element if it exists, or null.
// Based on the HX-Trigger header.
public function trigger(): string | null
{
return $this->trigger;
}
// The name of the triggered element if it exists, or null.
// Based on the HX-Trigger-Name header.
public function trigger_name(): string | null
{
return $this->trigger_name;
}
// The deserialized JSON representation of the event that
// triggered the request if it exists, or null. This header
// is set by the event-header htmx extension, and contains
// details of the DOM event that triggered the request.
public function triggering_event(): array
{
return $this->triggering_event;
}
}
<?php
$app = new \Slim\App();
/**
*
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response
* @param callable $next Next middleware
*
* @return \Psr\Http\Message\ResponseInterface
*/
$app->add(function ($request, $response, $next) {
$details = new alkemann\htmx\HtmxDetails(
fn(string $name, $default = null) =>
$request->hasHeader($name) ? $request->getHeader($name) : $default
);
$request = $request->withAttribute('htmx', $details);
return $next($request, $response);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment