blib/lib/Dancer/Session/JSON.pm

filter: Statementstmt 100% 76 / 76 Branchbran 66.6% 16 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 18 / 18 Podpod 100% 7 / 7 totaltotal 93.6% 117 / 125 CC 13 SCAR 10.4
CC
13
Cov
92%
retrieve
5.1
init
5.0
flush
4.0
CRAP 2.8
« Build ↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Dancer::Session::JSON;
4 BEGIN {
5 2 ▶ $Dancer::Session::JSON::VERSION = '0.001';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 }
7 # ABSTRACT: JSON session backend for Dancer
8  
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use base 'Dancer::Session::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use JSON -convert_blessed_universally;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Dancer::ModuleLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Dancer::FileUtils qw(path set_file_mode);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # static
20  
21 2 my %session_dir_initialized;
22 2 my $json = JSON->new;
23  
24 sub init {
25 3 ▶ my $self = shift;
Subroutine init: called CC=5 SCAR=16.2
Pod documented
26  
27 3 $self->SUPER::init(@_);
28  
29 # default value for session_dir
30 3 ▶ defined setting('session_dir')
Branch: 1 branch
BranchTrueFalseTotal
defined setting("session_dir") or setting("session_dir", path(setting("appdir"), "sessions")) 1 2 3
31 or setting( session_dir => path( setting('appdir'), 'sessions' ) );
32  
33 3 my $session_dir = setting('session_dir');
34 3 ▶ if ( ! exists $session_dir_initialized{$session_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $session_dir_initialized{$session_dir}) 2 1 3
35 2 $session_dir_initialized{$session_dir} = 1;
36  
37 # make sure session_dir exists
38 2 ▶ if ( ! -d $session_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $session_dir) 2 0 2
39 2 ▶ mkdir $session_dir
Branch: 1 branch
BranchTrueFalseTotal
mkdir $session_dir or croak("session_dir $session_dir cannot be created") 0 2 2
40 or croak "session_dir $session_dir cannot be created";
41 }
42  
43 2 Dancer::Logger::core("session_dir: $session_dir");
44 }
45 }
46  
47 # create a new session and return the newborn object
48 # representing that session
49 sub create {
50 3 ▶ my ($class) = @_;
Subroutine create: called CC=1 SCAR=0.0
Pod documented
51  
52 3 my $self = Dancer::Session::JSON->new;
53 3 $self->flush;
54  
55 2 return $self;
56 }
57  
58 # deletes the dir cache
59 sub reset {
60 1 ▶ my ($class) = @_;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
61 1 %session_dir_initialized = ();
62 }
63  
64 # Return the session object corresponding to the given id
65 sub retrieve {
66 6 ▶ my $class = shift;
Subroutine retrieve: called CC=5 SCAR=16.3
Pod documented
67 6 my $id = shift;
68 6 my $session_file = json_file($id);
69  
70 6 ▶ return unless -f $session_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $session_file 2 4 6
71  
72 4 ▶ open my $fh, '+<', $session_file or die "Can't open '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "+<", $session_file or die "Can't open '${session_file}': $!\n" 0 4 4
73 4 ▶ flock $fh, LOCK_EX or die "Can't lock file '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or die "Can't lock file '${session_file}': $!\n" 0 4 4
74 4 my $json_data = do { local $/ = undef; <$fh>; };
75 4 my $content = $json->decode($json_data);
76 4 ▶ close $fh or die "Can't close '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close '${session_file}': $!\n" 0 4 4
77  
78 4 return bless $content => 'Dancer::Session::JSON';
79 }
80  
81 # instance
82 sub json_file {
83 12 ▶ my $id = shift;
Subroutine json_file: called CC=1 SCAR=0.0
Pod documented
84 12 return path( setting('session_dir'), "$id.json" );
85 }
86  
87 sub destroy {
88 1 ▶ my ($self) = @_;
Subroutine destroy: called CC=2 SCAR=7.0
Pod documented
89  
90 1 my $file = json_file( $self->id );
91 1 Dancer::Logger::core("trying to remove session file: $file");
92  
93 1 ▶ -f $file and unlink $file;
Branch: 1 branch
BranchTrueFalseTotal
-f $file and unlink $file 1 0 1
94 }
95  
96 sub flush {
97 4 ▶ my $self = shift;
Subroutine flush: called CC=4 SCAR=14.0
Pod documented
98 4 my $session_file = json_file( $self->id );
99  
100 4 ▶ open my $fh, '>', $session_file or die "Can't open '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $session_file or die "Can't open '${session_file}': $!\n" 1 3 4
101 3 ▶ flock $fh, LOCK_EX or die "Can't lock file '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or die "Can't lock file '${session_file}': $!\n" 0 3 3
102 3 set_file_mode($fh);
103 3 print {$fh} $json->allow_blessed->convert_blessed->encode($self);
104 3 ▶ close $fh or die "Can't close '$session_file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Can't close '${session_file}': $!\n" 0 3 3
105  
106 3 return $self;
107 }
108  
109 2 1;
110  
111  
112  
113 =pod
114  
115 =head1 NAME
116  
117 Dancer::Session::JSON - JSON session backend for Dancer
118  
119 =head1 VERSION
120  
121 version 0.001
122  
123 =head1 DESCRIPTION
124  
125 This module implements a session engine based on JSON files. Session are stored
126 in a I<session_dir> as JSON files. The idea behind this module was to provide a
127 transparent session storage for the developer.
128  
129 This backend is intended to be used in development environments, when looking
130 inside a session can be useful.
131  
132 It's not recommended to use this session engine in production environments.
133  
134 Typically you would want to use L<Dancer::Session::YAML> for this, which comes
135 in core, but a demand for a faster-but-still-file-based session backend arose,
136 and thus you now have JSON. :)
137  
138 =head1 CONFIGURATION
139  
140 The setting B<session> should be set to C<JSON> in order to use this session
141 engine in a Dancer application.
142  
143 Files will be stored to the value of the setting C<session_dir>, whose default
144 value is C<appdir/sessions>.
145  
146 Here is an example configuration that use this session engine and stores session
147 files in /tmp/dancer-sessions
148  
149 session: "JSON"
150 session_dir: "/tmp/dancer-sessions"
151  
152 =head1 SUBROUTINES/METHODS
153  
154 =head2 init
155  
156 Initializes the session backend.
157  
158 =head2 create
159  
160 Creates a new object, runs C<flush> and returns the object.
161  
162 =head2 flush
163  
164 Writes the session information to the session dir.
165  
166 =head2 retrieve
167  
168 Retrieves session information from the session dir.
169  
170 =head2 destroy
171  
172 Deletes session information from the session dir.
173  
174 =head2 reset
175  
176 Wipes the sessions directory, forcing a test for existence of the sessions
177 directory next time a session is created. It takes no argument.
178  
179 This is particulary useful if you want to remove the sessions directory on the
180 system where your app is running, but you want this session engine to continue
181 to work without having to restart your application.
182  
183 =head2 json_file
184  
185 Fetch the location of a json session file.
186  
187 =head1 SEE ALSO
188  
189 L<Dancer::Session::YAML> - the original core development session backend.
190  
191 L<Dancer::Session::Simple> - a faster in-memory core session backend.
192  
193 L<Dancer::Session::Cookie> - an encrypted cookie session backend, suitable
194 for production.
195  
196 =head1 AUTHOR
197  
198 Sawyer X <xsawyerx@cpan.org>
199  
200 =head1 COPYRIGHT AND LICENSE
201  
202 This software is copyright (c) 2011 by Sawyer X.
203  
204 This is free software; you can redistribute it and/or modify it under
205 the same terms as the Perl 5 programming language system itself.
206  
207 =cut
208  
209  
210 __END__
« Build ↑ Summary