From: Tatsuhiko Miyagawa Date: Sat, 9 Jan 2010 20:32:45 +0000 (-0800) Subject: Pass $env in more Middleware APIs so subclasses can do more things flexibly in the... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FWeb-Session.git;a=commitdiff_plain;h=7518e927e0b76465fb24288dd0c39a0592e16e6a Pass $env in more Middleware APIs so subclasses can do more things flexibly in the runtime. --- diff --git a/lib/Plack/Middleware/Session.pm b/lib/Plack/Middleware/Session.pm index 1dbe441..d022f70 100644 --- a/lib/Plack/Middleware/Session.pm +++ b/lib/Plack/Middleware/Session.pm @@ -63,7 +63,7 @@ sub call { my $res = $self->app->($env); $self->response_cb($res, sub { my $res = Plack::Response->new(@{$_[0]}); - $self->finalize($request, $res); + $self->finalize($env, $res); $res = $res->finalize; $_[0]->[0] = $res->[0]; $_[0]->[1] = $res->[1]; @@ -85,7 +85,11 @@ sub generate_id { } 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->remove($options->{id}); } else { @@ -94,27 +98,27 @@ sub commit { } sub finalize { - my($self, $request, $response) = @_; + my($self, $env, $response) = @_; - 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}, $response, $env); } else { - $self->save_state($options->{id}, $response, $session, $options); + $self->save_state($options->{id}, $response, $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; diff --git a/lib/Plack/Middleware/Session/Cookie.pm b/lib/Plack/Middleware/Session/Cookie.pm index 3351d1d..3356352 100644 --- a/lib/Plack/Middleware/Session/Cookie.pm +++ b/lib/Plack/Middleware/Session/Cookie.pm @@ -44,10 +44,10 @@ sub generate_id { sub commit { } sub save_state { - my($self, $id, $res, $session, $options) = @_; + my($self, $id, $res, $env) = @_; - my $cookie = $self->_serialize($id, $session); - $self->state->finalize($cookie, $res, $options); + my $cookie = $self->_serialize($id, $env->{'psgix.session'}); + $self->state->finalize($cookie, $res, $env->{'psgix.session.options'}); } sub _serialize { diff --git a/lib/Plack/Session.pm b/lib/Plack/Session.pm index b86c625..e6a1c91 100644 --- a/lib/Plack/Session.pm +++ b/lib/Plack/Session.pm @@ -9,6 +9,8 @@ use Plack::Util::Accessor qw( session options ); sub new { my ($class, $env) = @_; + # NOTE: when you make a subclass, be sure to NEVER save $env in + # your hash. That will create a circular reference. bless { session => $env->{'psgix.session'}, options => $env->{'psgix.session.options'}, @@ -110,8 +112,7 @@ This is the accessor for the session id. =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