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