Huge rewrite of the code: $session->get, ->set etc. do not read from
[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
4a0cb5a0 8use Plack::Util::Accessor qw( id is_new manager );
9
10sub fetch_or_create {
11 my($class, $request, $manager) = @_;
12
13 my $id = $manager->state->extract($request);
14 if ($id) {
15 my $store = $manager->store->fetch($id);
16 return $class->new( id => $id, _stash => $store, manager => $manager );
17 } else {
18 $id = $manager->state->generate($request);
19 return $class->new( id => $id, _stash => {}, manager => $manager, is_new => 1 );
20 }
21}
06190e8b 22
23sub new {
24 my ($class, %params) = @_;
ac4892f4 25 bless { %params } => $class;
06190e8b 26}
27
28## Data Managment
29
4a0cb5a0 30sub dump {
31 my $self = shift;
32 $self->{_stash};
33}
34
06190e8b 35sub get {
36 my ($self, $key) = @_;
4a0cb5a0 37 $self->{_stash}{$key};
06190e8b 38}
39
40sub set {
41 my ($self, $key, $value) = @_;
4a0cb5a0 42 $self->{_stash}{$key} = $value;
06190e8b 43}
44
45sub remove {
46 my ($self, $key) = @_;
4a0cb5a0 47 delete $self->{_stash}{$key};
48}
49
50sub keys {
51 my $self = shift;
52 keys %{$self->{_stash}};
06190e8b 53}
54
55## Lifecycle Management
56
57sub expire {
58 my $self = shift;
4a0cb5a0 59 $self->{_stash} = {};
60 $self->manager->store->cleanup( $self->id );
61 $self->manager->state->expire_session_id( $self->id );
06190e8b 62}
63
64sub finalize {
bd992981 65 my ($self, $response) = @_;
4a0cb5a0 66 $self->manager->store->store( $self->id, $self );
67 $self->manager->state->finalize( $self->id, $response );
06190e8b 68}
69
ac4892f4 701;
71
72__END__
73
74=pod
75
76=head1 NAME
77
78Plack::Session - Middleware for session management
79
80=head1 SYNOPSIS
81
82 use Plack::Session;
83
3d92cf47 84 my $store = Plack::Session::Store->new;
85 my $state = Plack::Session::State->new;
86
87 my $s = Plack::Session->new(
88 store => $store,
89 state => $state,
90 request => Plack::Request->new( $env )
91 );
92
93 # ...
94
ac4892f4 95=head1 DESCRIPTION
96
3d92cf47 97This is the core session object, you probably want to look
d5abacda 98at L<Plack::Middleware::Session>, unless you are writing your
3d92cf47 99own session middleware component.
100
ac4892f4 101=head1 METHODS
102
103=over 4
104
105=item B<new ( %params )>
106
43f34c01 107The constructor expects keys in C<%params> for I<state>,
108I<store> and I<request>. The I<request> param is expected to be
109a L<Plack::Request> instance or an object with an equivalent
110interface.
111
ac4892f4 112=item B<id>
113
43f34c01 114This is the accessor for the session id.
115
ac4892f4 116=item B<state>
117
43f34c01 118This is expected to be a L<Plack::Session::State> instance or
119an object with an equivalent interface.
120
ac4892f4 121=item B<store>
122
43f34c01 123This is expected to be a L<Plack::Session::Store> instance or
124an object with an equivalent interface.
125
ac4892f4 126=back
127
4a0cb5a0 128=head2 Session Data Management
43f34c01 129
4a0cb5a0 130These methods allows you to read and write the session data like
131Perl's normal hash. The operation is not synced to the storage until
132you call C<finalize> on it.
43f34c01 133
ac4892f4 134=over 4
135
136=item B<get ( $key )>
137
138=item B<set ( $key, $value )>
139
140=item B<remove ( $key )>
141
4a0cb5a0 142=item B<keys>
143
ac4892f4 144=back
145
43f34c01 146=head2 Session Lifecycle Management
147
ac4892f4 148=over 4
149
150=item B<expire>
151
43f34c01 152This method can be called to expire the current session id. It
153will call the C<cleanup> method on the C<store> and the C<finalize>
154method on the C<state>, passing both of them the session id and
155the C<$response>.
156
ac4892f4 157=item B<finalize ( $response )>
158
43f34c01 159This method should be called at the end of the response cycle. It
4a0cb5a0 160will call the C<store> method on the C<store> and the
43f34c01 161C<expire_session_id> method on the C<state>, passing both of them
162the session id. The C<$response> is expected to be a L<Plack::Response>
163instance or an object with an equivalent interface.
164
ac4892f4 165=back
166
167=head1 BUGS
168
169All complex software has bugs lurking in it, and this module is no
170exception. If you find a bug please either email me, or add the bug
171to cpan-RT.
172
173=head1 AUTHOR
174
175Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
176
177=head1 COPYRIGHT AND LICENSE
178
000c696e 179Copyright 2009, 2010 Infinity Interactive, Inc.
ac4892f4 180
181L<http://www.iinteractive.com>
182
183This library is free software; you can redistribute it and/or modify
184it under the same terms as Perl itself.
185
186=cut
187