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