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