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