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