File Coverage

blib/lib/Dancer/Session/YAML.pm
Criterion Covered Total %
statement 67 69 97.1
branch 16 24 66.6
condition 5 6 83.3
subroutine 17 17 100.0
pod 6 6 100.0
total 111 122 90.9


line stmt bran cond sub pod time code
1             package Dancer::Session::YAML;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: YAML-file-based session backend for Dancer
4             $Dancer::Session::YAML::VERSION = '1.3520';
5 3     3   3847 use strict;
  3         6  
  3         89  
6 3     3   17 use warnings;
  3         9  
  3         69  
7 3     3   14 use Carp;
  3         4  
  3         166  
8 3     3   36 use base 'Dancer::Session::Abstract';
  3         7  
  3         1532  
9              
10 3     3   21 use Dancer::Logger;
  3         9  
  3         55  
11 3     3   18 use Dancer::ModuleLoader;
  3         6  
  3         76  
12 3     3   15 use Dancer::Config 'setting';
  3         13  
  3         151  
13 3     3   24 use Dancer::FileUtils qw(path atomic_write);
  3         7  
  3         164  
14 3     3   18 use Dancer::Exception qw(:all);
  3         7  
  3         2165  
15              
16             # static
17              
18             my %session_dir_initialized;
19              
20             sub init {
21 3     3 1 5 my $self = shift;
22 3         16 $self->SUPER::init(@_);
23              
24 3 100       11 if (!keys %session_dir_initialized) {
25 2 50       23 raise core_session => "YAML is needed and is not installed"
26             unless Dancer::ModuleLoader->load('YAML');
27             }
28              
29             # default value for session_dir
30 3 50       14 setting('session_dir' => path(setting('appdir'), 'sessions'))
31             if not defined setting('session_dir');
32              
33 3         8 my $session_dir = setting('session_dir');
34 3 100       14 if (! exists $session_dir_initialized{$session_dir}) {
35 2         6 $session_dir_initialized{$session_dir} = 1;
36             # make sure session_dir exists
37 2 50       37 if (!-d $session_dir) {
38 2 50       124 mkdir $session_dir
39             or raise core_session => "session_dir $session_dir cannot be created";
40             }
41 2         17 Dancer::Logger::core("session_dir : $session_dir");
42             }
43             }
44              
45             # create a new session and return the newborn object
46             # representing that session
47             sub create {
48 3     3 1 1441 my ($class) = @_;
49              
50 3         22 my $self = Dancer::Session::YAML->new;
51 3         15 $self->flush;
52 2         8 return $self;
53             }
54              
55             # deletes the dir cache
56             sub reset {
57 1     1 1 1187 my ($class) = @_;
58 1         6 %session_dir_initialized = ();
59             }
60              
61             # Return the session object corresponding to the given id
62             sub retrieve {
63 6     6 1 25 my ($class, $id) = @_;
64              
65 6 50       46 unless( $id =~ /^[\da-z]+$/i ) {
66 0         0 warn "session id '$id' contains illegal characters\n";
67 0         0 return;
68             }
69              
70 6         58 my $session_file = yaml_file($id);
71              
72 6 100 100     135 return unless defined $session_file && -f $session_file;
73              
74 4 50       154 open my $fh, '+<', $session_file or die "Can't open '$session_file': $!\n";
75 4         53 my $content = YAML::LoadFile($fh);
76 4 50       15979 close $fh or die "Can't close '$session_file': $!\n";
77              
78 4   66     77 return bless $content => ref($class) || $class;
79             }
80              
81             # instance
82              
83             sub yaml_file {
84 19     19   1184 my $id = shift;
85              
86             # Untaint Session ID before using it in file actions
87             # required when running under Perl Taint mode
88 19         75 $id =~ m/^([\d]*)$/;
89 19 100       72 return unless $1;
90 18         46 my $yaml_file = "$1.yml";
91              
92 18         61 return path(setting('session_dir'), $yaml_file);
93             }
94              
95             sub destroy {
96 1     1 1 9 my ($self) = @_;
97 3     3   24 use Dancer::Logger;
  3         8  
  3         487  
98 1         4 Dancer::Logger::core(
99             "trying to remove session file: " . yaml_file($self->id));
100 1 50       4 unlink yaml_file($self->id) if -f yaml_file($self->id);
101             }
102              
103             sub flush {
104 4     4 1 850 my $self = shift;
105 4         12 my $session_file = yaml_file( $self->id );
106              
107 4         11 atomic_write( setting('session_dir'), yaml_file($self->id), YAML::Dump($self) );
108              
109 3         49 return $self;
110             }
111              
112             1;
113              
114             __END__