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 | |
3bf03fd8 |
8 | use Plack::Util::Accessor qw( session options ); |
06190e8b |
9 | |
10 | sub new { |
3bf03fd8 |
11 | my ($class, $env) = @_; |
399edae6 |
12 | bless { |
399edae6 |
13 | session => $env->{'psgix.session'}, |
14 | options => $env->{'psgix.session.options'}, |
15 | }, $class; |
06190e8b |
16 | } |
17 | |
85265792 |
18 | sub id { |
19 | my $self = shift; |
20 | $self->options->{id}; |
21 | } |
22 | |
06190e8b |
23 | ## Data Managment |
24 | |
4a0cb5a0 |
25 | sub dump { |
26 | my $self = shift; |
399edae6 |
27 | $self->session; |
4a0cb5a0 |
28 | } |
29 | |
06190e8b |
30 | sub get { |
31 | my ($self, $key) = @_; |
399edae6 |
32 | $self->session->{$key}; |
06190e8b |
33 | } |
34 | |
35 | sub set { |
36 | my ($self, $key, $value) = @_; |
399edae6 |
37 | delete $self->options->{no_store}; |
38 | $self->session->{$key} = $value; |
06190e8b |
39 | } |
40 | |
41 | sub remove { |
42 | my ($self, $key) = @_; |
399edae6 |
43 | delete $self->options->{no_store}; |
44 | delete $self->session->{$key}; |
4a0cb5a0 |
45 | } |
46 | |
47 | sub keys { |
48 | my $self = shift; |
399edae6 |
49 | keys %{$self->session}; |
06190e8b |
50 | } |
51 | |
52 | ## Lifecycle Management |
53 | |
54 | sub expire { |
55 | my $self = shift; |
85265792 |
56 | for my $key ($self->keys) { |
399edae6 |
57 | delete $self->session->{$key}; |
85265792 |
58 | } |
4ff41723 |
59 | $self->options->{expire} = 1; |
caf3bd90 |
60 | } |
61 | |
ac4892f4 |
62 | 1; |
63 | |
64 | __END__ |
65 | |
66 | =pod |
67 | |
68 | =head1 NAME |
69 | |
70 | Plack::Session - Middleware for session management |
71 | |
72 | =head1 SYNOPSIS |
73 | |
4ff41723 |
74 | # Use with Middleware::Session |
75 | enable "Session", session_class => "Plack::Session"; |
ac4892f4 |
76 | |
399edae6 |
77 | my $app = sub { |
78 | my $env = shift; |
79 | my $session = $env->{'plack.session'}; # not psgix. |
3d92cf47 |
80 | |
399edae6 |
81 | $session->id; |
82 | $session->get($key); |
83 | $session->set($key, $value); |
84 | $session->remove($key); |
85 | $session->keys; |
3d92cf47 |
86 | |
399edae6 |
87 | $session->expire; |
399edae6 |
88 | }; |
3d92cf47 |
89 | |
ac4892f4 |
90 | =head1 DESCRIPTION |
91 | |
3d92cf47 |
92 | This is the core session object, you probably want to look |
d5abacda |
93 | at L<Plack::Middleware::Session>, unless you are writing your |
3d92cf47 |
94 | own session middleware component. |
95 | |
ac4892f4 |
96 | =head1 METHODS |
97 | |
98 | =over 4 |
99 | |
3bf03fd8 |
100 | =item B<new ( $env )> |
ac4892f4 |
101 | |
3bf03fd8 |
102 | The constructor takes a PSGI request env hash reference. |
43f34c01 |
103 | |
ac4892f4 |
104 | =item B<id> |
105 | |
43f34c01 |
106 | This is the accessor for the session id. |
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 | |
399edae6 |
126 | =item B<session>, B<dump> |
127 | |
ac4892f4 |
128 | =back |
129 | |
43f34c01 |
130 | =head2 Session Lifecycle Management |
131 | |
ac4892f4 |
132 | =over 4 |
133 | |
134 | =item B<expire> |
135 | |
399edae6 |
136 | This method can be called to expire the current session id. |
43f34c01 |
137 | |
ac4892f4 |
138 | =back |
139 | |
140 | =head1 BUGS |
141 | |
142 | All complex software has bugs lurking in it, and this module is no |
143 | exception. If you find a bug please either email me, or add the bug |
144 | to cpan-RT. |
145 | |
146 | =head1 AUTHOR |
147 | |
148 | Stevan Little E<lt>stevan.little@iinteractive.comE<gt> |
149 | |
150 | =head1 COPYRIGHT AND LICENSE |
151 | |
000c696e |
152 | Copyright 2009, 2010 Infinity Interactive, Inc. |
ac4892f4 |
153 | |
154 | L<http://www.iinteractive.com> |
155 | |
156 | This library is free software; you can redistribute it and/or modify |
157 | it under the same terms as Perl itself. |
158 | |
159 | =cut |
160 | |