blib/lib/Catalyst/Plugin/Session/Store/KiokuDB.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 113 CC 12 SCAR 30.3
CC
12
Cov
0%
store_session_data
56.0
setup_session
20.0
get_session_data
6.0
CRAP 20.6
1 package Catalyst::Plugin::Session::Store::KiokuDB;
2  
3 use strict;
4 use warnings;
5 use base qw/Catalyst::Plugin::Session::Store/;
6 use NEXT;
7 use KiokuDB;
8 use KiokuDB::Backend::BDB::GIN;
9  
10 our $VERSION = '0.02';
11  
12 sub new { return bless {}, shift; }
13  
14 sub setup_session {
15 my $c = shift;
16  
17 $c->NEXT::setup_session(@_);
18
19 my $confSess = $c->config->{session};
20 if ($confSess->{kiokuObject}) {
21 $confSess->{kioku} = $confSess->{kiokuObject};
22 }
23 elsif ($confSess->{kiokuDir}) {
24 $confSess->{kioku} = KiokuDB->new(
25 backend => KiokuDB::Backend::BDB::GIN->new(manager => { home => $confSess->{kiokuDir}, create => 1 }),
26 );
27 }
28 elsif ($confSess->{kiokuModel}) {
29 # This is a NOP - handled in get_kioku() below
30 }
31 else {
32 Catalyst::Exception->throw(
33 message => "KiokuDB requires at least 'kiokuObject', 'kiokuDir' or 'kiokuModel' (in conjunction with Catalyst::Model::KiokuDB) to be set."
34 );
35 }
36  
37 }
38  
39 sub get_kioku {
40 my ($c) = @_;
41
42 return $c->config->{session}->{kioku} ||
43 $c->model($c->config->{session}->{kiokuModel});
44 }
45
46 sub get_session_data {
47 my ($c, $key) = @_;
48
49 my $kioku = get_kioku($c);
50 my ($type, $id) = split ':', $key;
51
52 my $obj = $kioku->lookup($id) || return;
53 return $obj->expires if $type eq 'expires';
54 return $obj->data;
55 }
56  
57 sub store_session_data {
58 my ($c, $key, $data) = @_;
59
60 my $kioku = get_kioku($c);
61 my ($type, $id) = split ':', $key;
62
63 if (my $obj = $kioku->lookup($id)) {
64 if ($type eq 'expires') {
65 $obj->expires($data);
66 }
67 else {
68 $obj->flash(($type eq 'flash') ? 1 : 0);
69 $obj->data($data);
70 }
71 $kioku->store($obj); # no id means update
72 }
73 else {
74 my $obj = Catalyst::Plugin::Session::Store::KiokuDB::Session->new(
75 id => $id,
76 flash => ($type eq 'flash') ? 1 : 0,
77 expires => ($type eq 'expires') ? $data : undef,
78 data => ($type eq 'expires') ? {} : $data,
79 );
80 $kioku->store($id => $obj); # id means insert
81 }
82 return;
83 }
84  
85 sub delete_session_data {
86 my ($c, $key) = @_;
87
88 my $kioku = get_kioku($c);
89 my ($type, $id) = split ':', $key;
90  
91 return if $type eq 'expires';
92  
93 $kioku->delete($id);
94  
95 return;
96 }
97  
98 # not supported yet
99 sub delete_expired_sessions {}
100  
101  
102 package Catalyst::Plugin::Session::Store::KiokuDB::Session;
103 use Moose;
104  
105 has 'id' => (
106 is => 'ro',
107 isa => 'Str',
108 required => 1,
109 );
110  
111 has 'expires' => (
112 is => 'rw',
113 isa => 'Int',
114 );
115  
116 has 'flash' => (
117 is => 'rw',
118 isa => 'Bool',
119 );
120  
121 has 'data' => (
122 is => 'rw',
123 );
124  
125 1;
126 =pod
127  
128 =head1 NAME
129  
130 Catalyst::Plugin::Session::Store::KiokuDB - Store sessions using KiokuDB
131  
132 =head1 SYNOPSIS
133  
134 # In Catalyst
135 use Catalyst qw/
136 Session
137 Session::State::Whatever
138 Session::Store::KiokuDB
139 /;
140
141 # Configure it
142 MyApp->config->{session}->{kiokuDir} = '/path/to/storage/dir';
143 # or
144 MyApp->config->{session}->{kiokuObject} = KiokuDB->new(...);
145  
146 # then use it as you would any session plugin
147  
148 =head1 DESCRIPTION
149  
150 This session storage module will store data using L<KiokuDB>. Aside from that
151 it does pretty much the very same things other session modules do.
152  
153 =head1 CONFIGURATION
154  
155 Under the C<session> key in your configuration parameters, you can use
156 C<kiokuDir> which points to a directory in which KiokuDB will store its
157 data, C<kiokuObject> which allows you to reuse an existing KiokuDB instance
158 or C<kiokuModel> which points the name of a C<Catalyst> model that must be
159 of class L<Catalyst::Model::KiokuDB> (typically just 'kiokudb').
160  
161 =head1 METHODS
162  
163 These are the classic store methods from L<Catalyst::Plugin::Session::Store>.
164  
165 =head2 get_session_data
166  
167 =head2 store_session_data
168  
169 =head2 delete_session_data
170  
171 =head2 delete_expired_sessions
172  
173 This one is currently a no-op.
174  
175 =head2 setup_session
176  
177 =head1 SEE ALSO
178  
179 L<Catalyst>, L<Catalyst::Plugin::Session>, L<Catalyst::Model::KiokuDB>,
180 L<KiokuX::Model>
181  
182 =head1 MODULE HOME PAGE
183  
184 L<http://github.com/mzedeler/Catalyst-Plugin-Session-Store-KiokuDB>.
185  
186 If you find a bug, please fork the master branch from Github, write a test
187 case and push it to GitHub. After this, open an issue using Githubs issue
188 tracker.
189  
190 =head1 MAINTAINER
191  
192 Michael Zedeler, <michael@zedeler.dk>.
193  
194 =head1 ORIGINAL AUTHOR
195  
196 Robin Berjon, <robin@berjon.com>, L<http://robineko.com/>.
197  
198 =head1 COPYRIGHT
199  
200 This program is free software, you can redistribute it and/or modify it
201 under the same terms as Perl itself.
202  
203 =cut