File Coverage

blib/lib/MojoX/Session/Simple.pm
Criterion Covered Total %
statement 6 34 17.6
branch 0 24 0.0
condition 0 30 0.0
subroutine 2 5 40.0
pod 2 2 100.0
total 10 95 10.5


line stmt bran cond sub pod time code
1             package MojoX::Session::Simple;
2 3     3   780969 use 5.010001;
  3         10  
  3         99  
3 3     3   461 use Mojo::Base 'Mojolicious::Sessions';
  3         7566  
  3         15  
4             our $VERSION = "0.05";
5              
6             sub load {
7 0     0 1   my ($self, $c) = @_;
8 0           my $session = $c->req->env->{'psgix.session'};
9 0           $c->stash->{'mojo.session'} = $session;
10              
11             ## "expiration" value is inherited
12 0   0       my $expiration = $session->{expiration} // $self->default_expiration;
13              
14 0     0     my $remove_session = sub { delete @$session{ keys %$session } };
  0            
15              
16 0 0 0       $remove_session->() and return
      0        
17             if !(my $expires = delete $session->{expires}) && $expiration;
18 0 0 0       $remove_session->() and return
      0        
19             if defined $expires && $expires <= time;
20              
21 0           my $stash = $c->stash;
22 0 0 0       $remove_session->() and return
23             unless $stash->{'mojo.active_session'} = keys %$session;
24 0 0         $session->{flash} = delete $session->{new_flash} if $session->{new_flash};
25             }
26              
27             sub store {
28 0     0 1   my ($self, $c) = @_;
29 0           my $env = $c->req->env;
30              
31             # Make sure session was active
32 0           my $stash = $c->stash;
33 0 0         return unless my $session = $stash->{'mojo.session'};
34 0 0 0       return unless keys %$session || $stash->{'mojo.active_session'};
35              
36             # Don't reset flash for static files
37 0           my $old = delete $session->{flash};
38 0 0         $session->{new_flash} = $old if $stash->{'mojo.static'};
39 0 0         delete $session->{new_flash} unless keys %{ $session->{new_flash} };
  0            
40              
41             # Generate "expires" value from "expiration" if necessary
42 0   0       my $expiration = $session->{expiration} // $self->default_expiration;
43 0           my $default = delete $session->{expires};
44 0 0 0       $session->{expires} = $default || time + $expiration
      0        
45             if $expiration || $default;
46              
47 0           my $regenerate = delete $session->{regenerate};
48 0 0         delete $session->{flash} if exists $session->{flash};
49              
50 0 0 0       if (defined($session->{expires}) && $session->{expires} <= time) {
    0          
51 0           $env->{'psgix.session.options'}{expire} = 1;
52             }
53             elsif ($regenerate) {
54 0           $env->{'psgix.session.options'}{change_id} = 1;
55             }
56             }
57              
58             1;
59             __END__