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;
38 $c->maybe::next::method(@_);
40 $c->_session_plugin_config->{stash_key} ||= '_session';
45 my $id = $c->get_session_id;
50 $c->maybe::next::method( @_ );
55 if(!$c->_deleted_session_id and my $session = $c->_get_session) {
56 my $sid = $session->{id};
59 $c->maybe::next::method(@_);
64 $c->_set_session(id => $sid);
65 $c->maybe::next::method($sid);
68 sub get_session_expires {
70 my $session = $c->_get_session;
71 defined $session->{expires} ? $session->{expires} : undef;
74 sub set_session_expires {
75 my ( $c, $expires ) = @_;
76 $c->_set_session(expires => time() + $expires);
77 $c->maybe::next::method($expires)
80 sub delete_session_id {
82 $c->_deleted_session_id(1);
84 %{$c->_get_session} = ();
85 $c->maybe::next::method($sid);
96 Catalyst::Plugin::Session::State::Stash - Maintain session IDs using the stash
100 use Catalyst qw/Session Session::State::Stash Session::Store::Foo/;
104 An alternative state storage plugin that allows you some more flexibility in
105 dealing with session storage. This plugin loads and saves the session ID from
112 =item delete_session_id
114 Deletes the session. Unfortunately I've been unable to squash a bug that will
115 stop you from opening a new session in the same execution, however.
120 Gets the current session id.
124 Sets the session id to the C<shift>.
126 =item get_session_expires
128 Gets when the current session expires.
130 =item set_session_expires
132 Sets how many seconds from now the session should expire.
136 =head1 EXTENDED METHODS
142 Loads the id off the stash.
146 Defaults the C<stash_key> parameter to C<_session>.
156 The name of the hash key to use. Defaults to C<_session>.
160 If present, splits C<stash_key> at this character to nest. E.g. a C<delim> of '/'
161 and C<stash_key> of '123/456' will store it as $c->stash->{123}->{456}
165 How long the session should last in seconds.
169 For example, you could stick this in F<MyApp.pm>:
171 __PACKAGE__->config( 'Plugin::Session' => {
172 stash_key => 'session_id',
177 You can't delete a session then create a new one. If this is important to you,
178 patches welcome. It is not important to me and fixing this for completeness
179 is pretty low on my list of priorities.
183 Manual work may be involved to make better use of this.
185 =for stopwords stateful
187 If you are writing a stateful web service with
188 L<Catalyst::Plugin::Server::XMLRPC>, you will probably only have to deal with
189 loading, as when saving, the ID will already be on the stash.
193 L<Catalyst>, L<Catalyst::Plugin::Session>, L<Catalyst::Plugin::Session::State>,
194 L<Catalyst::Plugin::Session::State::Cookie> (what you probably want).
198 James Laver E<lt>perl -e 'printf qw/%s@%s.com cpan jameslaver/'E<gt>
202 This module is derived from L<Catalyst::Plugin::Session::State::Cookie> code.
204 Thanks to anyone who wrote code for that.
206 Thanks to Kent Fredric for a patch for nested keys
210 This program is free software, you can redistribute it and/or modify it
211 under the same terms as Perl itself.