aff0685573dd5408eb1c663947a1a5fef7096e30
[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::Util;
9 use Scalar::Util;
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
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
24 sub 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();
34 }
35
36 sub call {
37     my $self = shift;
38     my $env  = shift;
39
40     $env->{'plack.session'} = Plack::Session->new(
41         state   => $self->state,
42         store   => $self->store,
43         request => Plack::Request->new( $env )
44     );
45
46     my $res = $self->app->($env);
47     $self->response_cb($res, sub {
48         my $res = Plack::Response->new(@{$_[0]});
49         $env->{'plack.session'}->finalize( $res );
50         @{$_[0]} = @{$res->finalize};
51     });
52 }
53
54 1;
55
56 __END__
57
58 =pod
59
60 =head1 NAME
61
62 Plack::Middleware::Session - Middleware for session management
63
64 =head1 SYNOPSIS
65
66   use Plack::Builder;
67
68   my $app = sub {
69       return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
70   };
71
72   builder {
73       enable 'Session';
74       $app;
75   };
76
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
84 =head1 DESCRIPTION
85
86 This is a Plack Middleware component for session management. By
87 default it will use cookies to keep session state and store data in
88 memory. This distribution also comes with other state and store
89 solutions. See perldoc for these backends how to use them.
90
91 =head2 State
92
93 =over 4
94
95 =item L<Plack::Session::State>
96
97 This will maintain session state by passing the session through
98 the request params. It does not do this automatically though,
99 you are responsible for passing the session param.
100
101 =item L<Plack::Session::State::Cookie>
102
103 This 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
113 This is your basic in-memory session data store. It is volatile storage
114 and not recommended for multiprocessing environments. However it is
115 very useful for development and testing.
116
117 =item L<Plack::Session::Store::File>
118
119 This will persist session data in a file. By default it uses
120 L<Storable> but it can be configured to have a custom serializer and
121 deserializer.
122
123 =item L<Plack::Session::Store::Cache>
124
125 This will persist session data using the L<Cache> interface.
126
127 =item L<Plack::Session::Store::Null>
128
129 Sometimes you don't care about storing session data, in that case
130 you can use this noop module.
131
132 =back
133
134 =head1 BUGS
135
136 All complex software has bugs lurking in it, and this module is no
137 exception. If you find a bug please either email me, or add the bug
138 to cpan-RT.
139
140 =head1 AUTHOR
141
142 Tatsuhiko Miyagawa
143
144 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
145
146 =head1 COPYRIGHT AND LICENSE
147
148 Copyright 2009 Infinity Interactive, Inc.
149
150 L<http://www.iinteractive.com>
151
152 This library is free software; you can redistribute it and/or modify
153 it under the same terms as Perl itself.
154
155 =cut
156
157