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