File Coverage

blib/lib/Dancer/Session.pm
Criterion Covered Total %
statement 46 46 100.0
branch 12 16 75.0
condition 5 5 100.0
subroutine 11 11 100.0
pod 0 6 0.0
total 74 84 88.1


line stmt bran cond sub pod time code
1             package Dancer::Session;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: session engine for the Dancer framework
4             $Dancer::Session::VERSION = '1.3514_04'; # TRIAL
5             $Dancer::Session::VERSION = '1.351404';
6 185     185   109952 use strict;
  185         376  
  185         4658  
7 185     185   868 use warnings;
  185         323  
  185         4104  
8              
9 185     185   828 use Carp;
  185         321  
  185         9708  
10 185     185   65692 use Dancer::Cookies;
  185         410  
  185         5088  
11 185     185   1072 use Dancer::Engine;
  185         352  
  185         61580  
12              
13             # Singleton representing the session engine class to use
14             my $ENGINE = undef;
15 485     485 0 1655 sub engine {$ENGINE}
16              
17             # This wrapper look for the session engine and try to load it.
18             sub init {
19 98     98 0 1324 my ($class, $name, $config) = @_;
20 98         710 $ENGINE = Dancer::Engine->build(session => $name, $config);
21              
22             #$ENGINE->init(); already done
23             }
24              
25             # retrieve or create a session for the client
26             sub get_current_session {
27 177     177 0 255 shift;
28 177         298 my %p = @_;
29 177         344 my $sid = engine->read_session_id;
30 177         297 my $session = undef;
31 177         298 my $class = ref(engine);
32              
33 177   100     536 my $sessions = Dancer::SharedData->sessions || {};
34 177         533 my $name = $class->session_name();
35 177 100 100     757 if ($sid and $session = $sessions->{$name}{$sid}) {
36 80         251 return $session;
37             }
38            
39 97 100       376 $session = $class->retrieve($sid) if $sid;
40              
41 97 100       225 if (not defined $session) {
42 34         140 $session = $class->create();
43             }
44              
45 97         331 $sessions->{$name}{$session->id} = $session;
46 97         379 Dancer::SharedData->sessions($sessions);
47              
48             # Generate a session cookie; we want to do this regardless of whether the
49             # session is new or existing, so that the cookie expiry is updated.
50             engine->write_session_id($session->id)
51 97 100       303 unless $p{no_update};
52              
53 97         320 return $session;
54             }
55              
56 132     132 0 340 sub get { get_current_session(@_) }
57              
58             sub read {
59 8     8 0 16 my ($class, $key) = @_;
60 8 50       17 return unless $key;
61 8         19 my $session = get_current_session();
62 8         24 return $session->get_value($key);
63             }
64              
65             sub write {
66 8     8 0 21 my ($class, $key, $value) = @_;
67              
68 8 50       16 return unless $key;
69 8 50       23 $key eq 'id' and croak 'Can\'t store to session key with name "id"';
70              
71 8         17 my $session = get_current_session();
72 8         47 $session->set_value($key, $value);
73              
74             # TODO : should be moved as an "after" filter
75 8 50       27 $session->flush unless $session->is_lazy;
76 8         22 return $value;
77             }
78              
79             1;
80              
81             __END__