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=4682da8327471b7110770071234465f77743deb9;hp=7102089c5eb7f381d6009ebf0749aa5d9c297a6e;hb=566678d0245e49d7f2f1abce553b5bdb87879086;hpb=300b04ce8f2591a09a4d43060dbb84c4f799131a diff --git a/lib/Catalyst/Middleware/Stash.pm b/lib/Catalyst/Middleware/Stash.pm index 7102089..4682da8 100644 --- a/lib/Catalyst/Middleware/Stash.pm +++ b/lib/Catalyst/Middleware/Stash.pm @@ -9,12 +9,12 @@ use Carp 'croak'; our @EXPORT_OK = qw(stash get_stash); -sub PSGI_KEY { 'Catalyst.Stash.v1' }; +sub PSGI_KEY () { 'Catalyst.Stash.v1' } sub get_stash { my $env = shift; - return $env->{PSGI_KEY} || - _init_stash($env); + return $env->{+PSGI_KEY} || + croak "You requested a stash, but one does not exist."; } sub stash { @@ -23,7 +23,7 @@ sub stash { ->(@args); } -sub _generate_stash_closure { +sub _create_stash { my $stash = shift || +{}; return sub { if(@_) { @@ -38,19 +38,16 @@ sub _generate_stash_closure { }; } -sub _init_stash { - my ($env) = @_; - return $env->{PSGI_KEY} ||= - _generate_stash_closure; -} - sub call { my ($self, $env) = @_; - _init_stash($env); - return $self->app->($env); + my $new_env = +{ %$env }; + my %stash = %{ ($env->{+PSGI_KEY} || sub {})->() || +{} }; + + $new_env->{+PSGI_KEY} = _create_stash( \%stash ); + return $self->app->($new_env); } -=head1 TITLE +=head1 NAME Catalyst::Middleware::Stash - The Catalyst stash - in middleware @@ -61,7 +58,16 @@ 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. + +The stash middleware is designed so that you can 'nest' applications that +use it. If for example you have a L application that is called +by a controller under a parent L application, the child application +will inherit the full stash of the parent BUT any new keys added by the child +will NOT bubble back up to the parent. However, children of children will. + +For more information the current test case t/middleware-stash.t is the best +documentation. =head1 SUBROUTINES @@ -79,8 +85,7 @@ Expect: $psgi_env. Exportable subroutine. -Get the stash out of the C<$env>. If the stash does not yet exist, we initialize -one and return that. +Get the stash out of the C<$env>. =head2 stash @@ -105,7 +110,7 @@ clients. Stash key / value are stored in memory. ["I found $stashed in the stash!"]]; }; -If the stash does not yet exist, we initialize one and return that. +If the stash does not yet exist, an exception is thrown. =head1 METHODS