Session.pm

filter: Statementstmt 88.7% 182 / 205 Branchbran 72.3% 55 / 76 Conditioncond 68.7% 22 / 32 MC/DCmcdc 50.0% 9 / 18 Subroutinesub 85.7% 30 / 35 Podpod 100% 17 / 17 totaltotal 82.2% 315 / 383 CC 59 SCAR 17.5
CC
59
Cov
83%
set
14.9
import
8.7
run_trigger
6.1
CRAP 5.8
« Session/Storage/MySQL.pm ↑ Summary Session/Storage/File.pm »
1 package WWW::Session;
2  
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 WWW::Session - Generic session management engine for web applications
10  
11 =head1 DESCRIPTION
12  
13 Generic session management engine for web applications with multiple backends,
14 object serialization and data validation
15  
16 =head1 VERSION
17  
18 Version 0.12
19  
20 =cut
21  
22 5 our $VERSION = '0.12';
23  
24 =head1 SYNOPSIS
25  
26 This module allows you to easily create sessions , store data in them and later
27 retrieve that information, using multiple storage backends
28  
29 Example:
30  
31 use WWW::Session;
32
33 #set up the storage backends
34 WWW::Session->add_storage( 'File', {path => '/tmp/sessions'} );
35 WWW::Session->add_storage( 'Memcached', {servers => ['127.0.0.1:11211']} );
36
37 #Set up the serialization engine (defaults to JSON)
38 WWW::Session->serialization_engine('JSON');
39
40 #Set up the default expiration time (in seconds or -1 for never)
41 WWW::Session->default_expiration_time(3600);
42  
43 #Turn on autosave
44 WWW::Session->autosave(1);
45
46 #and than ...
47
48 #Create a new session
49 my $session = WWW::Session->new($sid,$hash_ref);
50 ...
51 $session->sid(); #returns $sid
52 $session->data(); #returns $hash_ref
53  
54 #set the user
55 $session->user($user);
56 #retrieve the user
57 my $user = $session->user();
58  
59 #returns undef if it doesn't exist or it's expired
60 my $session = WWW::Session->find($sid);
61
62 #returns the existing session if it exists, creates a new session if it doesn't
63 my $session = WWW::Session->find_or_create($sid);
64  
65 Using the session :
66  
67 =over 4
68  
69 =item * Settings values
70  
71 There are two ways you can save a value on the session :
72  
73 $session->set('user',$user);
74
75 or
76
77 $session->user($user);
78
79 If the requested field ("user" in the example above) already exists it will be
80 assigned the new value, if it doesn't it will be added.
81  
82 When you set a value for a field it will be validated first (see setup_field() ).
83 If the value doesn't pass validation the field will keep it's old value and the
84 set method will return 0. If everything goes well the set method will return 1.
85
86 =item * Retrieving values
87  
88 my $user = $session->get('user');
89
90 or
91
92 my $user = $session->user();
93
94 If the requested field ("user" in the example above) already exists it will return
95 it's value, otherwise will return C<undef>
96  
97 =back
98  
99 We can automaticaly deflate/inflate certain informations when we store / retrieve
100 from storage the session data (see setup_field() for more details):
101  
102 WWW::Session->setup_field( 'user',
103 inflate => sub { return Some::Package->new( $_[0] ) },
104 deflate => sub { $_[0]->id() }
105 );
106  
107 We can automaticaly validate certain informations when we store / retrieve
108 from storage the session data (see setup_field() for more details):
109  
110 WWW::Session->setup_field( 'age',
111 filter => sub { $_[0] >= 18 }
112 );
113  
114  
115 Another way to initialize the module :
116  
117 use WWW::Session storage => [ 'File' => { path => '/tmp/sessions'},
118 'Memcached' => { servers => ['127.0.0.1'] }
119 ],
120 serialization => 'JSON',
121 expires => 3600,
122 fields => {
123 user => {
124 inflate => sub { return Some::Package->new( $_[0] ) },
125 deflate => sub { $_[0]->id() },
126 }
127 };
128
129 =cut
130  
131 #Internal variables
132 5 my @storage_engines = ();
133 5 my $serializer = undef;
134 5 my $default_expiration = -1;
135 5 my $fields_modifiers = {};
136 5 my $autosave = 1;
137  
138 #Set up the default serializer
139 5 __PACKAGE__->serialization_engine('JSON');
140  
141 =head1 SESSION & OBJECTS
142  
143 The default serialization engine is JSON, but JSON can't serialize objects by default,
144 you will have to write more code to accomplish that. If your session data data contains
145 objects you can take one of the following approaches :
146  
147 =over 4
148  
149 =item * Use inflate/deflate (recommended)
150  
151 # if we have a user object (eg MyApp::User) we can deflate it like this
152
153 WWW::Session->setup_field('user', deflate => sub { return $_[0]->id() } );
154
155 #and inflate it back like this
156
157 WWW::Session->setup_field('user',inflate => sub { return Some::Package->new( $_[0] ) } );
158
159 This method even thow it's slower, it reduces the size of the session object when stored, and
160 it ensures that if the object data changed since we saved it, this changes will be reflected in the
161 object when we retrieve restore it (usefull for database result objects)
162  
163 =item * Change the serialization module to 'Storable'
164  
165 The 'Storable' serialization engine can handle object without any additional changes
166  
167 WWW::Session->serialization_engine('Storable');
168  
169 Note : The perl Storable module is not very compatible between different version, so sharing data
170 between multiple machines could cause problems. We recommad using the 'JSON' engine with
171 inflate/defate (described above);
172  
173 =back
174  
175 =head1 STORAGE BACKENDS
176  
177 You can use one or more of the fallowing backends (the list might not be complete, more backends might be available on CPAN):
178  
179 =head2 File storage
180  
181 Here is how you can set up the File storage backend :
182  
183 use WWW::Session;
184  
185 WWW::Session->add_storage('File', {path => '.'} );
186  
187 See WWW::Session::Storage::File for more details
188  
189 =head2 Database storage
190  
191 If you want to store your session is MySQL do this :
192  
193 use WWW::Session;
194  
195 WWW::Session->add_storage( 'MySQL', {
196 dbh => $dbh,
197 table => 'sessions',
198 fields => {
199 sid => 'session_id',
200 expires => 'expires',
201 data => 'data'
202 },
203 }
204 );
205  
206 The "fields" hasref contains the mapping of session internal data to the column names from MySQL.
207 The keys are the session fields ("sid","expires" and "data") and must all be present.
208  
209 The MySQL types of the columns should be :
210  
211 =over 4
212  
213 =item * sid => varchar(32)
214  
215 =item * expires => DATETIME or TIMESTAMP
216  
217 =item * data => text
218  
219 =back
220  
221 See WWW::Session::Storage::MySQL for more details
222  
223 =head2 Memcached storage
224  
225 To use memcached as a storage backend do this :
226  
227 use WWW::Session;
228  
229 WWW::Session->add_storage('Memcached', {servers => ['127.0.0.1:11211']} );
230  
231  
232 See WWW::Session::Storage::Memcached for more details
233  
234  
235 =head1 SUBROUTINES/METHODS
236  
237 =head2 new
238  
239 Creates a new session object with the unique identifier and the given data.
240 If a session with the same identifier previously existed it will be overwritten
241  
242 Parameters
243  
244 =over 4
245  
246 =item * sid = unique id for this session
247  
248 =item * data = hash reference containing the data that we want to store in the session object
249  
250 =item * exipres = for how many secconds is this session valid (defaults to the default expiration time)
251  
252 =back
253  
254 Retuns a WWW::Session object
255  
256 Usage :
257  
258 my $session = WWW::Session->new('session_id',{ a=> 1, b=> 2});
259  
260 =cut
261  
262 15 ▶sub new {
Subroutine new: called CC=5 SCAR=16.5
Pod documented
263 15 my ($class,$sid,$data,$expires) = @_;
264
265 15 ▶ $expires ||= -1;
Condition50%1/2
$expires ||= -1
l!l
015
MC/DC0%0/1
$expires ||= -1
$expires
Truth table50%1/2
A
A= $expires
Aresult
11
00
266 15 ▶ $data ||= {};
Condition50%1/2
$data ||= {}
l!l
150
MC/DC0%0/1
$data ||= {}
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
267
268 15 ▶ die "You cannot use a undefined string as a session id!" unless $sid;
Branch: 1 branch
BranchTrueFalseTotal
unless $sid 0 15 15
269
270 15 my $self = {
271 data => {},
272 expires => $expires,
273 sid => $sid,
274 changed => {},
275 };
276
277 15 bless $self, $class;
278
279 15 $self->set($_,$data->{$_}) foreach keys %{$data};
280
281 15 return $self;
282 }
283  
284 =head2 find
285  
286 Retieves the session object for the given session id
287  
288 Usage :
289  
290 my $session = WWW::Session->find('session_id');
291  
292 =cut
293 8 ▶sub find {
Subroutine find: called CC=5 SCAR=16.1
Pod documented
294 8 my ($class,$sid) = @_;
295
296 8 ▶ die "You cannot use a undefined string as a session id!" unless $sid;
Branch: 1 branch
BranchTrueFalseTotal
unless $sid 0 8 8
297
298 8 my $info;
299
300 8 foreach my $storage (@storage_engines) {
301 8 $info = $storage->retrieve($sid);
302 8 ▶ last if defined $info;
Branch: 1 branch
BranchTrueFalseTotal
if defined $info 5 3 8
303 }
304
305 8 ▶ if ($info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) 5 3 8
306 5 my $session = $class->load($info);
307 5 $session->{changed} = {};
308 5 return $session;
309 }
310
311 3 return undef;
312 }
313  
314 =head2 find_or_create
315  
316 Retieves the session object for the given session id if it exists, if not it
317 creates a new object with the given session id
318  
319 =over 4
320  
321 =item * sid = unique id for this session
322  
323 =item * data = hash reference containing the data that we want to store in the session object
324  
325 =item * exipres = for how many secconds is this session valid (defaults to the default expiration time),
326  
327 =back
328  
329 Usage:
330  
331 my $session = WWW::Session->find_or_create('session_id',{ c=>2 })
332  
333 =cut
334 2 ▶sub find_or_create {
Subroutine find_or_create: called CC=4 SCAR=13.9
Pod documented
335 2 my ($class,$sid,$data,$expires) = @_;
336
337 2 my $self = $class->find($sid);
338
339 2 ▶ if ($self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) { } 1 1 2
340 1 ▶ $self->expires($expires) if defined ($expires);
Branch: 1 branch
BranchTrueFalseTotal
if defined $expires 0 1 1
341 1 $self->set($_,$data->{$_}) foreach keys %{$data};
342 }
343 else {
344 1 $self = $class->new($sid,$data,$expires);
345 }
346
347 2 return $self;
348 }
349  
350  
351 =head2 set
352  
353 Adds/sets a new value for the given field
354  
355 Usage :
356  
357 $session->set('user',$user);
358
359 The values can also be set by calling the name of the field you want to set
360 as a method :
361  
362 $session->user($user);
363  
364 =cut
365  
366 35 ▶sub set {
Subroutine set: called CC=14 SCAR=27.0
Pod documented
367 35 my ($self,$field,$value) = @_;
368
369 35 ▶ if (! defined $value && exists $fields_modifiers->{$field} && defined $fields_modifiers->{$field}->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value and exists $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"default"}) 0 35 35
Condition33%2/6
not defined $value and exists $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"default"}
!ll&&!rl&&r
3500
not defined $value and exists $fields_modifiers->{$field}
!ll&&!rl&&r
3500
MC/DC0%0/3
not defined $value and exists $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"default"}
not defined $value exists $fields_modifiers->{$field} defined $fields_modifiers->{$field}{"default"}
Truth table25%1/4
not(A) and B and C
A= not defined $valueB= exists $fields_modifiers->{$field}C= defined $fields_modifiers->{$field}{"default"}
ABCresult
0XX0
10X0
1100
1111
370 0 $value = $fields_modifiers->{$field}->{default};
371 }
372
373 35 $self->run_trigger('before_set_value',$field,$value,$self->get($field));
374
375 35 my $validated = 1;
376
377 35 ▶ if ( exists $fields_modifiers->{$field} && defined $fields_modifiers->{$field}->{filter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"filter"}) 6 29 35
Condition100%3/3
exists $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"filter"}
!ll&&!rl&&r
2546
MC/DC100%2/2
exists $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"filter"}
exists $fields_modifiers->{$field} defined $fields_modifiers->{$field}{"filter"}
Truth table100%3/3
A and B
A= exists $fields_modifiers->{$field}B= defined $fields_modifiers->{$field}{"filter"}
ABresult
0X0
100
111
378
379 6 $validated = 0; #we have a filter, check the value against the filter first
380
381 6 my $filter = $fields_modifiers->{$field}->{filter};
382
383 6 ▶ die "Filter must be a hash ref or array ref or code ref" unless ref($filter);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $filter 0 6 6
384
385 6 ▶ if (ref($filter) eq "ARRAY") {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $filter eq 'ARRAY') { } 2 4 6
elsif (ref $filter eq 'CODE') { } 2 2 4
elsif (ref $filter eq 'HASH') { } 2 0 2
386 2 ▶ if (grep { $value eq $_ } @{$filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$value eq $_;} @{$filter;}) 1 1 2
387 1 $validated = 1;
388 }
389 }
390 elsif (ref($filter) eq "CODE") {
391 2 $validated = $filter->($value);
392 }
393 elsif (ref($filter) eq "HASH") {
394 2 my $h_valid = 1;
395
396 2 ▶ if ( defined $filter->{isa} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter->{"isa"}) 2 0 2
397 2 ▶ $h_valid = 0 unless ref($value) eq $filter->{isa};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq $filter->{"isa"} 1 1 2
398 }
399
400 2 $validated = $h_valid;
401 }
402 }
403
404 35 ▶ if ($validated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validated) { } 32 3 35
405 32 $self->{data}->{$field} = $value;
406 32 $self->{changed}->{$field} = 1;
407
408 32 $self->run_trigger('after_set_value',$field,$value);
409 }
410 else {
411 3 warn "Value $value failed validation for key $field";
412 }
413
414 35 return $validated;
415 }
416  
417  
418 =head2 get
419  
420 Retrieves the value of the given key from the session object
421  
422 Usage :
423  
424 my $user = $session->get('user');
425
426 You can also use the name of the field you want to retrieve as a method.
427 The above call does the same as :
428  
429 my $user = $session->user();
430
431 =cut
432  
433 70 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
434 70 my ($self,$field) = @_;
435
436 70 return $self->{data}->{$field};
437 }
438  
439 =head2 delete
440  
441 Removes the given key from the session data
442  
443 Usage :
444  
445 $session->delete('user');
446  
447 =cut
448 3 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
449 3 my ($self,$field) = @_;
450
451 3 $self->run_trigger('before_delete',$field,$self->get($field));
452
453 3 $self->{changed}->{$field} = 1;
454 3 my $rv = delete $self->{data}->{$field};
455
456 3 $self->run_trigger('after_delete',$field,$self->get($field));
457
458 3 return $rv;
459 }
460  
461 =head2 sid
462  
463 Returns the session id associated with this session
464
465 =cut
466  
467 23 ▶sub sid {
Subroutine sid: called CC=1 SCAR=0.0
Pod documented
468 23 my ($self) = @_;
469
470 23 return $self->{sid};
471 }
472  
473 =head2 expires
474  
475 Getter/Setter for the expiration time of this session
476
477 =cut
478  
479 0 ▶sub expires {
Subroutine expires: not called CC=2 SCAR=17.9
Pod documented
480 0 my ($self,$value) = @_;
481  
482 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
483 0 $self->{expires} = $value;
484 }
485  
486 0 return $self->{expires};
487 }
488  
489 =head2 add_storage
490  
491 Adds a new storge engine to the list of Storage engines that will be used to
492 store the session info
493  
494 Usage :
495  
496 WWW::Session->add_storage($storage_engine_name,$storage_engine_options);
497
498 Parameters :
499  
500 =over 4
501  
502 =item * $storage_engine_name = Name of the class that defines a valid storage engine
503  
504 For WWW::Session::Storage::* modules you can use only the name of the storage,
505 you don't need the full name. eg Memcached and WWW::Session::Storage::Memcached
506 are synonyms
507  
508 =item * $storage_engine_options = hash ref containing the options that will be
509 passed on to the storage engine module when new() is called
510  
511 =back
512  
513 Example :
514  
515 WWW::Session->add_storage( 'File', {path => '/tmp/sessions'} );
516
517 WWW::Session->add_storage( 'Memcached', {servers => ['127.0.0.1:11211']} );
518  
519 See each storage module for aditional details
520  
521 =cut
522  
523 3 ▶sub add_storage {
Subroutine add_storage: called CC=5 SCAR=16.7
Pod documented
524 3 my ($class,$name,$options) = @_;
525
526 3 ▶ $options ||= {};
Condition50%1/2
$options ||= {}
l!l
30
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
527
528 3 ▶ if ($name !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /::/) 3 0 3
529 3 $name = "WWW::Session::Storage::$name";
530 }
531
532 3 ▶ eval "use $name";
Subroutine BEGIN: called CC=1 SCAR=0.0
533
534 3 ▶ die "WWW::Session cannot load '$name' storage engine! Error : $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
535
536 3 my $storage = $name->new($options);
537
538 3 ▶ if ($storage) {
Branch: 1 branch
BranchTrueFalseTotal
if ($storage) { } 3 0 3
539 3 push @storage_engines, $storage;
540 }
541 else {
542 0 die "WWW::Session storage engine '$name' failed to initialize with the given arguments!";
543 }
544 }
545  
546 =head2 serialization_engine
547  
548 Configures the serialization engine to be used for serialising sessions.
549  
550 The default serialization engine is JSON
551  
552 Usage :
553  
554 WWW::Session->serialization_engine('JSON');
555
556 Parameters :
557  
558 =over 4
559  
560 =item * $serialization_engine_name = Name of the class that defines a valid serialization engine
561  
562 For WWW::Session::Serialization::* modules you can use only the short name of the module,
563 you don't need the full name. eg JSON and WWW::Session::Serialization::JSON
564 are synonyms
565  
566 =back
567  
568 =cut
569  
570 8 ▶sub serialization_engine {
Subroutine serialization_engine: called CC=4 SCAR=14.1
Pod documented
571 8 my ($class,$name) = @_;
572
573 8 ▶ if ($name !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /::/) 8 0 8
574 8 $name = "WWW::Session::Serialization::$name";
575 }
576
577 5 ▶ eval "use $name";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
578
579 8 ▶ die "WWW::Session cannot load '$name' serialization engine! Error : $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
580
581 8 my $serializer_object = $name->new($fields_modifiers);
582
583 8 ▶ if ($serializer_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($serializer_object) { } 8 0 8
584 8 $serializer = $serializer_object;
585 }
586 else {
587 0 die "WWW::Session serialization engine '$name' failed to initialize!";
588 }
589 }
590  
591 =head2 autosave
592  
593 Turn on/off the autosave feature (on by default)
594  
595 If this feature is on the object will always be saved before beying destroyed
596  
597 Usage :
598  
599 WWW::Session->autosave(1);
600  
601 =cut
602  
603 3 ▶sub autosave {
Subroutine autosave: called CC=2 SCAR=7.1
Pod documented
604 3 my ($class,$value) = @_;
605
606 3 ▶ $autosave = $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 3 0 3
607
608 3 return $autosave;
609 }
610  
611 =head2 default_expiration_time
612  
613 Setter/Getter for the default expiration time
614  
615 Usage :
616  
617 WWW::Session->default_expiration_time(1800);
618
619 =cut
620  
621 0 ▶sub default_expiration_time {
Subroutine default_expiration_time: not called CC=2 SCAR=17.9
Pod documented
622 0 my ($class,$value) = @_;
623
624 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
625 0 $default_expiration = $value;
626 }
627
628 0 return $default_expiration;
629 }
630  
631 =head2 destroy
632  
633 Completely removes all the data related to the current session
634  
635 NOTE: After calling destroy the session object will no longer be usable
636  
637 Usage :
638  
639 $session->destroy();
640
641 =cut
642  
643 17 ▶sub destroy {
Subroutine destroy: called CC=2 SCAR=6.9
Pod documented
644
645 #save the session id fiers and undef the object before we delete it from
646 #storage to avoid autosave kikking in after we remove it from storage
647
648 17 my $sid = $_[0]->sid();
649
650 17 $_[0] = undef;
651
652 17 foreach my $storage (@storage_engines) {
653 17 $storage->delete($sid);
654 }
655 }
656  
657  
658 =head2 setup_field
659  
660 Sets up the filters, inflators and deflators for the given field
661  
662 =head3 deflators
663  
664 Deflators are passed as code refs. The only argument the deflator
665 method receives is the value of the filed that it must be deflated and
666 it must return a single value (scalar, object or reference) that will be
667 asigned to the key.
668  
669 Example :
670  
671 # if we have a user object (eg MyApp::User) we can deflate it like this
672
673 WWW::Session->setup_field('user', deflate => sub { return $_[0]->id() } );
674  
675 =head3 inflators
676  
677 Inflators are passed as code refs. The only argument the inflator
678 method receives is the value of the filed that it must inflate and
679 it must return a single value (scalar, object or reference) that will be
680 asigned to the key.
681  
682 Example :
683  
684 # if we have a user object (eg MyApp::User) we can inflate it like this
685  
686 WWW::Session->setup_field('user',inflate => sub { return Some::Package->new( $_[0] ) } );
687  
688 =head3 filters
689  
690 Filters can be used to ensure that the values from the session have the required values
691  
692 Filters can be :
693  
694 =over 4
695  
696 =item * array ref
697  
698 In this case when we call $session->set($field,$value) the values will have to be one of the
699 values from the array ref , or the operation will fail
700  
701 Example :
702  
703 #Check that the age is between 18 and 99
704 WWW::Session->setup_field('age',filter => [18..99] );
705  
706 =item * code ref
707  
708 In this case the field value will be passed to the code ref as the only parameter. The code ref
709 must return a true or false value. If it returns a false value the set() operation will fail
710  
711 Example :
712  
713 #Check that the age is > 18
714 WWW::Session->setup_field('age',filter => sub { $_[0] > 18 } );
715  
716 =item * hash ref
717  
718 In this case the only key from the hash that is recognised is "isa" will will chek that the
719 given value has the types specified as the value for "isa"
720  
721 Example :
722  
723 #Check that the 'rights' field is an array
724 WWW::Session->setup_field('age',filter => { isa => "ARRAY" } );
725
726 #Check that the 'user' field is an MyApp::User object
727 WWW::Session->setup_field('user',filter => { isa => "MyApp::User" } );
728  
729 =back
730  
731 =head3 triggers
732  
733 Triggers allow you to execute a code ref when certain events happen on the key.
734  
735 The return values from the triggers are completely ignored.
736  
737 Available triggers are:
738  
739 =over 4
740  
741 =item * before_set_value
742  
743 Executed before the value is actually storred on the code. Arguments sent to the code ref
744 are : session object , new value, old value - in this order
745  
746 =item * after_set_value
747  
748 Executed after the new value is set on the session object. Arguments sent to the code ref
749 are : session object, new value
750  
751 =item * before_delete
752  
753 Executed before the key is removed from the session object. Arguments sent to the code ref
754 are : session object, current_value
755  
756 =item * after_delete
757  
758 Executed after the key is removed from the session object. Arguments sent to the code ref
759 are : session object, previous_value
760  
761 =back
762  
763 Example :
764  
765 WWW::Session->setup_field(
766 'user',
767 filter => { isa => "MyApp::User" },
768 deflate => sub { $_[0]->id() },
769 inflate => sub { return MyApp::User->find($_[0]) }
770 trigger => { before_set_value => sub { warn "About to set the user },
771 after_delete => sub { ... },
772 }
773 );
774  
775 =cut
776  
777 7 ▶sub setup_field {
Subroutine setup_field: called CC=2 SCAR=6.9
Pod documented
778 7 my ($self,$field,%settings) = @_;
779
780 7 ▶ while (my ($key,$val) = each %settings) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %settings) 9 7 16
781 9 $fields_modifiers->{$field}{$key} = $val;
782 }
783 }
784  
785 =head2 save
786  
787 Serializes a WWW::Session object sends it to all storage engines for saving
788  
789 =cut
790  
791 19 ▶sub save {
Subroutine save: called CC=5 SCAR=16.1
Pod documented
792 19 my ($self) = @_;
793
794 my $data = {
795 sid => $self->{sid},
796 expires => $self->{expires},
797 19 };
798
799 19 foreach my $field ( keys %{$self->{data}} ) {
800 33 ▶ if (defined $fields_modifiers->{$field} && defined $fields_modifiers->{$field}->{deflate}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{'deflate'}) { } 2 31 33
Condition100%3/3
defined $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"deflate"}
!ll&&!rl&&r
2742
MC/DC100%2/2
defined $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"deflate"}
defined $fields_modifiers->{$field} defined $fields_modifiers->{$field}{"deflate"}
Truth table100%3/3
A and B
A= defined $fields_modifiers->{$field}B= defined $fields_modifiers->{$field}{"deflate"}
ABresult
0X0
100
111
801 2 $data->{data}->{$field} = $fields_modifiers->{$field}->{deflate}->($self->{data}->{$field});
802 }
803 else {
804 31 $data->{data}->{$field} = $self->{data}->{$field}
805 }
806 }
807
808 19 my $string = $serializer->serialize($data);
809
810 19 foreach my $storage (@storage_engines) {
811 19 $storage->save($self->{sid},$self->{expires},$string);
812 }
813 }
814  
815 =head1 ACCESSING SESSION DATA
816  
817 Allows us to get/set session data directly by calling the field name as a method
818  
819 Example:
820  
821 my $user = $session->user(); #same as $user = $session->get('user');
822
823 #or
824
825 $session->age(21); #same as $session->set('age',21);
826  
827 =cut
828  
829 5 our $AUTOLOAD;
830 4 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
831 4 my $self = shift;
832 4 my $value = shift;
833  
834 4 my $field = $AUTOLOAD;
835  
836 4 $field =~ s/.*:://;
837  
838 4 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 2 2 4
839 2 $self->set($field,$value);
840 }
841
842 4 return $self->get($field);
843 }
844  
845  
846 =head1 AUTOSAVE FEATURE
847  
848 If you set autosave to 1 the session will be saved before the object is
849 destroyed if any data has changed
850  
851 BE CAREFULL : If you store complex structures only the changes made to direct
852 session keys will be detected.
853  
854 Example :
855  
856 #this change will be detected because it affects a direct session attribute
857 $session->age(21);
858  
859 #this changes won't be detected :
860 my $user = $session->user();
861 $user->{age} = 21;
862
863 You have two choices :
864  
865 =over 4
866  
867 =item 1 Make a change that can be detected
868  
869 $session->some_random_field( time() );
870
871 =item 2 Save the session manually
872  
873 $session->save();
874
875 =back
876
877 =cut
878  
879 20 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
880 20 my $self = shift;
881
882 20 ▶ if ($autosave && scalar(keys %{$self->{changed}})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autosave and scalar keys %{$$self{"changed"};}) 14 6 20
Condition100%2/2
$autosave and scalar keys %{$$self{"changed"};}
!ll
218
MC/DC100%1/1
$autosave and scalar keys %{$$self{"changed"};}
$autosave
Truth table100%2/2
A
A= $autosave
Aresult
00
11
883 14 $self->save();
884 }
885 }
886  
887  
888 =head1 PRIVATE METHODS
889  
890 =head2 load
891  
892 Deserializes a WWW::Session object from the given string and deflates all the fields that
893 were inflated when the session was serialized
894  
895 =cut
896  
897 5 ▶sub load {
Subroutine load: called CC=4 SCAR=13.9
Pod documented
898 5 my ($class,$string) = @_;
899
900 5 my $self = $serializer->expand($string);
901
902 5 foreach my $field ( keys %{$self->{data}} ) {
903 9 ▶ if (defined $fields_modifiers->{$field} && defined $fields_modifiers->{$field}->{inflate}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"inflate"}) 1 8 9
Condition66%2/3
defined $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"inflate"}
!ll&&!rl&&r
801
MC/DC50%1/2
defined $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"inflate"}
defined $fields_modifiers->{$field} defined $fields_modifiers->{$field}{"inflate"}
Truth table66%2/3
A and B
A= defined $fields_modifiers->{$field}B= defined $fields_modifiers->{$field}{"inflate"}
ABresult
0X0
100
111
904 1 $self->{data}->{$field} = $fields_modifiers->{$field}->{inflate}->($self->{data}->{$field});
905 }
906 }
907
908 5 bless $self,$class;
909
910 5 return $self;
911 }
912  
913 =head2 import
914  
915 Allows us to configure all the module options in one line
916  
917 Example :
918  
919 use WWW::Session storage => [
920 'File' => { path => '/tmp/sessions'},
921 'Memcached' => { servers => ['127.0.0.1'] }
922 ],
923 serialization => 'Storable',
924 expires => 3600,
925 fields => {
926 user => {
927 inflate => sub { return Some::Package->new( $_[0]->id() ) },
928 deflate => sub { $_[0]->id() },
929 },
930 age => {
931 filter => [21..99],
932 }
933 },
934 autosave => 1;
935  
936 =cut
937  
938 4 ▶sub import {
Subroutine import: called CC=8 SCAR=21.6
939 4 my ($class, %params) = @_;
940
941 4 ▶ if (defined $params{storage}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"storage"}) 1 3 4
942 1 ▶ while ( scalar(@{$params{storage}}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$params{"storage"};}) 1 1 2
943 1 my $engine = shift @{$params{storage}};
944 1 my $options = shift @{$params{storage}};
945 1 $class->add_storage($engine,$options);
946 }
947 }
948 4 ▶ if (defined $params{serialization}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"serialization"}) 1 3 4
949 1 $class->serialization_engine($params{serialization});
950 }
951 4 ▶ if (defined $params{expires}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"expires"}) 0 4 4
952 0 $class->default_expiration_time($params{expires});
953 }
954 4 ▶ if (defined $params{autosave}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"autosave"}) 1 3 4
955 1 $class->autosave($params{autosave});
956 }
957 4 ▶ if (defined $params{fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"fields"}) 0 4 4
958 0 foreach my $field (keys %{$params{fields}}) {
959 0 $class->setup_field($field,%{ $params{fields}->{$field} });
960 }
961 }
962 }
963  
964 =head2 run_trigger
965  
966 Runs a trigger for the given field
967  
968 =cut
969 73 ▶sub run_trigger {
Subroutine run_trigger: called CC=6 SCAR=18.1
Pod documented
970 73 my $self = shift;
971 73 my $trigger = shift;
972 73 my $field = shift;
973
974 73 ▶ if ( exists $fields_modifiers->{$field}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"trigger"} and defined $fields_modifiers->{$field}{"trigger"}{$trigger}) 4 69 73
Condition100%6/6
exists $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"trigger"} and defined $fields_modifiers->{$field}{"trigger"}{$trigger}
!ll&&!rl&&r
6184
exists $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"trigger"}
!ll&&!rl&&r
52912
MC/DC100%3/3
exists $fields_modifiers->{$field} and defined $fields_modifiers->{$field}{"trigger"} and defined $fields_modifiers->{$field}{"trigger"}{$trigger}
exists $fields_modifiers->{$field} defined $fields_modifiers->{$field}{"trigger"} defined $fields_modifiers->{$field}{"trigger"}{$trigger}
Truth table100%4/4
A and B and C
A= exists $fields_modifiers->{$field}B= defined $fields_modifiers->{$field}{"trigger"}C= defined $fields_modifiers->{$field}{"trigger"}{$trigger}
ABCresult
0XX0
10X0
1100
1111
975 && defined $fields_modifiers->{$field}{trigger}
976 && defined $fields_modifiers->{$field}{trigger}{$trigger} )
977 {
978 4 my $trigger = $fields_modifiers->{$field}{trigger}{$trigger};
979 4 ▶ die "WWW::Session triggers must be code refs!" unless ref( $trigger ) && ref( $trigger ) eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $trigger and ref $trigger eq "CODE" 0 4 4
Condition33%1/3
ref $trigger and ref $trigger eq "CODE"
!ll&&!rl&&r
004
MC/DC0%0/2
ref $trigger and ref $trigger eq "CODE"
ref $trigger ref $trigger eq "CODE"
Truth table33%1/3
A and B
A= ref $triggerB= ref $trigger eq "CODE"
ABresult
0X0
100
111
980 4 $trigger->( $self, @_ );
981 }
982 }
983  
984  
985 =head1 TIE INTERFACE
986  
987 The WWW::Session objects can be tied to hashes to make them easier to use
988  
989 Example :
990  
991 my %session;
992
993 tie %session, WWW::Session, 'session_id', {user => $user, authenticated => 1};
994
995 ...
996 my $user = $session{user};
997  
998 ...
999 $session{authenticated} = 0;
1000 delete $session{user};
1001  
1002 =cut
1003  
1004 1 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
1005 1 my ($class,@params) = @_;
1006
1007 1 return $class->find_or_create(@params);
1008 }
1009  
1010 1 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
1011 1 my ($self,$key,$value) = @_;
1012
1013 1 $self->set($key,$value);
1014 }
1015  
1016 3 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
1017 3 my ($self,$key) = @_;
1018
1019 3 return $self->get($key);
1020 }
1021  
1022 1 ▶sub DELETE {
Subroutine DELETE: called CC=1 SCAR=0.0
1023 1 my ($self,$key) = @_;
1024
1025 1 $self->delete($key);
1026 }
1027  
1028 0 ▶sub CLEAR {
Subroutine CLEAR: not called CC=1 SCAR=6.9
1029 0 my ($self) = @_;
1030
1031 0 $self->{data} = {};
1032 }
1033  
1034 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=1 SCAR=6.9
1035 0 my ($self,$key) = @_;
1036
1037 0 return exists $self->{data}->{$key};
1038 }
1039  
1040 3 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
1041 3 my ($self) = @_;
1042
1043 3 my $a = keys %{ $self->{data} };
1044
1045 3 each %{ $self->{data} };
1046 }
1047  
1048 5 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=1 SCAR=0.0
1049 5 my ($self) = @_;
1050
1051 5 return each %{ $self->{data} };
1052 }
1053  
1054 0 ▶sub SCALAR {
Subroutine SCALAR: not called CC=1 SCAR=6.9
1055 0 my ($self) = @_;
1056
1057 0 return scalar %{ $self->{data} };
1058 }
1059  
1060 =head1 AUTHOR
1061  
1062 Gligan Calin Horea, C<< <gliganh at gmail.com> >>
1063  
1064 =head1 BUGS
1065  
1066 Please report any bugs or feature requests to C<bug-www-session at rt.cpan.org>, or through
1067 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Session>. I will be notified, and then you'll
1068 automatically be notified of progress on your bug as I make changes.
1069  
1070  
1071 =head1 SUPPORT
1072  
1073 You can find documentation for this module with the perldoc command.
1074  
1075 perldoc WWW::Session
1076  
1077  
1078 You can also look for information at:
1079  
1080 =over 4
1081  
1082 =item * RT: CPAN's request tracker (report bugs here)
1083  
1084 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Session>
1085  
1086 =item * AnnoCPAN: Annotated CPAN documentation
1087  
1088 L<http://annocpan.org/dist/WWW-Session>
1089  
1090 =item * CPAN Ratings
1091  
1092 L<http://cpanratings.perl.org/d/WWW-Session>
1093  
1094 =item * Search CPAN
1095  
1096 L<http://search.cpan.org/dist/WWW-Session/>
1097  
1098 =back
1099  
1100  
1101 =head1 ACKNOWLEDGEMENTS
1102  
1103  
1104 =head1 LICENSE AND COPYRIGHT
1105  
1106 Copyright 2012 Gligan Calin Horea.
1107  
1108 This program is free software; you can redistribute it and/or modify it
1109 under the terms of either: the GNU General Public License as published
1110 by the Free Software Foundation; or the Artistic License.
1111  
1112 See http://dev.perl.org/licenses/ for more information.
1113  
1114  
1115 =cut
1116  
1117 5 1; # End of WWW::Session
« Session/Storage/MySQL.pm ↑ Summary Session/Storage/File.pm »