| 1 | package Uniform::HTMX::PSGI; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | ▶ | use Carp qw(croak); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 2 | ▶ | use parent 'Uniform::HTMX'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 2 | our $VERSION = '1.00'; | |||||||||||||
| 9 | |||||||||||||||
| 10 | # Constructor: Expects a standard PSGI %env Hash reference | ||||||||||||||
| 11 | sub new { | ||||||||||||||
| 12 | 2 | ▶ | my ($class, $env) = @_; | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 2 | ▶ | croak "PSGI environment context must be a HASH reference" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 15 | unless ref($env) eq 'HASH'; | ||||||||||||||
| 16 | |||||||||||||||
| 17 | 1 | my $self = bless { | |||||||||||||
| 18 | in => {}, | ||||||||||||||
| 19 | out => {}, | ||||||||||||||
| 20 | _ctx => $env, | ||||||||||||||
| 21 | }, $class; | ||||||||||||||
| 22 | |||||||||||||||
| 23 | # Pass the raw %env directly to your core version 1.01 normalization engine. | ||||||||||||||
| 24 | # Your core automatically strips "HTTP_" prefixes and maps underscores to dashes! | ||||||||||||||
| 25 | 1 | $self->{in} = $self->_normalize_headers($env); | |||||||||||||
| 26 | |||||||||||||||
| 27 | 1 | return $self; | |||||||||||||
| 28 | } | ||||||||||||||
| 29 | |||||||||||||||
| 30 | # Flushes outbound HTMX instructions securely back into the PSGI lifecycle environment | ||||||||||||||
| 31 | sub apply { | ||||||||||||||
| 32 | 2 | ▶ | my ($self, $response_object) = @_; | ||||||||||||
|
Subroutine
apply: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 33 | 2 | ▶ | return $self unless ref($self->{out}) eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | |||||||||||||||
| 35 | 2 | my %outbound = %{ $self->{out} }; | |||||||||||||
| 36 | |||||||||||||||
| 37 | # Strategy A: If a Plack::Response object is explicitly passed, map headers directly | ||||||||||||||
| 38 | 2 | ▶ | if (eval { $response_object && $response_object->can('headers') }) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 39 | 1 | ▶ | while (my ($k, $v) = each %outbound) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | 2 | $response_object->headers->header($k => $v); | |||||||||||||
| 41 | } | ||||||||||||||
| 42 | } | ||||||||||||||
| 43 | # Strategy B: Fall back to stashing them inside the standard $env hash slot | ||||||||||||||
| 44 | else { | ||||||||||||||
| 45 | 1 | $self->{_ctx}->{'htmx.outbound'} = \%outbound; | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 2 | return $self; | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 2 | 1; | |||||||||||||
| 52 | |||||||||||||||
| 53 | __END__ | ||||||||||||||