update example to use Plack::Request->session, add docs about where we store session
[catagits/Web-Session.git] / lib / Plack / Middleware / Session.pm
CommitLineData
bd992981 1package Plack::Middleware::Session;
2use strict;
3use warnings;
4
30cc0a71 5our $VERSION = '0.01';
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
d6af4aa8 48 $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]});
726a37c1 57 $env->{'plack.session'}->finalize( $res );
fe1bfe7d 58 @{$_[0]} = @{$res->finalize};
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' ],
81 [ 'Hello, your Session ID is ' . $env->{'plack.session'}->id ]
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
105C<plack.session> key inside the C<$env> where you can access it
106as needed. Additionally, as of version 0.09, you can call the
107C<session> method of a L<Plack::Request> instance to fetch
108whatever is stored in C<plack.session>.
109
3d92cf47 110=head2 State
111
112=over 4
113
114=item L<Plack::Session::State>
115
116This will maintain session state by passing the session through
117the request params. It does not do this automatically though,
118you are responsible for passing the session param.
119
120=item L<Plack::Session::State::Cookie>
121
122This will maintain session state using browser cookies.
123
124=back
125
126=head2 Store
127
128=over 4
129
130=item L<Plack::Session::Store>
131
132This is your basic in-memory session data store. It is volatile storage
133and not recommended for multiprocessing environments. However it is
134very useful for development and testing.
135
136=item L<Plack::Session::Store::File>
137
138This will persist session data in a file. By default it uses
139L<Storable> but it can be configured to have a custom serializer and
140deserializer.
141
20ede533 142=item L<Plack::Session::Store::Cache>
3d92cf47 143
20ede533 144This will persist session data using the L<Cache> interface.
3d92cf47 145
146=item L<Plack::Session::Store::Null>
147
148Sometimes you don't care about storing session data, in that case
149you can use this noop module.
150
151=back
152
30cc0a71 153=head1 OPTIONS
154
155The following are options that can be passed to this mdoule.
156
157=over 4
158
159=item I<state>
160
161This is expected to be an instance of L<Plack::Session::State> or an
162object that implements the same interface. If no option is provided
163the default L<Plack::Session::State::Cookie> will be used.
164
165=item I<store>
166
167This is expected to be an instance of L<Plack::Session::Store> or an
168object that implements the same interface. If no option is provided
169the default L<Plack::Session::Store> will be used.
170
171It should be noted that this default is an in-memory volatile store
172is only suitable for development (or single process servers). For a
173more robust solution see L<Plack::Session::Store::File> or
174L<Plack::Session::Store::Cache>.
175
176=item I<session_class>
177
178This can be used to override the actual session class. It currently
179defaults to L<Plack::Session> but you can substitute any class which
180implements the same interface.
181
182=back
183
ac4892f4 184=head1 BUGS
185
186All complex software has bugs lurking in it, and this module is no
187exception. If you find a bug please either email me, or add the bug
188to cpan-RT.
189
190=head1 AUTHOR
191
192Tatsuhiko Miyagawa
193
194Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
195
196=head1 COPYRIGHT AND LICENSE
197
198Copyright 2009 Infinity Interactive, Inc.
199
200L<http://www.iinteractive.com>
201
202This library is free software; you can redistribute it and/or modify
203it under the same terms as Perl itself.
204
205=cut
206
207