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