| 1 | package Dancer::Session; | ||||||||||||||||||||||||||||
| 2 | 201 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||||||||||
| 3 | #ABSTRACT: session engine for the Dancer framework | ||||||||||||||||||||||||||||
| 4 | 201 | $Dancer::Session::VERSION = '1.3520'; | |||||||||||||||||||||||||||
| 5 | 201 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 201 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 201 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 201 | ▶ | use Dancer::Cookies; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 201 | ▶ | use Dancer::Engine; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # Singleton representing the session engine class to use | ||||||||||||||||||||||||||||
| 13 | 201 | my $ENGINE = undef; | |||||||||||||||||||||||||||
| 14 | 485 | ▶ | sub engine {$ENGINE} | ||||||||||||||||||||||||||
|
Subroutine
engine: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # This wrapper look for the session engine and try to load it. | ||||||||||||||||||||||||||||
| 17 | sub init { | ||||||||||||||||||||||||||||
| 18 | 97 | ▶ | my ($class, $name, $config) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 19 | 97 | $ENGINE = Dancer::Engine->build(session => $name, $config); | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | #$ENGINE->init(); already done | ||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # retrieve or create a session for the client | ||||||||||||||||||||||||||||
| 25 | sub get_current_session { | ||||||||||||||||||||||||||||
| 26 | 177 | ▶ | shift; | ||||||||||||||||||||||||||
|
Subroutine
get_current_session: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 177 | my %p = @_; | |||||||||||||||||||||||||||
| 28 | 177 | my $sid = engine->read_session_id; | |||||||||||||||||||||||||||
| 29 | 177 | my $session = undef; | |||||||||||||||||||||||||||
| 30 | 177 | my $class = ref(engine); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 177 | ▶ | my $sessions = Dancer::SharedData->sessions || {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
'Dancer::SharedData'->sessions || {}
MC/DC100%1/1
'Dancer::SharedData'->sessions || {}
'Dancer::SharedData'->sessions
Truth table100%2/2
A
A= 'Dancer::SharedData'->sessions
| |||||||||||||||||||||||||||||
| 33 | 177 | my $name = $class->session_name(); | |||||||||||||||||||||||||||
| 34 | 177 | ▶ | if ($sid and $session = $sessions->{$name}{$sid}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$sid and $session = $sessions->{$name}{$sid}
MC/DC100%2/2
$sid and $session = $sessions->{$name}{$sid}
$sid
$session = $sessions->{$name}{$sid}
Truth table100%3/3
A and B
A= $sidB= $session = $sessions->{$name}{$sid}
| |||||||||||||||||||||||||||||
| 35 | 80 | return $session; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 97 | ▶ | $session = $class->retrieve($sid) if $sid; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 97 | ▶ | if (not defined $session) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 34 | $session = $class->create(); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 97 | $sessions->{$name}{$session->id} = $session; | |||||||||||||||||||||||||||
| 45 | 97 | Dancer::SharedData->sessions($sessions); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Generate a session cookie; we want to do this regardless of whether the | ||||||||||||||||||||||||||||
| 48 | # session is new or existing, so that the cookie expiry is updated. | ||||||||||||||||||||||||||||
| 49 | engine->write_session_id($session->id) | ||||||||||||||||||||||||||||
| 50 | 97 | ▶ | unless $p{no_update}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 97 | return $session; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 132 | ▶ | sub get { get_current_session(@_) } | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub read { | ||||||||||||||||||||||||||||
| 58 | 8 | ▶ | my ($class, $key) = @_; | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 8 | ▶ | return unless $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 8 | my $session = get_current_session(); | |||||||||||||||||||||||||||
| 61 | 8 | return $session->get_value($key); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | sub write { | ||||||||||||||||||||||||||||
| 65 | 8 | ▶ | my ($class, $key, $value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=4 SCAR=14.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 8 | ▶ | return unless $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 8 | ▶ | $key eq 'id' and croak 'Can\'t store to session key with name "id"'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 8 | my $session = get_current_session(); | |||||||||||||||||||||||||||
| 71 | 8 | $session->set_value($key, $value); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # TODO : should be moved as an "after" filter | ||||||||||||||||||||||||||||
| 74 | 8 | ▶ | $session->flush unless $session->is_lazy; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 8 | return $value; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 201 | 1; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | __END__ | ||||||||||||||||||||||||||||