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