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