| 1 |
package Dancer2::Core::Role::SessionFactory::File; |
| 2 |
# ABSTRACT: Role for file-based session factories |
| 3 |
$Dancer2::Core::Role::SessionFactory::File::VERSION = '2.2.2'; |
| 4 |
use Moo::Role; |
| 5 |
with 'Dancer2::Core::Role::SessionFactory'; |
| 6 |
|
| 7 |
use Carp 'croak'; |
| 8 |
use Dancer2::Core::Types; |
| 9 |
use Dancer2::FileUtils qw(escape_filename); |
| 10 |
use Fcntl ':flock'; |
| 11 |
use File::Copy (); |
| 12 |
use Path::Tiny (); |
| 13 |
|
| 14 |
#--------------------------------------------------------------------------# |
| 15 |
# Required by classes consuming this role |
| 16 |
#--------------------------------------------------------------------------# |
| 17 |
|
| 18 |
requires '_suffix'; # '.yml', '.json', etc. |
| 19 |
requires '_thaw_from_handle'; # given handle, return session 'data' field |
| 20 |
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 |
default => sub { Path::Tiny::path( '.', 'sessions' )->stringify }, |
| 31 |
); |
| 32 |
|
| 33 |
has _session_dir_path => ( |
| 34 |
is => 'ro', |
| 35 |
lazy => 1, |
| 36 |
builder => '_build_session_dir_path', |
| 37 |
init_arg => undef, |
| 38 |
); |
| 39 |
|
| 40 |
sub _build_session_dir_path { |
| 41 |
my $self = shift; |
| 42 |
return Path::Tiny::path( $self->session_dir ); |
| 43 |
} |
| 44 |
|
| 45 |
sub BUILD { |
| 46 |
my $self = shift; |
| 47 |
|
| 48 |
if ( !$self->_session_dir_path->is_dir ) { |
| 49 |
mkdir $self->session_dir |
| 50 |
or croak "Unable to create session dir : " |
| 51 |
. $self->session_dir . ' : ' |
| 52 |
. $!; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
sub _sessions { |
| 57 |
my ($self) = @_; |
| 58 |
my $sessions = []; |
| 59 |
|
| 60 |
opendir( my $dh, $self->session_dir ) |
| 61 |
or croak "Unable to open directory " . $self->session_dir . " : $!"; |
| 62 |
|
| 63 |
my $suffix = $self->_suffix; |
| 64 |
|
| 65 |
while ( my $file = readdir($dh) ) { |
| 66 |
next if $file eq '.' || $file eq '..'; |
| 67 |
if ( $file =~ /(\w+)\Q$suffix\E/ ) { |
| 68 |
push @{$sessions}, $1; |
| 69 |
} |
| 70 |
} |
| 71 |
closedir($dh); |
| 72 |
|
| 73 |
return $sessions; |
| 74 |
} |
| 75 |
|
| 76 |
sub _retrieve { |
| 77 |
my ( $self, $id ) = @_; |
| 78 |
my $session_file = $self->_session_dir_path->child( |
| 79 |
escape_filename($id) . $self->_suffix, |
| 80 |
)->stringify; |
| 81 |
|
| 82 |
croak "Invalid session ID: $id" unless -f $session_file; |
| 83 |
|
| 84 |
open my $fh, '+<', $session_file or die "Can't open '$session_file': $!\n"; |
| 85 |
flock $fh, LOCK_SH or die "Can't lock file '$session_file': $!\n"; |
| 86 |
my $data = $self->_thaw_from_handle($fh); |
| 87 |
close $fh or die "Can't close '$session_file': $!\n"; |
| 88 |
|
| 89 |
return $data; |
| 90 |
} |
| 91 |
|
| 92 |
sub _change_id { |
| 93 |
my ($self, $old_id, $new_id) = @_; |
| 94 |
|
| 95 |
my $old_path = $self->_session_dir_path->child( |
| 96 |
escape_filename($old_id) . $self->_suffix |
| 97 |
)->stringify; |
| 98 |
|
| 99 |
return if !-f $old_path; |
| 100 |
|
| 101 |
my $new_path = $self->_session_dir_path->child( |
| 102 |
escape_filename($new_id) . $self->_suffix |
| 103 |
)->stringify; |
| 104 |
|
| 105 |
File::Copy::move($old_path, $new_path); |
| 106 |
} |
| 107 |
|
| 108 |
sub _destroy { |
| 109 |
my ( $self, $id ) = @_; |
| 110 |
my $session_file = $self->_session_dir_path->child( |
| 111 |
escape_filename($id) . $self->_suffix |
| 112 |
)->stringify; |
| 113 |
return if !-f $session_file; |
| 114 |
|
| 115 |
unlink $session_file; |
| 116 |
} |
| 117 |
|
| 118 |
sub _flush { |
| 119 |
my ( $self, $id, $data ) = @_; |
| 120 |
my $session_file = $self->_session_dir_path->child( |
| 121 |
escape_filename($id) . $self->_suffix |
| 122 |
)->stringify; |
| 123 |
|
| 124 |
open my $fh, '>', $session_file or die "Can't open '$session_file': $!\n"; |
| 125 |
flock $fh, LOCK_EX or die "Can't lock file '$session_file': $!\n"; |
| 126 |
seek $fh, 0, 0 or die "Can't seek in file '$session_file': $!\n"; |
| 127 |
truncate $fh, 0 or die "Can't truncate file '$session_file': $!\n"; |
| 128 |
binmode $fh, ':encoding(UTF-8)'; |
| 129 |
$self->_freeze_to_handle( $fh, $data ); |
| 130 |
close $fh or die "Can't close '$session_file': $!\n"; |
| 131 |
|
| 132 |
return $data; |
| 133 |
} |
| 134 |
|
| 135 |
1; |
| 136 |
|
| 137 |
__END__ |
| 138 |
|
| 139 |
=pod |
| 140 |
|
| 141 |
=encoding UTF-8 |
| 142 |
|
| 143 |
=head1 NAME |
| 144 |
|
| 145 |
Dancer2::Core::Role::SessionFactory::File - Role for file-based session factories |
| 146 |
|
| 147 |
=head1 VERSION |
| 148 |
|
| 149 |
version 2.2.2 |
| 150 |
|
| 151 |
=head1 DESCRIPTION |
| 152 |
|
| 153 |
This is a specialized SessionFactory role for storing session |
| 154 |
data in files. |
| 155 |
|
| 156 |
This role manages the files. Classes consuming it only need to handle |
| 157 |
serialization and deserialization. |
| 158 |
|
| 159 |
Classes consuming this must satisfy three requirements: C<_suffix>, |
| 160 |
C<_freeze_to_handle> and C<_thaw_from_handle>. |
| 161 |
|
| 162 |
package Dancer2::Session::XYX; |
| 163 |
|
| 164 |
use Dancer2::Core::Types; |
| 165 |
use Moo; |
| 166 |
|
| 167 |
has _suffix => ( |
| 168 |
is => 'ro', |
| 169 |
isa => Str, |
| 170 |
default => sub { '.xyz' }, |
| 171 |
); |
| 172 |
|
| 173 |
with 'Dancer2::Core::Role::SessionFactory::File'; |
| 174 |
|
| 175 |
sub _freeze_to_handle { |
| 176 |
my ($self, $fh, $data) = @_; |
| 177 |
|
| 178 |
# ... do whatever to get data into $fh |
| 179 |
|
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
sub _thaw_from_handle { |
| 184 |
my ($self, $fh) = @_; |
| 185 |
my $data; |
| 186 |
|
| 187 |
# ... do whatever to get data from $fh |
| 188 |
|
| 189 |
return $data; |
| 190 |
} |
| 191 |
|
| 192 |
1; |
| 193 |
|
| 194 |
=head1 ATTRIBUTES |
| 195 |
|
| 196 |
=head2 session_dir |
| 197 |
|
| 198 |
Where to store the session files. Defaults to "./sessions". |
| 199 |
|
| 200 |
=head1 AUTHOR |
| 201 |
|
| 202 |
Dancer Core Developers |
| 203 |
|
| 204 |
=head1 COPYRIGHT AND LICENSE |
| 205 |
|
| 206 |
This software is copyright (c) 2026 by Alexis Sukrieh. |
| 207 |
|
| 208 |
This is free software; you can redistribute it and/or modify it under |
| 209 |
the same terms as the Perl 5 programming language system itself. |
| 210 |
|
| 211 |
=cut |