| 1 | package Dancer::Plugin::WindowSession; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Clone; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | our $VERSION = '0.02'; | |||||||||
| 9 | |||||||||||
| 10 | 2 | ▶ | use Dancer ':syntax'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use Dancer::Plugin; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | sub _get_window_session_object | ||||||||||
| 14 | 0 | ▶ | { | ||||||||
|
Subroutine
_get_window_session_object: not called CC=6 SCAR=37.4
| |||||||||||
| 15 | 0 | my $win_sess_id ; ## The window session ID, from the HTTP request. | |||||||||
| 16 | my $win_sess_obj ; ## The Dancer::Session::Abstract object | ||||||||||
| 17 | |||||||||||
| 18 | ## Is there a cached version of the window-session object ? | ||||||||||
| 19 | 0 | ▶ | if (defined(var 'window_session_object')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 0 | $win_sess_obj = var 'window_session_object'; | |||||||||
| 21 | 0 | $win_sess_id = $win_sess_obj->id; | |||||||||
| 22 | 0 | return $win_sess_obj; | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | # This is the first time 'window_session' is request during this request handling, | ||||||||||
| 27 | # check if we have one in the HTTP Request | ||||||||||
| 28 | 0 | $win_sess_id = param 'winsid'; | |||||||||
| 29 | |||||||||||
| 30 | 0 | ▶ | my $session_engine = engine 'session' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | or die "Can't find session engine"; | ||||||||||
| 32 | |||||||||||
| 33 | # If we have a Window-session-Id, try to retrieve the corresponding hash. | ||||||||||
| 34 | 0 | ▶ | if ($win_sess_id) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 0 | $win_sess_obj = $session_engine->retrieve($win_sess_id); | |||||||||
| 36 | |||||||||||
| 37 | ## If we failed to retrieve existing window-session, force a new one | ||||||||||
| 38 | 0 | ▶ | $win_sess_id = undef unless $win_sess_obj; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | # If anything failed along the way, just create a new window session | ||||||||||
| 42 | 0 | ▶ | if (!$win_sess_id) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | $win_sess_obj = $session_engine->create(); | |||||||||
| 44 | 0 | $win_sess_id = $win_sess_obj->id; | |||||||||
| 45 | |||||||||||
| 46 | } | ||||||||||
| 47 | ## Cache the new window-session object, | ||||||||||
| 48 | ## for other route handlers downstream in the current request handling. | ||||||||||
| 49 | 0 | var 'window_session_object' => $win_sess_obj; | |||||||||
| 50 | |||||||||||
| 51 | 0 | return $win_sess_obj; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 0 | ▶ | hook before => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 55 | 0 | my $window_session = _get_window_session_object; | |||||||||
| 56 | 0 | my $window_session_id = $window_session->id; | |||||||||
| 57 | 2 | }; | |||||||||
| 58 | |||||||||||
| 59 | 0 | ▶ | hook before_template_render => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 60 | 0 | my $tokens = shift ; | |||||||||
| 61 | |||||||||||
| 62 | 0 | my $window_session = _get_window_session_object; | |||||||||
| 63 | |||||||||||
| 64 | 0 | $tokens->{winsid} = $window_session->id; | |||||||||
| 65 | 0 | $tokens->{window_session} = Clone::clone($window_session); | |||||||||
| 66 | 2 | }; | |||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | hook after => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 69 | 0 | my $window_session = _get_window_session_object; | |||||||||
| 70 | 0 | my $window_session_id = $window_session->id; | |||||||||
| 71 | 0 | $window_session->flush(); | |||||||||
| 72 | 2 | }; | |||||||||
| 73 | |||||||||||
| 74 | 0 | ▶ | register window_session_id => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 75 | 0 | my $window_session = _get_window_session_object; | |||||||||
| 76 | 0 | return $window_session->id; | |||||||||
| 77 | 2 | }; | |||||||||
| 78 | |||||||||||
| 79 | |||||||||||
| 80 | 0 | ▶ | register window_session => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 81 | 0 | my $window_session = _get_window_session_object; | |||||||||
| 82 | 0 | my $window_session_id = $window_session->id; | |||||||||
| 83 | |||||||||||
| 84 | 0 | my ($key,$value) = @_; | |||||||||
| 85 | 0 | ▶ | $key eq 'id' and croak 'Can\'t store to window_session key with name "id"'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | $window_session->{$key} = $value if (@_==2); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | return $window_session->{$key}; | |||||||||
| 89 | 2 | }; | |||||||||
| 90 | |||||||||||
| 91 | 2 | register_plugin; | |||||||||
| 92 | |||||||||||
| 93 | # ABSTRACT: A Dancer plugin for managing Browser Window Sessions | ||||||||||
| 94 | |||||||||||
| 95 | 2 | 1; | |||||||||
| 96 | __END__ | ||||||||||