1 package Catalyst::Plugin::Session::State::Stash;
2 # ABSTRACT: Maintain session IDs using the stash
7 use namespace::autoclean;
9 extends 'Catalyst::Plugin::Session::State';
11 our $VERSION = '0.15';
13 has _deleted_session_id => ( is => 'rw' );
14 has _prepared => ( is => 'rw' );
16 sub _stash_key_components {
18 my $config = $c->_session_plugin_config;
19 return ($config->{stash_delim}) ?
20 split $config->{stash_delim}, $config->{stash_key} :
26 # This turns the list of path components into a nested tree of hashrefs for obtaining info/storing in: 123/456 = {123}->{456}
28 $ref = ($ref->{$_} ||= {}) foreach $c->_stash_key_components;
33 my ( $c,$key,$value) = @_;
34 $c->_get_session->{$key} = $value;
40 $c->maybe::next::method(@_);
42 $c->_session_plugin_config->{stash_key} ||= '_session';
47 my $id = $c->get_session_id;
52 $c->maybe::next::method( @_ );
57 if(!$c->_deleted_session_id and my $session = $c->_get_session) {
58 my $sid = $session->{id};
61 $c->maybe::next::method(@_);
66 $c->_set_session(id => $sid);
67 $c->maybe::next::method($sid);
70 sub get_session_expires {
72 my $session = $c->_get_session;
73 defined $session->{expires} ? $session->{expires} : undef;
76 sub set_session_expires {
77 my ( $c, $expires ) = @_;
78 $c->_set_session(expires => time() + $expires);
79 $c->maybe::next::method($expires)
82 sub delete_session_id {
84 $c->_deleted_session_id(1);
86 %{$c->_get_session} = ();
87 $c->maybe::next::method($sid);
98 use Catalyst qw/Session Session::State::Stash Session::Store::Foo/;
102 An alternative state storage plugin that allows you some more flexibility in
103 dealing with session storage. This plugin loads and saves the session ID from
110 =item delete_session_id
112 Deletes the session. Unfortunately I've been unable to squash a bug that will
113 stop you from opening a new session in the same execution, however.
118 Gets the current session id.
122 Sets the session id to the C<shift>.
124 =item get_session_expires
126 Gets when the current session expires.
128 =item set_session_expires
130 Sets how many seconds from now the session should expire.
134 =head1 EXTENDED METHODS
140 Loads the id off the stash.
144 Defaults the C<stash_key> parameter to C<_session>.
154 The name of the hash key to use. Defaults to C<_session>.
158 If present, splits C<stash_key> at this character to nest. E.g. a C<delim> of '/'
159 and C<stash_key> of '123/456' will store it as $c->stash->{123}->{456}
163 How long the session should last in seconds.
167 For example, you could stick this in F<MyApp.pm>:
169 __PACKAGE__->config( 'Plugin::Session' => {
170 stash_key => 'session_id',
175 You can't delete a session then create a new one. If this is important to you,
180 Manual work may be involved to make better use of this.
182 =for stopwords stateful
184 If you are writing a stateful web service with
185 L<Catalyst::Plugin::Server::XMLRPC>, you will probably only have to deal with
186 loading, as when saving, the ID will already be on the stash.
190 L<Catalyst>, L<Catalyst::Plugin::Session>, L<Catalyst::Plugin::Session::State>,
191 L<Catalyst::Plugin::Session::State::Cookie> (what you probably want).
198 This module is derived from L<Catalyst::Plugin::Session::State::Cookie> code.
199 Thanks to anyone who wrote code for that.
202 Thanks to Kent Fredric for a patch for nested keys