0.03 release
[catagits/Web-Session.git] / lib / Plack / Session.pm
CommitLineData
06190e8b 1package Plack::Session;
2use strict;
3use warnings;
4
000c696e 5our $VERSION = '0.03';
30cc0a71 6our $AUTHORITY = 'cpan:STEVAN';
7
06190e8b 8use Plack::Util::Accessor qw[
9 id
10 store
11 state
12];
13
14sub new {
15 my ($class, %params) = @_;
ac4892f4 16 my $request = delete $params{'request'};
17 $params{'id'} = $params{'state'}->get_session_id( $request );
18 bless { %params } => $class;
06190e8b 19}
20
21## Data Managment
22
23sub get {
24 my ($self, $key) = @_;
25 $self->store->fetch( $self->id, $key )
26}
27
28sub set {
29 my ($self, $key, $value) = @_;
30 $self->store->store( $self->id, $key, $value );
31}
32
33sub remove {
34 my ($self, $key) = @_;
35 $self->store->delete( $self->id, $key );
36}
37
38## Lifecycle Management
39
40sub expire {
41 my $self = shift;
42 $self->store->cleanup( $self->id );
43 $self->state->expire_session_id( $self->id );
44}
45
46sub finalize {
bd992981 47 my ($self, $response) = @_;
48 $self->store->persist( $self->id, $response );
49 $self->state->finalize( $self->id, $response );
06190e8b 50}
51
ac4892f4 521;
53
54__END__
55
56=pod
57
58=head1 NAME
59
60Plack::Session - Middleware for session management
61
62=head1 SYNOPSIS
63
64 use Plack::Session;
65
3d92cf47 66 my $store = Plack::Session::Store->new;
67 my $state = Plack::Session::State->new;
68
69 my $s = Plack::Session->new(
70 store => $store,
71 state => $state,
72 request => Plack::Request->new( $env )
73 );
74
75 # ...
76
ac4892f4 77=head1 DESCRIPTION
78
3d92cf47 79This is the core session object, you probably want to look
d5abacda 80at L<Plack::Middleware::Session>, unless you are writing your
3d92cf47 81own session middleware component.
82
ac4892f4 83=head1 METHODS
84
85=over 4
86
87=item B<new ( %params )>
88
43f34c01 89The constructor expects keys in C<%params> for I<state>,
90I<store> and I<request>. The I<request> param is expected to be
91a L<Plack::Request> instance or an object with an equivalent
92interface.
93
ac4892f4 94=item B<id>
95
43f34c01 96This is the accessor for the session id.
97
ac4892f4 98=item B<state>
99
43f34c01 100This is expected to be a L<Plack::Session::State> instance or
101an object with an equivalent interface.
102
ac4892f4 103=item B<store>
104
43f34c01 105This is expected to be a L<Plack::Session::Store> instance or
106an object with an equivalent interface.
107
ac4892f4 108=back
109
43f34c01 110=head2 Session Data Storage
111
3d92cf47 112These methods delegate to appropriate methods on the C<store>
113to manage your session data.
43f34c01 114
ac4892f4 115=over 4
116
117=item B<get ( $key )>
118
119=item B<set ( $key, $value )>
120
121=item B<remove ( $key )>
122
123=back
124
43f34c01 125=head2 Session Lifecycle Management
126
ac4892f4 127=over 4
128
129=item B<expire>
130
43f34c01 131This method can be called to expire the current session id. It
132will call the C<cleanup> method on the C<store> and the C<finalize>
133method on the C<state>, passing both of them the session id and
134the C<$response>.
135
ac4892f4 136=item B<finalize ( $response )>
137
43f34c01 138This method should be called at the end of the response cycle. It
139will call the C<persist> method on the C<store> and the
140C<expire_session_id> method on the C<state>, passing both of them
141the session id. The C<$response> is expected to be a L<Plack::Response>
142instance or an object with an equivalent interface.
143
ac4892f4 144=back
145
146=head1 BUGS
147
148All complex software has bugs lurking in it, and this module is no
149exception. If you find a bug please either email me, or add the bug
150to cpan-RT.
151
152=head1 AUTHOR
153
154Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
155
156=head1 COPYRIGHT AND LICENSE
157
000c696e 158Copyright 2009, 2010 Infinity Interactive, Inc.
ac4892f4 159
160L<http://www.iinteractive.com>
161
162This library is free software; you can redistribute it and/or modify
163it under the same terms as Perl itself.
164
165=cut
166