Checking in changes prior to tagging of version 0.10. Changelog diff is:
[catagits/Web-Session.git] / lib / Plack / Session.pm
CommitLineData
06190e8b 1package Plack::Session;
2use strict;
3use warnings;
4
69c28b0e 5our $VERSION = '0.10';
30cc0a71 6our $AUTHORITY = 'cpan:STEVAN';
7
3bf03fd8 8use Plack::Util::Accessor qw( session options );
06190e8b 9
10sub 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 18sub id {
19 my $self = shift;
20 $self->options->{id};
21}
22
06190e8b 23## Data Managment
24
4a0cb5a0 25sub dump {
26 my $self = shift;
399edae6 27 $self->session;
4a0cb5a0 28}
29
06190e8b 30sub get {
31 my ($self, $key) = @_;
399edae6 32 $self->session->{$key};
06190e8b 33}
34
35sub set {
36 my ($self, $key, $value) = @_;
399edae6 37 delete $self->options->{no_store};
38 $self->session->{$key} = $value;
06190e8b 39}
40
41sub remove {
42 my ($self, $key) = @_;
399edae6 43 delete $self->options->{no_store};
44 delete $self->session->{$key};
4a0cb5a0 45}
46
47sub keys {
48 my $self = shift;
399edae6 49 keys %{$self->session};
06190e8b 50}
51
52## Lifecycle Management
53
54sub 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 621;
63
64__END__
65
66=pod
67
68=head1 NAME
69
70Plack::Session - Middleware for session management
71
72=head1 SYNOPSIS
73
4ff41723 74 # Use with Middleware::Session
c6a7260c 75 enable "Session";
ac4892f4 76
c6a7260c 77 # later in your app
78 use Plack::Session;
399edae6 79 my $app = sub {
80 my $env = shift;
c6a7260c 81 my $session = Plack::Session->new($env);
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 94This is the core session object, you probably want to look
d5abacda 95at L<Plack::Middleware::Session>, unless you are writing your
3d92cf47 96own session middleware component.
97
ac4892f4 98=head1 METHODS
99
100=over 4
101
3bf03fd8 102=item B<new ( $env )>
ac4892f4 103
3bf03fd8 104The constructor takes a PSGI request env hash reference.
43f34c01 105
ac4892f4 106=item B<id>
107
43f34c01 108This is the accessor for the session id.
109
ac4892f4 110=back
111
4a0cb5a0 112=head2 Session Data Management
43f34c01 113
4a0cb5a0 114These methods allows you to read and write the session data like
7518e927 115Perl'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 137This method can be called to expire the current session id.
43f34c01 138
ac4892f4 139=back
140
141=head1 BUGS
142
143All complex software has bugs lurking in it, and this module is no
144exception. If you find a bug please either email me, or add the bug
145to cpan-RT.
146
147=head1 AUTHOR
148
149Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
150
151=head1 COPYRIGHT AND LICENSE
152
000c696e 153Copyright 2009, 2010 Infinity Interactive, Inc.
ac4892f4 154
155L<http://www.iinteractive.com>
156
157This library is free software; you can redistribute it and/or modify
158it under the same terms as Perl itself.
159
160=cut
161