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