1 package Catalyst::Plugin::Session::State::Stash;
5 use namespace::autoclean;
7 extends 'Catalyst::Plugin::Session::State';
11 has _deleted_session_id => ( is => 'rw' );
12 has _prepared => ( is => 'rw' );
14 sub _stash_key_components {
16 my $config = $c->_session_plugin_config;
17 return ($config->{stash_delim}) ?
18 split $config->{stash_delim}, $config->{stash_key} :
24 # This turns the list of path components into a nested tree of hashrefs for obtaining info/storing in: 123/456 = {123}->{456}
26 $ref = ($ref->{$_} ||= {}) foreach $c->_stash_key_components;
31 my ( $c,$key,$value) = @_;
32 $c->_get_session->{$key} = $value;
37 $c->_session_plugin_config->{stash_key} ||= '_session';
42 my $id = $c->get_session_id;
47 $c->maybe::next::method( @_ );
52 if(!$c->_deleted_session_id and my $session = $c->_get_session) {
53 my $sid = $session->{id};
56 $c->maybe::next::method(@_);
61 $c->_set_session(id => $sid);
62 $c->maybe::next::method($sid);
65 sub get_session_expires {
67 my $session = $c->_get_session;
68 defined $session->{expires} ? $session->{expires} : undef;
71 sub set_session_expires {
72 my ( $c, $expires ) = @_;
73 $c->_set_session(expires => time() + $expires);
74 $c->maybe::next::method($expires)
77 sub delete_session_id {
79 $c->_deleted_session_id(1);
81 %{$c->_get_session} = ();
82 $c->maybe::next::method($sid);
93 Catalyst::Plugin::Session::State::Stash - Maintain session IDs using the stash
97 use Catalyst qw/Session Session::State::Stash Session::Store::Foo/;
101 An alternative state storage plugin that allows you some more flexibility in
102 dealing with session storage. This plugin loads and saves the session ID from
109 =item delete_session_id
111 Deletes the session. Unfortunately I've been unable to squash a bug that will
112 stop you from opening a new session in the same execution, however.
117 Gets the current session id.
121 Sets the session id to the C<shift>.
123 =item get_session_expires
125 Gets when the current session expires.
127 =item set_session_expires
129 Sets how many seconds from now the session should expire.
133 =head1 EXTENDED METHODS
139 Loads the id off the stash.
143 Defaults the C<stash_key> parameter to C<_session>.
153 The name of the hash key to use. Defaults to C<_session>.
157 If present, splits stash_key at this character to nest. E.g. delim of '/'
158 and key of '123/456' will store it as $c->stash->{123}->{456}
162 How long the session should last in seconds.
166 For example, you could stick this in MyApp.pm:
168 __PACKAGE__->config( 'Plugin::Session' => {
169 stash_key => 'session_id',
174 You can't delete a session then create a new one. If this is important to you,
175 patches welcome. It is not important to me and fixing this for completeness
176 is pretty low on my list of priorities.
180 Manual work may be involved to make better use of this.
182 If you are writing a stateful web service with
183 L<Catalyst::Plugin::Server::XMLRPC>, you will probably only have to deal with
184 loading, as when saving, the ID will already be on the stash.
188 L<Catalyst>, L<Catalyst::Plugin::Session>, L<Catalyst::Plugin::Session::State>,
189 L<Catalyst::Plugin::Session::State::Cookie> (what you probably want).
193 James Laver E<lt>perl -e 'printf qw/%s@%s.com cpan jameslaver/'E<gt>
197 This module is derived from L<Catalyst::Plugin::Session::State::Cookie> code.
199 Thanks to anyone who wrote code for that.
201 Thanks to Kent Fredric for a patch for nested keys
205 This program is free software, you can redistribute it and/or modify it
206 under the same terms as Perl itself.