Commit | Line | Data |
06190e8b |
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) = @_; |
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 | |
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 { |
bd992981 |
44 | my ($self, $response) = @_; |
45 | $self->store->persist( $self->id, $response ); |
46 | $self->state->finalize( $self->id, $response ); |
06190e8b |
47 | } |
48 | |
ac4892f4 |
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 | |
43f34c01 |
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 | |
ac4892f4 |
76 | =item B<id> |
77 | |
43f34c01 |
78 | This is the accessor for the session id. |
79 | |
ac4892f4 |
80 | =item B<state> |
81 | |
43f34c01 |
82 | This is expected to be a L<Plack::Session::State> instance or |
83 | an object with an equivalent interface. |
84 | |
ac4892f4 |
85 | =item B<store> |
86 | |
43f34c01 |
87 | This is expected to be a L<Plack::Session::Store> instance or |
88 | an object with an equivalent interface. |
89 | |
ac4892f4 |
90 | =back |
91 | |
43f34c01 |
92 | =head2 Session Data Storage |
93 | |
94 | These 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 |
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 | |
ac4892f4 |
117 | =item B<finalize ( $response )> |
118 | |
43f34c01 |
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 | |
ac4892f4 |
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 | |