print sprintf??? printf!
[catagits/Catalyst-Plugin-Session-State-Stash.git] / lib / Catalyst / Plugin / Session / State / Stash.pm
CommitLineData
642b19cf 1package Catalyst::Plugin::Session::State::Stash;
2use base qw/Catalyst::Plugin::Session::State Class::Accessor::Fast/;
3
f6c278f9 4#Need to look up which version of perl is required.
5#use 5.008;
642b19cf 6use strict;
7use warnings;
8use MRO::Compat;
9
10our $VERSION = "0.10";
11
12BEGIN { __PACKAGE__->mk_accessors(qw/_deleted_session_id _prepared/) }
13
e08a2683 14sub _stash_key_components {
642b19cf 15 my ($c) = @_;
e08a2683 16 return ($c->config->{session}->{stash_delim}) ?
17 split $c->config->{session}->{stash_delim}, $c->config->{session}->{stash_key} :
18 $c->config->{session}->{stash_key};
642b19cf 19}
20
21sub _get_session {
22 my ($c) = @_;
e08a2683 23 # This turns the list of path components into a nested tree of hashrefs for obtaining info/storing in: 123/456 = {123}->{456}
24 my $ref = $c->stash;
25 $ref = ($ref->{$_} ||= {}) foreach $c->_stash_key_components;
26 $ref;
642b19cf 27}
28
29sub _set_session {
30 my ( $c,$key,$value) = @_;
31
e08a2683 32 $c->_get_session->{$key} = $value;
642b19cf 33}
34
35sub setup_session {
36 my $c = shift;
37
38 $c->config->{session}->{stash_key}
39 ||= '_session';
40}
41
42sub prepare_action {
43 my $c = shift;
44 my $id = $c->get_session_id;
45 $c->_prepared(1);
46 if ( $id ) {
47 $c->sessionid( $id );
48 }
49 $c->maybe::next::method( @_ );
50}
51
52sub get_session_id {
53 my $c = shift;
54 if(!$c->_deleted_session_id and my $session = $c->_get_session) {
55 my $sid = $session->{id};
56 return $sid if $sid;
57 }
58 $c->maybe::next::method(@_);
59}
60
61sub set_session_id {
62 my ( $c, $sid ) = @_;
63 $c->_set_session(id => $sid);
64 $c->maybe::next::method($sid);
65}
66
67sub get_session_expires {
68 my $c = shift;
69 my $session = $c->_get_session;
70 defined $session->{expires} ? $session->{expires} : undef;
71}
72
73sub set_session_expires {
74 my ( $c, $expires ) = @_;
75
76 $c->_set_session(expires => time() + $expires);
77 $c->maybe::next::method($expires)
78}
79
80sub delete_session_id {
81 my ($c, $sid ) = @_;
82 $c->_deleted_session_id(1);
e08a2683 83 #Empty the tip
84 %{$c->_get_session} = ();
642b19cf 85 $c->maybe::next::method($sid);
86}
87
88
891;
90__END__
91
92=pod
93
94=head1 NAME
95
96Catalyst::Plugin::Session::State::Stash - Maintain session IDs using the stash
97
98=head1 SYNOPSIS
99
100 use Catalyst qw/Session Session::State::Stash Session::Store::Foo/;
101
102=head1 DESCRIPTION
103
104An alternative state storage plugin that allows you some more flexibility in
105dealing with session storage. This plugin loads and saves the session ID from
106and to the stash.
107
108=head1 METHODS
109
110=over 4
111
112=item delete_session_id
113
114Deletes the session. Unfortunately I've been unable to squash a bug that will
115stop you from opening a new session in the same execution, however.
116Patches welcome!
117
118=item get_session_id
119
120Gets the current session id.
121
122=item set_session_id
123
124Sets the session id to the C<shift>.
125
126=item get_session_expires
127
128Gets when the current session expires.
129
130=item set_session_expires
131
132Sets how many seconds from now the session should expire.
133
134=back
135
136=head1 EXTENDED METHODS
137
138=over 4
139
140=item prepare_action
141
142Loads the id off the stash.
143
144=item setup_session
145
146Defaults the C<stash_key> parameter to C<_session>.
147
148=back
149
150=head1 CONFIGURATION
151
152=over 4
153
154=item stash_key
155
156The name of the hash key to use. Defaults to C<_session>.
157
e08a2683 158=item stash_delim
159
160If present, splits stash_key at this character to nest. E.g. delim of '/'
161and key of '123/456' will store it as $c->stash->{123}->{456}
162
642b19cf 163=item expires
164
165How long the session should last in seconds.
166
167=back
168
169For example, you could stick this in MyApp.pm:
170
171 __PACKAGE__->config( session => {
172 stash_key => 'session_id',
173 });
174
175=head1 BUGS
176
177You can't delete a session then create a new one. If this is important to you,
178patches welcome. It is not important to me and fixing this for completeness
179is pretty low on my list of priorities.
180
181=head1 CAVEATS
182
183Manual work may be involved to make better use of this.
184
185If you are writing a stateful web service with
186L<Catalyst::Plugin::Server::XMLRPC>, you will probably only have to deal with
187loading, as when saving, the ID will already be on the stash.
188
189=head1 SEE ALSO
190
191L<Catalyst>, L<Catalyst::Plugin::Session>, L<Catalyst::Plugin::Session::State>,
192L<Catalyst::Plugin::Session::State::Cookie> (what you probably want).
193
194=head1 AUTHORS
195
196James Laver E<lt>perl -e 'printf qw/%s@%s.com cpan jameslaver/'E<gt>
197
198=head1 CONTRIBUTORS
199
200This module is derived from L<Catalyst::Plugin::Session::State::Cookie> code.
201
202Thanks to anyone who wrote code for that.
203
e08a2683 204Thanks to Kent Fredric for a patch for nested keys
205
642b19cf 206=head1 COPYRIGHT
207
208This program is free software, you can redistribute it and/or modify it
209under the same terms as Perl itself.
210
211=cut
212
2131;