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