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