Make psgix.session a hash reference and plack.session an (optional)
[catagits/Web-Session.git] / lib / Plack / Session.pm
CommitLineData
06190e8b 1package Plack::Session;
2use strict;
3use warnings;
4
000c696e 5our $VERSION = '0.03';
30cc0a71 6our $AUTHORITY = 'cpan:STEVAN';
7
4ff41723 8use Plack::Util::Accessor qw( manager _data options );
06190e8b 9
10sub new {
11 my ($class, %params) = @_;
ac4892f4 12 bless { %params } => $class;
06190e8b 13}
14
15## Data Managment
16
4a0cb5a0 17sub dump {
18 my $self = shift;
4ff41723 19 $self->_data;
4a0cb5a0 20}
21
06190e8b 22sub get {
23 my ($self, $key) = @_;
4ff41723 24 $self->_data->{$key};
06190e8b 25}
26
27sub set {
28 my ($self, $key, $value) = @_;
4ff41723 29 delete $self->options->{no_commit};
30 $self->_data->{$key} = $value;
06190e8b 31}
32
33sub remove {
34 my ($self, $key) = @_;
4ff41723 35 delete $self->options->{no_commit};
36 delete $self->_data->{$key};
4a0cb5a0 37}
38
39sub keys {
40 my $self = shift;
4ff41723 41 keys %{$self->_data};
06190e8b 42}
43
44## Lifecycle Management
45
46sub expire {
47 my $self = shift;
4ff41723 48 $self->options->{expire} = 1;
caf3bd90 49}
50
51sub commit {
52 my $self = shift;
4ff41723 53 $self->options->{no_commit} = 1;
54 $self->manager->commit($self->_data, $self->options);
06190e8b 55}
56
ac4892f4 571;
58
59__END__
60
61=pod
62
63=head1 NAME
64
65Plack::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 79This is the core session object, you probably want to look
d5abacda 80at L<Plack::Middleware::Session>, unless you are writing your
3d92cf47 81own session middleware component.
82
ac4892f4 83=head1 METHODS
84
85=over 4
86
87=item B<new ( %params )>
88
43f34c01 89The constructor expects keys in C<%params> for I<state>,
90I<store> and I<request>. The I<request> param is expected to be
91a L<Plack::Request> instance or an object with an equivalent
92interface.
93
ac4892f4 94=item B<id>
95
43f34c01 96This is the accessor for the session id.
97
ac4892f4 98=item B<state>
99
43f34c01 100This is expected to be a L<Plack::Session::State> instance or
101an object with an equivalent interface.
102
ac4892f4 103=item B<store>
104
43f34c01 105This is expected to be a L<Plack::Session::Store> instance or
106an object with an equivalent interface.
107
ac4892f4 108=back
109
4a0cb5a0 110=head2 Session Data Management
43f34c01 111
4a0cb5a0 112These methods allows you to read and write the session data like
113Perl's normal hash. The operation is not synced to the storage until
114you 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
134This method synchronizes the session data to the data store, without
135waiting for the response final phase.
136
ac4892f4 137=item B<expire>
138
caf3bd90 139This method can be called to expire the current session id. It marks
140the session as expire and call the C<cleanup> method on the C<store>
141and the C<expire_session_id> method on the C<state>.
43f34c01 142
caf3bd90 143=item B<finalize ( $manager, $response )>
ac4892f4 144
caf3bd90 145This method should be called at the end of the response cycle. It will
146call the C<store> method on the C<store> and the C<expire_session_id>
147method on the C<state>. The C<$response> is expected to be a
148L<Plack::Response> instance or an object with an equivalent interface.
43f34c01 149
ac4892f4 150=back
151
152=head1 BUGS
153
154All complex software has bugs lurking in it, and this module is no
155exception. If you find a bug please either email me, or add the bug
156to cpan-RT.
157
158=head1 AUTHOR
159
160Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
161
162=head1 COPYRIGHT AND LICENSE
163
000c696e 164Copyright 2009, 2010 Infinity Interactive, Inc.
ac4892f4 165
166L<http://www.iinteractive.com>
167
168This library is free software; you can redistribute it and/or modify
169it under the same terms as Perl itself.
170
171=cut
172