inflate store/state with strings like "store => 'File'" with autoloading.
[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
123=item L<Plack::Session::Store::CHI>
124
125This will persist session data using the L<CHI> module. This
126offers a lot of flexibility due to the many excellent L<CHI>
127drivers available.
128
129=item L<Plack::Session::Store::Null>
130
131Sometimes you don't care about storing session data, in that case
132you can use this noop module.
133
134=back
135
ac4892f4 136=head1 BUGS
137
138All complex software has bugs lurking in it, and this module is no
139exception. If you find a bug please either email me, or add the bug
140to cpan-RT.
141
142=head1 AUTHOR
143
144Tatsuhiko Miyagawa
145
146Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
147
148=head1 COPYRIGHT AND LICENSE
149
150Copyright 2009 Infinity Interactive, Inc.
151
152L<http://www.iinteractive.com>
153
154This library is free software; you can redistribute it and/or modify
155it under the same terms as Perl itself.
156
157=cut
158
159