X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst%2FMiddleware%2FStash.pm;h=0ed488dcc151966e52127e3287435232b5c3b7dc;hp=6004d7f308329f82c502047219cafeca6206d865;hb=5e7e6b272a9e1b8bf1c401dd3a607fc851c43cc7;hpb=02c4d5985fe1c908c75639a02a469136bcd9851e diff --git a/lib/Catalyst/Middleware/Stash.pm b/lib/Catalyst/Middleware/Stash.pm index 6004d7f..0ed488d 100644 --- a/lib/Catalyst/Middleware/Stash.pm +++ b/lib/Catalyst/Middleware/Stash.pm @@ -5,23 +5,33 @@ package Catalyst::Middleware::Stash; use base 'Plack::Middleware'; use Exporter 'import'; -use Scalar::Util 'blessed'; use Carp 'croak'; our @EXPORT_OK = qw(stash get_stash); -sub PSGI_KEY { 'Catalyst.Stash.v1' }; +sub PSGI_KEY () { 'Catalyst.Stash.v2' } -sub get_stash { return shift->{PSGI_KEY} } +sub get_stash { + my $env = shift; + return $env->{+PSGI_KEY} || + croak "You requested a stash, but one does not exist."; +} + +sub stash { + my ($host, @args) = @_; + return get_stash($host->env) + ->(@args); +} -sub generate_stash_closure { +sub _create_stash { + my $self = shift; my $stash = shift || +{}; return sub { if(@_) { my $new_stash = @_ > 1 ? {@_} : $_[0]; croak('stash takes a hash or hashref') unless ref $new_stash; - foreach my $key ( keys %$new_stash ) { + foreach my $key (keys %$new_stash) { $stash->{$key} = $new_stash->{$key}; } } @@ -29,24 +39,15 @@ sub generate_stash_closure { }; } -sub _init_stash { - my ($self, $env) = @_; - return $env->{PSGI_KEY} ||= - generate_stash_closure; -} - -sub stash { - my ($host, @args) = @_; - return get_stash($host->env)->(@args); -} - sub call { my ($self, $env) = @_; - $self->_init_stash($env); + $env->{+PSGI_KEY} = $self->_create_stash + unless exists($env->{+PSGI_KEY}); + return $self->app->($env); } -=head1 TITLE +=head1 NAME Catalyst::Middleware::Stash - The Catalyst stash - in middleware @@ -57,7 +58,18 @@ directly since it is likely to move off the Catalyst namespace into a stand alone distribution We store a coderef under the C which can be dereferenced with -key values or nothing to access the underly hashref. +key values or nothing to access the underlying hashref. + +Anything placed into the stash will be available in the stash of any 'mounted' +Catalyst applictions. A mounted Catalyst application may set the stash and +'pass back' information to the parent application. Non Catalyst applications +may use this middleware to access and set stash values. + +Please note I highly recommend having a stronger interface than a stash key +between applications. + +For more information the current test case t/middleware-stash.t is the best +documentation. =head1 SUBROUTINES @@ -65,14 +77,22 @@ This class defines the following subroutines. =head2 PSGI_KEY -Returns the hash key where we store the stash +Returns the hash key where we store the stash. You should not assume +the string value here will never change! Also, its better to use +L or L. =head2 get_stash -Get the stash out of the C<$env> +Expect: $psgi_env. + +Exportable subroutine. + +Get the stash out of the C<$env>. =head2 stash +Expects: An object that does C and arguments + Exportable subroutine. Given an object with a method C get or set stash values, either @@ -80,15 +100,19 @@ as a method or via hashref modification. This stash is automatically reset for each request (it is not persistent or shared across connected clients. Stash key / value are stored in memory. - Catalyst::Middleware::Stash 'stash'; + use Plack::Request; + use Catalyst::Middleware::Stash 'stash'; - $c->stash->{foo} = $bar; - $c->stash( { moose => 'majestic', qux => 0 } ); - $c->stash( bar => 1, gorch => 2 ); # equivalent to passing a hashref + my $app = sub { + my $env = shift; + my $req = Plack::Request->new($env); + my $stashed = $req->stash->{in_the_stash}; # Assume the stash was previously populated. -=head2 generate_stash_closure + return [200, ['Content-Type' => 'text/plain'], + ["I found $stashed in the stash!"]]; + }; -Creates the closure which is stored in the L environment. +If the stash does not yet exist, an exception is thrown. =head1 METHODS