use strict;
use warnings;
-our $VERSION = '0.03';
+our $VERSION = '0.10';
our $AUTHORITY = 'cpan:STEVAN';
-use Plack::Util::Accessor qw( id is_new manager );
+use Plack::Util::Accessor qw( session options );
-sub fetch_or_create {
- my($class, $request, $manager) = @_;
-
- my $id = $manager->state->extract($request);
- if ($id) {
- my $store = $manager->store->fetch($id);
- return $class->new( id => $id, _stash => $store, manager => $manager );
- } else {
- $id = $manager->state->generate($request);
- return $class->new( id => $id, _stash => {}, manager => $manager, is_new => 1 );
- }
+sub new {
+ my ($class, $env) = @_;
+ bless {
+ session => $env->{'psgix.session'},
+ options => $env->{'psgix.session.options'},
+ }, $class;
}
-sub new {
- my ($class, %params) = @_;
- bless { %params } => $class;
+sub id {
+ my $self = shift;
+ $self->options->{id};
}
## Data Managment
sub dump {
my $self = shift;
- $self->{_stash};
+ $self->session;
}
sub get {
my ($self, $key) = @_;
- $self->{_stash}{$key};
+ $self->session->{$key};
}
sub set {
my ($self, $key, $value) = @_;
- $self->{_stash}{$key} = $value;
+ delete $self->options->{no_store};
+ $self->session->{$key} = $value;
}
sub remove {
my ($self, $key) = @_;
- delete $self->{_stash}{$key};
+ delete $self->options->{no_store};
+ delete $self->session->{$key};
}
sub keys {
my $self = shift;
- keys %{$self->{_stash}};
+ keys %{$self->session};
}
## Lifecycle Management
sub expire {
my $self = shift;
- $self->{_stash} = {};
- $self->manager->store->cleanup( $self->id );
- $self->manager->state->expire_session_id( $self->id );
-}
-
-sub finalize {
- my ($self, $response) = @_;
- $self->manager->store->store( $self->id, $self );
- $self->manager->state->finalize( $self->id, $response );
+ for my $key ($self->keys) {
+ delete $self->session->{$key};
+ }
+ $self->options->{expire} = 1;
}
1;
=head1 SYNOPSIS
- use Plack::Session;
+ # Use with Middleware::Session
+ enable "Session";
- my $store = Plack::Session::Store->new;
- my $state = Plack::Session::State->new;
+ # later in your app
+ use Plack::Session;
+ my $app = sub {
+ my $env = shift;
+ my $session = Plack::Session->new($env);
- my $s = Plack::Session->new(
- store => $store,
- state => $state,
- request => Plack::Request->new( $env )
- );
+ $session->id;
+ $session->get($key);
+ $session->set($key, $value);
+ $session->remove($key);
+ $session->keys;
- # ...
+ $session->expire;
+ };
=head1 DESCRIPTION
=over 4
-=item B<new ( %params )>
+=item B<new ( $env )>
-The constructor expects keys in C<%params> for I<state>,
-I<store> and I<request>. The I<request> param is expected to be
-a L<Plack::Request> instance or an object with an equivalent
-interface.
+The constructor takes a PSGI request env hash reference.
=item B<id>
This is the accessor for the session id.
-=item B<state>
-
-This is expected to be a L<Plack::Session::State> instance or
-an object with an equivalent interface.
-
-=item B<store>
-
-This is expected to be a L<Plack::Session::Store> 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<finalize> on it.
+Perl's normal hash.
=over 4
=item B<keys>
+=item B<session>, B<dump>
+
=back
=head2 Session Lifecycle Management
=item B<expire>
-This method can be called to expire the current session id. It
-will call the C<cleanup> method on the C<store> and the C<finalize>
-method on the C<state>, passing both of them the session id and
-the C<$response>.
-
-=item B<finalize ( $response )>
-
-This method should be called at the end of the response cycle. It
-will call the C<store> method on the C<store> and the
-C<expire_session_id> method on the C<state>, passing both of them
-the session id. The C<$response> is expected to be a L<Plack::Response>
-instance or an object with an equivalent interface.
+This method can be called to expire the current session id.
=back