d59da4dda28378724d885309f8ca0f9dad7a7d3e
[catagits/Web-Session.git] / lib / Plack / Session.pm
1 package Plack::Session;
2 use strict;
3 use warnings;
4
5 our $VERSION   = '0.09_02';
6 our $AUTHORITY = 'cpan:STEVAN';
7
8 use Plack::Util::Accessor qw( session options );
9
10 sub new {
11     my ($class, $env) = @_;
12     bless {
13         session => $env->{'psgix.session'},
14         options => $env->{'psgix.session.options'},
15     }, $class;
16 }
17
18 sub id {
19     my $self = shift;
20     $self->options->{id};
21 }
22
23 ## Data Managment
24
25 sub dump {
26     my $self = shift;
27     $self->session;
28 }
29
30 sub get {
31     my ($self, $key) = @_;
32     $self->session->{$key};
33 }
34
35 sub set {
36     my ($self, $key, $value) = @_;
37     delete $self->options->{no_store};
38     $self->session->{$key} = $value;
39 }
40
41 sub remove {
42     my ($self, $key) = @_;
43     delete $self->options->{no_store};
44     delete $self->session->{$key};
45 }
46
47 sub keys {
48     my $self = shift;
49     keys %{$self->session};
50 }
51
52 ## Lifecycle Management
53
54 sub expire {
55     my $self = shift;
56     for my $key ($self->keys) {
57         delete $self->session->{$key};
58     }
59     $self->options->{expire} = 1;
60 }
61
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
74   # Use with Middleware::Session
75   enable "Session";
76
77   # later in your app
78   use Plack::Session;
79   my $app = sub {
80       my $env = shift;
81       my $session = Plack::Session->new($env);
82
83       $session->id;
84       $session->get($key);
85       $session->set($key, $value);
86       $session->remove($key);
87       $session->keys;
88
89       $session->expire;
90   };
91
92 =head1 DESCRIPTION
93
94 This is the core session object, you probably want to look
95 at L<Plack::Middleware::Session>, unless you are writing your
96 own session middleware component.
97
98 =head1 METHODS
99
100 =over 4
101
102 =item B<new ( $env )>
103
104 The constructor takes a PSGI request env hash reference.
105
106 =item B<id>
107
108 This is the accessor for the session id.
109
110 =back
111
112 =head2 Session Data Management
113
114 These methods allows you to read and write the session data like
115 Perl's normal hash.
116
117 =over 4
118
119 =item B<get ( $key )>
120
121 =item B<set ( $key, $value )>
122
123 =item B<remove ( $key )>
124
125 =item B<keys>
126
127 =item B<session>, B<dump>
128
129 =back
130
131 =head2 Session Lifecycle Management
132
133 =over 4
134
135 =item B<expire>
136
137 This method can be called to expire the current session id.
138
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
153 Copyright 2009, 2010 Infinity Interactive, Inc.
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