Created
December 29, 2011 00:13
-
-
Save haarg/1530621 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Plack::Middleware::MultiSession; | |
use strict; | |
use warnings; | |
our $VERSION = '0.001'; | |
use Plack::Util; | |
use Plack::Util::Accessor qw( | |
name | |
); | |
use parent 'Plack::Middleware::Session'; | |
sub call { | |
my $self = shift; | |
my $env = shift; | |
my $old_session = delete $env->{'psgix.session'}; | |
my $old_options = delete $env->{'psgix.session.options'}; | |
my $m_sessions = $env->{'psgix.sessions'} ||= {}; | |
my $m_options = $env->{'psgix.sessions.options'} ||= {}; | |
my $app = $self->app; | |
my $wrap_app = sub { | |
my $env = shift; | |
if ($self->name) { | |
$m_sessions->{$self->name} = $env->{'psgix.session'}; | |
$m_options->{$self->name} = $env->{'psgix.session.options'}; | |
} | |
$app->($env); | |
}; | |
$self->{app} = $wrap_app; | |
my $res = $self->SUPER::call($env); | |
$self->response_cb($res, sub { | |
if (defined $old_session) { | |
$env->{'psgix.session'} = $old_session; | |
$env->{'psgix.sessions.options'} = $old_options; | |
} | |
}); | |
} | |
1; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment