X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FPlack%2FSession.pm;h=dff6672a658c7605a381443cb6e009096c60bef9;hb=HEAD;hp=b437e292805faf8cdde4b39b1b0e14baec0d4928;hpb=4ff41723d59739434df7ba7293c8c40b968c8600;p=catagits%2FWeb-Session.git diff --git a/lib/Plack/Session.pm b/lib/Plack/Session.pm index b437e29..dff6672 100644 --- a/lib/Plack/Session.pm +++ b/lib/Plack/Session.pm @@ -2,58 +2,63 @@ package Plack::Session; use strict; use warnings; -our $VERSION = '0.03'; +our $VERSION = '0.14'; our $AUTHORITY = 'cpan:STEVAN'; -use Plack::Util::Accessor qw( manager _data options ); +use Plack::Util::Accessor qw( session options ); sub new { - my ($class, %params) = @_; - bless { %params } => $class; + my ($class, $env) = @_; + bless { + session => $env->{'psgix.session'}, + options => $env->{'psgix.session.options'}, + }, $class; +} + +sub id { + my $self = shift; + $self->options->{id}; } ## Data Managment sub dump { my $self = shift; - $self->_data; + $self->session; } sub get { my ($self, $key) = @_; - $self->_data->{$key}; + $self->session->{$key}; } sub set { my ($self, $key, $value) = @_; - delete $self->options->{no_commit}; - $self->_data->{$key} = $value; + delete $self->options->{no_store}; + $self->session->{$key} = $value; } sub remove { my ($self, $key) = @_; - delete $self->options->{no_commit}; - delete $self->_data->{$key}; + delete $self->options->{no_store}; + delete $self->session->{$key}; } sub keys { my $self = shift; - keys %{$self->_data}; + keys %{$self->session}; } ## Lifecycle Management sub expire { my $self = shift; + for my $key ($self->keys) { + delete $self->session->{$key}; + } $self->options->{expire} = 1; } -sub commit { - my $self = shift; - $self->options->{no_commit} = 1; - $self->manager->commit($self->_data, $self->options); -} - 1; __END__ @@ -67,12 +72,22 @@ Plack::Session - Middleware for session management =head1 SYNOPSIS # Use with Middleware::Session - enable "Session", session_class => "Plack::Session"; + enable "Session"; + # later in your app use Plack::Session; + my $app = sub { + my $env = shift; + my $session = Plack::Session->new($env); - my $session = + $session->id; + $session->get($key); + $session->set($key, $value); + $session->remove($key); + $session->keys; + $session->expire; + }; =head1 DESCRIPTION @@ -84,34 +99,20 @@ own session middleware component. =over 4 -=item B +=item B -The constructor expects keys in C<%params> for I, -I and I. The I param is expected to be -a L instance or an object with an equivalent -interface. +The constructor takes a PSGI request env hash reference. =item B This is the accessor for the session id. -=item B - -This is expected to be a L instance or -an object with an equivalent interface. - -=item B - -This is expected to be a L instance or -an object with an equivalent interface. - =back =head2 Session Data Management These methods allows you to read and write the session data like -Perl's normal hash. The operation is not synced to the storage until -you call C on it. +Perl's normal hash. =over 4 @@ -123,29 +124,17 @@ you call C on it. =item B +=item B, B + =back =head2 Session Lifecycle Management =over 4 -=item B - -This method synchronizes the session data to the data store, without -waiting for the response final phase. - =item B -This method can be called to expire the current session id. It marks -the session as expire and call the C method on the C -and the C method on the C. - -=item B - -This method should be called at the end of the response cycle. It will -call the C method on the C and the C -method on the C. The C<$response> is expected to be a -L instance or an object with an equivalent interface. +This method can be called to expire the current session id. =back