File Coverage

blib/lib/Dancer2/Session/PSGI.pm
Criterion Covered Total %
statement 14 20 70.0
branch 4 4 100.0
condition n/a
subroutine 5 8 62.5
pod 0 2 0.0
total 23 34 67.6


line stmt bran cond sub pod time code
1             package Dancer2::Session::PSGI;
2              
3             # ABSTRACT: Dancer2 session storage via Plack::Middleware::Session
4              
5 2     2   1272 use Moo;
  2         2  
  2         11  
6             with 'Dancer2::Core::Role::SessionFactory';
7              
8             our $VERSION = '0.009'; # VERSION
9              
10             #-----------------------------------------#
11             # Alter SessionFactory attribute defaults
12             #-----------------------------------------#
13              
14             has '+cookie_name' => ( default => 'plack_session' );
15              
16             #-----------------------------------------#
17             # SessionFactory implementation methods
18             #-----------------------------------------#
19              
20             # Get middleware session hash
21             sub _retrieve {
22 6     6   41603 shift->request->env->{'psgix.session'};
23             }
24              
25             # Put data back/into middleware session hash
26             sub _flush {
27 8     8   198854 my ( $self, $id, $data ) = @_;
28 8         36 $self->request->env->{'psgix.session'} = $data;
29             }
30              
31             # Do nothing - we override change_id below but this method must exist to
32             # force Dance2::Core::App::change_session_id to call our change_id method
33       0     sub _change_id {}
34              
35             # Middleware handles cookie expiry
36 1     1   1492 sub _destroy { return }
37              
38             # Its the responsibility of Plack::Middleware::Session for
39             # tracking other sessions (we know nothing about them).
40             # So return an empty list.
41 0     0   0 sub _sessions { return [] }
42              
43             #-----------------------------------------#
44             # Overridden methods from SessionFactory
45             #-----------------------------------------#
46              
47             sub change_id {
48 0     0 0 0 my ( $self, %params ) = @_;
49 0         0 my $session = $params{session};
50              
51 0         0 $self->execute_hook( 'engine.session.before_change_id', $session->id );
52 0         0 $self->request->env->{'psgix.session.options'}->{'change_id'} = 1;
53             # we don't know what the new session ID will be yet so make it undef
54 0         0 $self->execute_hook( 'engine.session.before_change_id', undef );
55             }
56              
57             # Middleware sets the cookie.
58             sub set_cookie_header {
59 10     10 0 11561 my ( $self, %params ) = @_;
60              
61 10         18 my $session = $params{session};
62 10         32 my $options = $self->request->env->{'psgix.session.options'};
63              
64 10 100       138 if ( my $expires = $session->expires ) {
65 2 100       35 if ( $session->expires < time ) { # Cookie has expired.
66 1         12 $options->{expire} = 1;
67             }
68             else { # Pass upstream the cookie expire time
69 1         10 $options->{expires} = $expires;
70             }
71             }
72             }
73              
74             1;
75              
76             __END__