blib/lib/Mojolicious/Plugin/WWWSession.pm

filter: Statementstmt 62.9% 17 / 27 Branchbran n/a Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 71.4% 5 / 7 Podpod 100% 1 / 1 totaltotal 53.4% 23 / 43 CC 3 SCAR 11.7
CC
3
Cov
53%
register
6.0
__ANON__
6.0
BEGIN
1.0
CRAP 3.2
↑ Summary
1 package Mojolicious::Plugin::WWWSession;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Mojolicious::Plugin::WWWSession - Use WWWW::Session with Mojolicious
9  
10 =head2 DESCRIPTION
11  
12 An alternative session implementation for Mojolicious based on WWW::Session
13  
14 =head1 VERSION
15  
16 Version 0.05
17  
18 =cut
19  
20 2 our $VERSION = '0.06';
21  
22  
23 =head1 SYNOPSIS
24  
25 This module allows you to overwrite the standard Mojolicious session with a WWW::Session object and enjoy all the goodies it provides
26  
27 Example :
28  
29 =head2 Storage backends
30  
31 You can use one or more of the fallowing backends :
32  
33 =head3 File storage
34  
35 In you apllication module add the fallowing lines
36  
37 use Mojolicious::Plugin::WWWSession;
38  
39 sub startup {
40
41 ...
42
43 #Overwrite session
44 $self->plugin( WWWSession => { storage => [File => {path => '.'}] } );
45  
46 ...
47 }
48  
49 See WWW::Session::Storage::File for more details
50  
51 =head3 Database storage
52  
53 In you apllication module add the fallowing lines
54  
55 use Mojolicious::Plugin::WWWSession;
56  
57 sub startup {
58
59 ...
60
61 #Overwrite session
62 $self->plugin( WWWSession => { storage => [ MySQL => {
63 dbh => $dbh,
64 table => 'sessions',
65 fields => {
66 sid => 'session_id',
67 expires => 'expires',
68 data => 'data'
69 }
70 ]
71 } );
72  
73 ...
74 }
75  
76 The "fields" hasref contains the mapping of session internal data to the column names from MySQL.
77 The keys are the session fields ("sid","expires" and "data") and must all be present.
78  
79 The MySQL types of the columns should be :
80  
81 =over 4
82  
83 =item * sid => varchar(32)
84  
85 =item * expires => DATETIME or TIMESTAMP
86  
87 =item * data => text
88  
89 =back
90  
91 See WWW::Session::Storage::MySQL for more details
92  
93 =head3 Memcached storage
94  
95 In you apllication module add the fallowing lines
96  
97 use Mojolicious::Plugin::WWWSession;
98  
99 sub startup {
100
101 ...
102
103 #Overwrite session
104 $self->plugin( WWWSession => { storage => ['Memcached' => {servers => ['127.0.0.1:11211']}] } );
105  
106 ...
107 }
108  
109 See WWW::Session::Storage::Memcached for more details
110  
111  
112 =head1 Using the session
113  
114 This session can be used in the exact same way the strandard Mojolicious session is used
115  
116 =head1 Possible options for the plugin
117  
118 Here is an exmple containing the options you can pass to the plugin:
119  
120 {
121 storage => [ 'File' => { path => '/tmp/sessions'},
122 'Memcached' => { servers => ['127.0.0.1'] }
123 ],
124 serialization => 'JSON',
125 expires => 3600,
126 fields => {
127 user => {
128 inflate => sub { return Some::Package->new( $_[0] ) },
129 deflate => sub { $_[0]->id() },
130 }
131 age => {
132 filter => [21..99],
133 }
134 }
135
136 See WWW:Session for more details on possible options and on how you can use the session
137  
138 If you use the "Storable" serialization engine you can store objects in the session.
139 Also multiple session storage backends can be used simultaneously
140  
141 =cut
142  
143 2 ▶use base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 2 ▶use WWW::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
146 2 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
147  
148 =head1 METHODS
149  
150 =head2 register
151  
152 Called by Mojo when you register the plugin
153  
154 =cut
155  
156 0 ▶sub register {
Subroutine register: not called CC=2 SCAR=17.9
Pod documented
157 0 my ($self, $app, $args) = @_;
158  
159 0 ▶ $args ||= {};
Condition0%0/2
$args ||= {}
l!l
00
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
160
161 0 WWW::Session->import(%$args);
162  
163 $app->hook(
164 0 ▶ before_dispatch => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
165 0 my $self = shift;
166  
167 0 ▶ my $sid = $self->cookie('sid') || md5_hex($$ + time() + rand(time()));
Condition0%0/3
$self->cookie('sid') || md5_hex($$ + time + rand(time))
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->cookie('sid') || md5_hex($$ + time + rand(time))
$self->cookie('sid') md5_hex($$ + time + rand(time))
Truth table0%0/3
A || B
A= $self->cookie('sid')B= md5_hex($$ + time + rand(time))
ABresult
1X1
011
000
168  
169 0 $self->cookie(sid => $sid);
170  
171 0 my %session;
172  
173 0 tie %session, 'WWW::Session' , $sid, {sid => $sid}, $args->{expires};
174  
175 0 $self->stash('mojo.session' => \%session);
176 }
177 0 );
178  
179 }
180  
181  
182 =head1 AUTHOR
183  
184 Gligan Calin Horea, C<< <gliganh at gmail.com> >>
185  
186 Also thanks to Florian Adamsky for contributting with patches.
187  
188 =head1 BUGS
189  
190 Please report any bugs or feature requests to C<bug-mojolicious-plugin-wwwsession at rt.cpan.org>, or through
191 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mojolicious-Plugin-WWWSession>. I will be notified, and then you'll
192 automatically be notified of progress on your bug as I make changes.
193  
194  
195 =head1 SUPPORT
196  
197 You can find documentation for this module with the perldoc command.
198  
199 perldoc Mojolicious::Plugin::WWWSession
200  
201  
202 You can also look for information at:
203  
204 =over 4
205  
206 =item * RT: CPAN's request tracker (report bugs here)
207  
208 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mojolicious-Plugin-WWWSession>
209  
210 =item * AnnoCPAN: Annotated CPAN documentation
211  
212 L<http://annocpan.org/dist/Mojolicious-Plugin-WWWSession>
213  
214 =item * CPAN Ratings
215  
216 L<http://cpanratings.perl.org/d/Mojolicious-Plugin-WWWSession>
217  
218 =item * Search CPAN
219  
220 L<http://search.cpan.org/dist/Mojolicious-Plugin-WWWSession/>
221  
222 =back
223  
224  
225 =head1 ACKNOWLEDGEMENTS
226  
227  
228 =head1 LICENSE AND COPYRIGHT
229  
230 Copyright 2012 Gligan Calin Horea.
231  
232 This program is free software; you can redistribute it and/or modify it
233 under the terms of either: the GNU General Public License as published
234 by the Free Software Foundation; or the Artistic License.
235  
236 See http://dev.perl.org/licenses/ for more information.
237  
238  
239 =cut
240  
241 2 1; # End of Mojolicious::Plugin::WWWSession
↑ Summary