| 1 | package Dancer2::Core::Role::SessionFactory::File; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Role for file-based session factories | ||||||||||||||||||||||||||||
| 3 | 6 | $Dancer2::Core::Role::SessionFactory::File::VERSION = '2.3.0'; | |||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | with 'Dancer2::Core::Role::SessionFactory'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Dancer2::Core::Types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use Dancer2::FileUtils qw(escape_filename); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Fcntl ':flock'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use File::Copy (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use Path::Tiny (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 15 | # Required by classes consuming this role | ||||||||||||||||||||||||||||
| 16 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 6 | requires '_suffix'; # '.yml', '.json', etc. | |||||||||||||||||||||||||||
| 19 | 6 | requires '_thaw_from_handle'; # given handle, return session 'data' field | |||||||||||||||||||||||||||
| 20 | 6 | requires '_freeze_to_handle'; # given handle and data, serialize it | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 24 | # Attributes and methods | ||||||||||||||||||||||||||||
| 25 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | has session_dir => ( | ||||||||||||||||||||||||||||
| 28 | is => 'ro', | ||||||||||||||||||||||||||||
| 29 | isa => Str, | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | default => sub { Path::Tiny::path( '.', 'sessions' )->stringify }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 31 | 6 | ); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 6 | has _session_dir_path => ( | |||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||
| 35 | lazy => 1, | ||||||||||||||||||||||||||||
| 36 | builder => '_build_session_dir_path', | ||||||||||||||||||||||||||||
| 37 | init_arg => undef, | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 4 | ▶ | sub _build_session_dir_path { | ||||||||||||||||||||||||||
|
Subroutine
_build_session_dir_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 42 | 4 | return Path::Tiny::path( $self->session_dir ); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 4 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 4 | ▶ | if ( !$self->_session_dir_path->is_dir ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | mkdir $self->session_dir | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | or croak "Unable to create session dir : " | ||||||||||||||||||||||||||||
| 51 | . $self->session_dir . ' : ' | ||||||||||||||||||||||||||||
| 52 | . $!; | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub _sessions { | ||||||||||||||||||||||||||
|
Subroutine
_sessions: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 57 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 58 | 0 | my $sessions = []; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | opendir( my $dh, $self->session_dir ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | or croak "Unable to open directory " . $self->session_dir . " : $!"; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | my $suffix = $self->_suffix; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | while ( my $file = readdir($dh) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | next if $file eq '.' || $file eq '..'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$file eq "." or $file eq ".."
MC/DC0%0/2
$file eq "." or $file eq ".."
$file eq "."
$file eq ".."
Truth table0%0/3
A or B
A= $file eq "."B= $file eq ".."
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( $file =~ /(\w+)\Q$suffix\E/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | push @{$sessions}, $1; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 0 | closedir($dh); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | return $sessions; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 17 | ▶ | sub _retrieve { | ||||||||||||||||||||||||||
|
Subroutine
_retrieve: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 77 | 17 | my ( $self, $id ) = @_; | |||||||||||||||||||||||||||
| 78 | 17 | my $session_file = $self->_session_dir_path->child( | |||||||||||||||||||||||||||
| 79 | escape_filename($id) . $self->_suffix, | ||||||||||||||||||||||||||||
| 80 | )->stringify; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 17 | ▶ | croak "Invalid session ID: $id" unless -f $session_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 16 | ▶ | open my $fh, '+<', $session_file or die "Can't open '$session_file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 16 | ▶ | flock $fh, LOCK_SH or die "Can't lock file '$session_file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 16 | my $data = $self->_thaw_from_handle($fh); | |||||||||||||||||||||||||||
| 87 | 16 | ▶ | close $fh or die "Can't close '$session_file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 16 | return $data; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | ▶ | sub _change_id { | ||||||||||||||||||||||||||
|
Subroutine
_change_id: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 93 | 1 | my ($self, $old_id, $new_id) = @_; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | my $old_path = $self->_session_dir_path->child( | |||||||||||||||||||||||||||
| 96 | escape_filename($old_id) . $self->_suffix | ||||||||||||||||||||||||||||
| 97 | )->stringify; | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | ▶ | return if !-f $old_path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | my $new_path = $self->_session_dir_path->child( | |||||||||||||||||||||||||||
| 102 | escape_filename($new_id) . $self->_suffix | ||||||||||||||||||||||||||||
| 103 | )->stringify; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1 | File::Copy::move($old_path, $new_path); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 5 | ▶ | sub _destroy { | ||||||||||||||||||||||||||
|
Subroutine
_destroy: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 109 | 5 | my ( $self, $id ) = @_; | |||||||||||||||||||||||||||
| 110 | 5 | my $session_file = $self->_session_dir_path->child( | |||||||||||||||||||||||||||
| 111 | escape_filename($id) . $self->_suffix | ||||||||||||||||||||||||||||
| 112 | )->stringify; | ||||||||||||||||||||||||||||
| 113 | 5 | ▶ | return if !-f $session_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 5 | unlink $session_file; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 21 | ▶ | sub _flush { | ||||||||||||||||||||||||||
|
Subroutine
_flush: called CC=6 SCAR=18.8
| |||||||||||||||||||||||||||||
| 119 | 21 | my ( $self, $id, $data ) = @_; | |||||||||||||||||||||||||||
| 120 | 21 | my $session_file = $self->_session_dir_path->child( | |||||||||||||||||||||||||||
| 121 | escape_filename($id) . $self->_suffix | ||||||||||||||||||||||||||||
| 122 | )->stringify; | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 21 | ▶ | open my $fh, '>', $session_file or die "Can't open '$session_file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 21 | ▶ | flock $fh, LOCK_EX or die "Can't lock file '$session_file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 21 | ▶ | seek $fh, 0, 0 or die "Can't seek in file '$session_file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 21 | ▶ | truncate $fh, 0 or die "Can't truncate file '$session_file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 21 | binmode $fh, ':encoding(UTF-8)'; | |||||||||||||||||||||||||||
| 129 | 21 | $self->_freeze_to_handle( $fh, $data ); | |||||||||||||||||||||||||||
| 130 | 21 | ▶ | close $fh or die "Can't close '$session_file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 21 | return $data; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 6 | 1; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||