WebApp/Session.pm

filter: Statementstmt 84.3% 54 / 64 Branchbran 87.5% 7 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 83.3% 15 / 18 Podpod 90.9% 10 / 11 totaltotal 77.4% 86 / 111 CC 7 SCAR 8.0
CC
7
Cov
78%
fetch
6.0
remove
6.0
types
2.3
CRAP 2.2
« WebApp/Segment/Template/Loader.pm ↑ Summary WebApp/Template/TT2.pm »
1 =head1 NAME
2  
3 OpenFrame::WebApp::Session - sessions for OpenFrame-WebApp
4  
5 =head1 SYNOPSIS
6  
7 use OpenFrame::WebApp::Session;
8  
9 my $session = new OpenFrame::WebApp::Session()
10 ->set($key1, $val1)->set($key2, $val2);
11  
12 $val = $session->get( $key );
13  
14 my $id = $session->store();
15  
16 my $restored = OpenFrame::WebApp::Session->fetch( $id );
17  
18 =cut
19  
20 package OpenFrame::WebApp::Session;
21  
22 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 10 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 10 ▶use Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 10 ▶use Digest::MD5 qw( md5_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 10 ▶use Time::ParseDate;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 10 ▶use OpenFrame::WebApp::Error::Abstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 10 our $VERSION = (split(/ /, '$Revision: 1.7 $'))[1];
31  
32 10 ▶use base qw( OpenFrame::Object );
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 10 our $TYPES = {
35 file_cache => 'OpenFrame::WebApp::Session::FileCache',
36 mem_cache => 'OpenFrame::WebApp::Session::MemCache',
37 };
38  
39 23 ▶sub types {
Subroutine types: called CC=2 SCAR=8.4
Pod documented
40 23 my $self = shift;
41 23 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 23 23
42 0 $TYPES = shift;
43 0 return $self;
44 } else {
45 23 return $TYPES;
46 }
47 }
48  
49 12 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
50 12 my $self = shift;
51 12 $self->id( $self->generate_id );
52 }
53  
54 27 ▶sub id {
Subroutine id: called CC=2 SCAR=6.9
Pod documented
55 27 my $self = shift;
56 27 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 12 15 27
57 12 $self->{session_id} = shift;
58 12 return $self;
59 } else {
60 15 return $self->{session_id};
61 }
62 }
63  
64 10 ▶sub expiry {
Subroutine expiry: called CC=2 SCAR=6.9
Pod documented
65 10 my $self = shift;
66 10 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 3 7 10
67 3 $self->{expiry_period} = shift;
68 3 return $self;
69 } else {
70 7 return $self->{expiry_period};
71 }
72 }
73  
74 5 ▶sub get_expiry_seconds {
Subroutine get_expiry_seconds: called CC=2 SCAR=6.9
Pod documented
75 5 my $self = shift;
76  
77 # using NOW => 0 causes parsedate() to uses the current time
78 5 my ($time, $err) = parsedate($self->expiry, NOW => 1);
79  
80 5 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 2 3 5
81 2 $self->error( "got [$err] parsing expiry time: " . $self->expiry );
82 2 return;
83 }
84  
85 3 return ($time-1);
86 }
87  
88 9 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
89 9 my $self = shift;
90 9 my $key = shift;
91 9 return $self->{$key};
92 }
93  
94 8 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
95 8 my $self = shift;
96 8 my $key = shift;
97 8 $self->{$key} = shift;
98 8 return $self;
99 }
100  
101 13 ▶sub generate_id {
Subroutine generate_id: called CC=1 SCAR=0.0
Pod documented
102 13 my $self = shift;
103 13 substr( md5_hex( time() . md5_hex(time(). {}. rand(). $$) ), 0, 32 );
104 }
105  
106 0 ▶sub store {
Subroutine store: not called CC=1 SCAR=6.9
Pod documented
107 0 my $self = shift;
108 0 throw OpenFrame::WebApp::Error::Abstract( class => ref($self) );
109 }
110  
111 0 ▶sub fetch {
Subroutine fetch: not called CC=2 SCAR=17.9
Pod documented
112 0 my $class = shift;
113 0 ▶ $class = ref($class) || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
114 0 throw OpenFrame::WebApp::Error::Abstract( class => $class );
115 }
116  
117 0 ▶sub remove {
Subroutine remove: not called CC=2 SCAR=17.9
Pod documented
118 0 my $class = shift;
119 0 ▶ $class = ref($class) || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
120 0 throw OpenFrame::WebApp::Error::Abstract( class => $class );
121 }
122  
123  
124 10 1;
125  
126 =head1 DESCRIPTION
127  
128 The C<OpenFrame::WebApp::Session> class is an abstract wrapper around session
129 storing classes like C<Cache::FileCache>, C<CGI::Session>, C<Apache::Session>,
130 etc.
131  
132 In WebApp, sessions are a storable hash with a session id, and an expiry
133 period.
134  
135 Just incase something like Pixie is used to store the sessions, you should
136 always use the set/get methods to retrieve keys from the hash.
137  
138 This class was meant to be used with C<OpenFrame::WebApp::Session::Factory>.
139  
140 =head1 METHODS
141  
142 =over 4
143  
144 =item types
145  
146 set/get the hash of $session_types => $class_names known to this class.
147  
148 =item $session->id
149  
150 set/get the session id (stored as 'session_id', FYI).
151  
152 =item $session->expiry
153  
154 set/get the expiry period (stored as 'expiry_period', FYI). This should be
155 a string compatible with C<Time::ParseDate>.
156  
157 =item $time = $session->get_expiry_seconds
158  
159 parses the expiry period with C<Time::ParseDate>, and returns the result in
160 seconds.
161  
162 Note: C<$time == undef> implies no expiry time, whereas C<$time <= 0> implies
163 expires immediately.
164  
165 =item $session->set( $key, $val )
166  
167 associates $key with $val, and returns this object.
168  
169 =item $val = $session->get( $key )
170  
171 returns value associated with $key.
172  
173 =item $id = $session->store
174  
175 abstract method, saves the session to disk and returns the session id.
176  
177 =item $session = $class->fetch( $id )
178  
179 abstract method, returns the session with the given $id or undef if not found
180 or expired.
181  
182 =item $session->remove( [ $id ] )
183  
184 abstract method, removes this object from the store, and returns this object.
185 if called as a class method, $id is expected.
186  
187 =item $id = $session->generate_id
188  
189 internal method to generate a new session id.
190  
191 =back
192  
193 =head1 SUB-CLASSING
194  
195 Read through the source of this package and the known sub-classes first.
196 The minumum you need to do is this:
197  
198 use base qw( OpenFrame::WebApp::Session );
199  
200 OpenFrame::WebApp::Session->types->{my_type} = __PACKAGE__;
201  
202 sub store {
203 ...
204 return $id;
205 }
206  
207 sub fetch {
208 ...
209 return new Some::Session();
210 }
211  
212 sub remove {
213 ...
214 return $self;
215 }
216  
217 You must register your session type if you want to use the Session::Factory.
218  
219 =head1 AUTHOR
220  
221 Steve Purkis <spurkis@epn.nu>
222  
223 Based on C<OpenFrame::AppKit::Session> by James A. Duncan
224  
225 =head1 COPYRIGHT
226  
227 Copyright (c) 2003 Steve Purkis. All rights reserved.
228 Released under the same license as Perl itself.
229  
230 =head1 SEE ALSO
231  
232 L<Time::ParseDate>,
233 L<OpenFrame::WebApp::Sesssion::Factory>,
234 L<OpenFrame::WebApp::Sesssion::FileCache>,
235 L<OpenFrame::WebApp::Segment::Sesssion::Loader>
236  
237 Similar modules:
238  
239 L<OpenFrame::AppKit::Session>,
240 L<CGI::Session>,
241 L<Apache::Session>
242  
243 =cut
« WebApp/Segment/Template/Loader.pm ↑ Summary WebApp/Template/TT2.pm »