doc cleanup
[catagits/Web-Session.git] / lib / Plack / Middleware / Session.pm
CommitLineData
bd992981 1package Plack::Middleware::Session;
2use strict;
3use warnings;
4
5use Plack::Session;
6use Plack::Request;
7use Plack::Response;
ad80e445 8use Plack::Util;
9use Scalar::Util;
bd992981 10
11use parent 'Plack::Middleware';
12
13use Plack::Util::Accessor qw( state store );
14
fe1bfe7d 15sub prepare_app {
16 my $self = shift;
fe1bfe7d 17
ad80e445 18 $self->state('Cookie') unless $self->state;
19
20 $self->state( $self->inflate_backend('Plack::Session::State', $self->state) );
21 $self->store( $self->inflate_backend('Plack::Session::Store', $self->store) );
22}
23
24sub inflate_backend {
25 my($self, $prefix, $backend) = @_;
26
27 return $backend if defined $backend && Scalar::Util::blessed $backend;
28
29 my @class;
30 push @class, $backend if defined $backend; # undef means the root class
31 push @class, $prefix;
32
33 Plack::Util::load_class(@class)->new();
fe1bfe7d 34}
35
bd992981 36sub call {
37 my $self = shift;
38 my $env = shift;
39
726a37c1 40 $env->{'plack.session'} = Plack::Session->new(
3dbe8dfa 41 state => $self->state,
bd992981 42 store => $self->store,
43 request => Plack::Request->new( $env )
44 );
45
fe1bfe7d 46 my $res = $self->app->($env);
47 $self->response_cb($res, sub {
48 my $res = Plack::Response->new(@{$_[0]});
726a37c1 49 $env->{'plack.session'}->finalize( $res );
fe1bfe7d 50 @{$_[0]} = @{$res->finalize};
51 });
bd992981 52}
53
541;
55
56__END__
ac4892f4 57
58=pod
59
60=head1 NAME
61
62Plack::Middleware::Session - Middleware for session management
63
64=head1 SYNOPSIS
65
3d92cf47 66 use Plack::Builder;
ac4892f4 67
3d92cf47 68 my $app = sub {
69 return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
70 };
71
72 builder {
73 enable 'Session';
74 $app;
75 };
76
ad80e445 77 # Or, use the File store backend (great if you use multiprocess server)
78 # For more options, see perldoc Plack::Session::Store::File
79 builder {
80 enable 'Session', store => 'File';
81 $app;
82 };
83
ac4892f4 84=head1 DESCRIPTION
85
3d92cf47 86This is a Plack Middleware component for session management. By
ad80e445 87default it will use cookies to keep session state and store data in
88memory. This distribution also comes with other state and store
89solutions. See perldoc for these backends how to use them.
3d92cf47 90
91=head2 State
92
93=over 4
94
95=item L<Plack::Session::State>
96
97This will maintain session state by passing the session through
98the request params. It does not do this automatically though,
99you are responsible for passing the session param.
100
101=item L<Plack::Session::State::Cookie>
102
103This will maintain session state using browser cookies.
104
105=back
106
107=head2 Store
108
109=over 4
110
111=item L<Plack::Session::Store>
112
113This is your basic in-memory session data store. It is volatile storage
114and not recommended for multiprocessing environments. However it is
115very useful for development and testing.
116
117=item L<Plack::Session::Store::File>
118
119This will persist session data in a file. By default it uses
120L<Storable> but it can be configured to have a custom serializer and
121deserializer.
122
20ede533 123=item L<Plack::Session::Store::Cache>
3d92cf47 124
20ede533 125This will persist session data using the L<Cache> interface.
3d92cf47 126
127=item L<Plack::Session::Store::Null>
128
129Sometimes you don't care about storing session data, in that case
130you can use this noop module.
131
132=back
133
ac4892f4 134=head1 BUGS
135
136All complex software has bugs lurking in it, and this module is no
137exception. If you find a bug please either email me, or add the bug
138to cpan-RT.
139
140=head1 AUTHOR
141
142Tatsuhiko Miyagawa
143
144Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
145
146=head1 COPYRIGHT AND LICENSE
147
148Copyright 2009 Infinity Interactive, Inc.
149
150L<http://www.iinteractive.com>
151
152This library is free software; you can redistribute it and/or modify
153it under the same terms as Perl itself.
154
155=cut
156
157