— |
use Any::Moose 'X::Types' => [ -declare => [ qw/State Store/ ] ]; use Any::Moose 'X::Types::' .any_moose() => [ qw(CodeRef Object HashRef) ]; subtype State,
as CodeRef;
coerce State,
from Object,
via {
my $x = $_ ;
sub { $x }
};
coerce State,
from HashRef,
via {
my $klass = $_ ->{class};
$klass = $klass =~ s/^\+// ? $klass : "HTTP::Session::State::${klass}" ;
Any::Moose::load_class( $klass );
my $obj = $klass ->new( $_ ->{args} );
sub { $obj };
};
subtype Store,
as CodeRef;
coerce Store,
from Object,
via {
my $x = $_ ;
sub { $x }
};
coerce Store,
from HashRef,
via {
my $klass = $_ ->{class};
$klass = $klass =~ s/^\+// ? $klass : "HTTP::Session::Store::${klass}" ;
Any::Moose::load_class( $klass );
my $obj = $klass ->new( $_ ->{args} );
sub { $obj };
};
has 'state' => (
is => 'ro' ,
isa => State,
coerce => 1,
);
has 'store' => (
is => 'ro' ,
isa => Store,
coerce => 1,
);
{
my $SESSION ;
my $SELF ;
my $REQ ;
middleware_method 'session' => sub {
$SESSION ||= HTTP::Session->new(
state => $SELF ->state->(),
store => $SELF ->store->(),
request => $REQ ,
);
};
before_handle {
( undef , $SELF , $REQ ) = @_ ;
undef $SESSION ;
$REQ ;
};
after_handle {
my ( $c , $self , $req , $res ) = @_ ;
if ( $SESSION && $res ) {
$SESSION ->response_filter( $res );
$SESSION ->finalize();
}
undef $SESSION ;
undef $SELF ;
undef $REQ ;
$res ;
};
}
__MIDDLEWARE__
|