Huge rewrite of the code: $session->get, ->set etc. do not read from
[catagits/Web-Session.git] / lib / Plack / Middleware / Session.pm
CommitLineData
bd992981 1package Plack::Middleware::Session;
2use strict;
3use warnings;
4
000c696e 5our $VERSION = '0.03';
30cc0a71 6our $AUTHORITY = 'cpan:STEVAN';
7
bd992981 8use Plack::Session;
9use Plack::Request;
10use Plack::Response;
ad80e445 11use Plack::Util;
12use Scalar::Util;
bd992981 13
14use parent 'Plack::Middleware';
15
d6af4aa8 16use Plack::Util::Accessor qw(
17 state
18 store
19 session_class
20);
bd992981 21
fe1bfe7d 22sub prepare_app {
23 my $self = shift;
fe1bfe7d 24
d6af4aa8 25 $self->session_class( 'Plack::Session' ) unless $self->session_class;
26 $self->state( 'Cookie' ) unless $self->state;
ad80e445 27
28 $self->state( $self->inflate_backend('Plack::Session::State', $self->state) );
29 $self->store( $self->inflate_backend('Plack::Session::Store', $self->store) );
30}
31
32sub inflate_backend {
33 my($self, $prefix, $backend) = @_;
34
35 return $backend if defined $backend && Scalar::Util::blessed $backend;
36
37 my @class;
38 push @class, $backend if defined $backend; # undef means the root class
39 push @class, $prefix;
40
41 Plack::Util::load_class(@class)->new();
fe1bfe7d 42}
43
bd992981 44sub call {
45 my $self = shift;
46 my $env = shift;
47
4a0cb5a0 48 my $session = $self->session_class->fetch_or_create( Plack::Request->new($env), $self );
49
50 $env->{'psgix.session'} = $env->{'plack.session'} = $session;
bd992981 51
fe1bfe7d 52 my $res = $self->app->($env);
53 $self->response_cb($res, sub {
54 my $res = Plack::Response->new(@{$_[0]});
b84f31d0 55 $env->{'psgix.session'}->finalize( $res );
b2504d01 56 $res = $res->finalize;
57 $_[0]->[0] = $res->[0];
58 $_[0]->[1] = $res->[1];
fe1bfe7d 59 });
bd992981 60}
61
621;
63
64__END__
ac4892f4 65
66=pod
67
68=head1 NAME
69
70Plack::Middleware::Session - Middleware for session management
71
72=head1 SYNOPSIS
73
3d92cf47 74 use Plack::Builder;
ac4892f4 75
3d92cf47 76 my $app = sub {
536da026 77 my $env = shift;
78 return [
79 200,
80 [ 'Content-Type' => 'text/plain' ],
b84f31d0 81 [ 'Hello, your Session ID is ' . $env->{'psgix.session'}->id ]
536da026 82 ];
3d92cf47 83 };
84
85 builder {
86 enable 'Session';
87 $app;
88 };
89
ad80e445 90 # Or, use the File store backend (great if you use multiprocess server)
91 # For more options, see perldoc Plack::Session::Store::File
92 builder {
93 enable 'Session', store => 'File';
94 $app;
95 };
96
ac4892f4 97=head1 DESCRIPTION
98
3d92cf47 99This is a Plack Middleware component for session management. By
ad80e445 100default it will use cookies to keep session state and store data in
101memory. This distribution also comes with other state and store
102solutions. See perldoc for these backends how to use them.
3d92cf47 103
536da026 104It should be noted that we store the current session in the
b84f31d0 105C<psgix.session> key inside the C<$env> where you can access it
536da026 106as needed. Additionally, as of version 0.09, you can call the
107C<session> method of a L<Plack::Request> instance to fetch
b84f31d0 108whatever is stored in C<psgix.session>.
109
110B<NOTE:> As of version 0.02 the session is stored in C<psgix.session>
111instead of C<plack.session>. We still keep a copy of it in
112C<plack.session>, but this is deprecated and will be removed
113in future versions.
536da026 114
3d92cf47 115=head2 State
116
117=over 4
118
119=item L<Plack::Session::State>
120
121This will maintain session state by passing the session through
122the request params. It does not do this automatically though,
123you are responsible for passing the session param.
124
125=item L<Plack::Session::State::Cookie>
126
127This will maintain session state using browser cookies.
128
129=back
130
131=head2 Store
132
133=over 4
134
135=item L<Plack::Session::Store>
136
137This is your basic in-memory session data store. It is volatile storage
138and not recommended for multiprocessing environments. However it is
139very useful for development and testing.
140
141=item L<Plack::Session::Store::File>
142
143This will persist session data in a file. By default it uses
144L<Storable> but it can be configured to have a custom serializer and
145deserializer.
146
20ede533 147=item L<Plack::Session::Store::Cache>
3d92cf47 148
20ede533 149This will persist session data using the L<Cache> interface.
3d92cf47 150
151=item L<Plack::Session::Store::Null>
152
153Sometimes you don't care about storing session data, in that case
154you can use this noop module.
155
156=back
157
30cc0a71 158=head1 OPTIONS
159
160The following are options that can be passed to this mdoule.
161
162=over 4
163
164=item I<state>
165
166This is expected to be an instance of L<Plack::Session::State> or an
167object that implements the same interface. If no option is provided
168the default L<Plack::Session::State::Cookie> will be used.
169
170=item I<store>
171
172This is expected to be an instance of L<Plack::Session::Store> or an
173object that implements the same interface. If no option is provided
174the default L<Plack::Session::Store> will be used.
175
176It should be noted that this default is an in-memory volatile store
177is only suitable for development (or single process servers). For a
178more robust solution see L<Plack::Session::Store::File> or
179L<Plack::Session::Store::Cache>.
180
181=item I<session_class>
182
183This can be used to override the actual session class. It currently
184defaults to L<Plack::Session> but you can substitute any class which
185implements the same interface.
186
187=back
188
ac4892f4 189=head1 BUGS
190
191All complex software has bugs lurking in it, and this module is no
192exception. If you find a bug please either email me, or add the bug
193to cpan-RT.
194
195=head1 AUTHOR
196
197Tatsuhiko Miyagawa
198
199Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
200
201=head1 COPYRIGHT AND LICENSE
202
000c696e 203Copyright 2009, 2010 Infinity Interactive, Inc.
ac4892f4 204
205L<http://www.iinteractive.com>
206
207This library is free software; you can redistribute it and/or modify
208it under the same terms as Perl itself.
209
210=cut
211
212