Checking in changes prior to tagging of version 0.09_02. Changelog diff is:
[catagits/Web-Session.git] / lib / Plack / Middleware / Session.pm
index 2d11bbc..8d9a3ee 100644 (file)
@@ -2,11 +2,9 @@ package Plack::Middleware::Session;
 use strict;
 use warnings;
 
-our $VERSION   = '0.03';
+our $VERSION   = '0.09_02';
 our $AUTHORITY = 'cpan:STEVAN';
 
-use Plack::Request;
-use Plack::Response;
 use Plack::Util;
 use Scalar::Util;
 
@@ -15,7 +13,6 @@ use parent 'Plack::Middleware';
 use Plack::Util::Accessor qw(
     state
     store
-    session_class
 );
 
 sub prepare_app {
@@ -24,8 +21,6 @@ sub prepare_app {
     $self->state( 'Cookie' ) unless $self->state;
     $self->state( $self->inflate_backend('Plack::Session::State', $self->state) );
     $self->store( $self->inflate_backend('Plack::Session::Store', $self->store) );
-
-    Plack::Util::load_class($self->session_class) if $self->session_class;
 }
 
 sub inflate_backend {
@@ -44,77 +39,69 @@ sub call {
     my $self = shift;
     my $env  = shift;
 
-    my $request = Plack::Request->new($env);
-
-    my($id, $session) = $self->get_session($request);
+    my($id, $session) = $self->get_session($env);
     if ($id && $session) {
         $env->{'psgix.session'} = $session;
     } else {
-        $id = $self->generate_id($request);
+        $id = $self->generate_id($env);
         $env->{'psgix.session'} = {};
     }
 
     $env->{'psgix.session.options'} = { id => $id };
 
-    if ($self->session_class) {
-        $env->{'plack.session'} = $self->session_class->new($env);
-    }
-
     my $res = $self->app->($env);
-    $self->response_cb($res, sub {
-        my $res = Plack::Response->new(@{$_[0]});
-        $self->finalize($request, $res);
-        $res = $res->finalize;
-        $_[0]->[0] = $res->[0];
-        $_[0]->[1] = $res->[1];
-    });
+    $self->response_cb($res, sub { $self->finalize($env, $_[0]) });
 }
 
 sub get_session {
-    my($self, $request) = @_;
+    my($self, $env) = @_;
 
-    my $id = $self->state->extract($request) or return;
-    my $session = $self->store->fetch($id)   or return;
+    my $id = $self->state->extract($env)   or return;
+    my $session = $self->store->fetch($id) or return;
 
     return ($id, $session);
 }
 
 sub generate_id {
-    my($self, $request) = @_;
-    $self->state->generate($request);
+    my($self, $env) = @_;
+    $self->state->generate($env);
 }
 
 sub commit {
-    my($self, $session, $options) = @_;
+    my($self, $env) = @_;
+
+    my $session = $env->{'psgix.session'};
+    my $options = $env->{'psgix.session.options'};
+
     if ($options->{expire}) {
-        $self->store->cleanup($options->{id});
+        $self->store->remove($options->{id});
     } else {
         $self->store->store($options->{id}, $session);
     }
 }
 
 sub finalize {
-    my($self, $request, $response) = @_;
+    my($self, $env, $res) = @_;
 
-    my $session = $request->env->{'psgix.session'};
-    my $options = $request->env->{'psgix.session.options'};
+    my $session = $env->{'psgix.session'};
+    my $options = $env->{'psgix.session.options'};
 
-    $self->commit($session, $options) unless $options->{no_store};
+    $self->commit($env) unless $options->{no_store};
     if ($options->{expire}) {
-        $self->expire_session($options->{id}, $response, $session, $options);
+        $self->expire_session($options->{id}, $res, $env);
     } else {
-        $self->save_state($options->{id}, $response, $session, $options);
+        $self->save_state($options->{id}, $res, $env);
     }
 }
 
 sub expire_session {
-    my($self, $id, $res, $session, $options) = @_;
-    $self->state->expire_session_id($options->{id}, $res, $options);
+    my($self, $id, $res, $env) = @_;
+    $self->state->expire_session_id($id, $res, $env->{'psgix.session.options'});
 }
 
 sub save_state {
-    my($self, $id, $res, $session, $options) = @_;
-    $self->state->finalize($id, $res, $options);
+    my($self, $id, $res, $env) = @_;
+    $self->state->finalize($id, $res, $env->{'psgix.session.options'});
 }
 
 1;
@@ -167,9 +154,6 @@ access it as needed.
 B<NOTE:> As of version 0.04 the session is stored in C<psgix.session>
 instead of C<plack.session>.
 
-Also, if you set I<session_class> option (see below), we create a
-session object out of the hash reference in C<plack.session>.
-
 =head2 State
 
 =over 4
@@ -236,13 +220,6 @@ is only suitable for development (or single process servers). For a
 more robust solution see L<Plack::Session::Store::File> or
 L<Plack::Session::Store::Cache>.
 
-=item I<session_class>
-
-This can be used to create an actual session object in
-C<plack.session> environment. Defaults to none, which means the
-session object is not created but you can set C<Plack::Session> to
-create an object for you.
-
 =back
 
 =head1 BUGS