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