fix issues with middleware stash localizing stuff
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Middleware / Stash.pm
index 7102089..0ed488d 100644 (file)
@@ -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.v2' }
 
 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,8 @@ sub stash {
     ->(@args);
 }
 
-sub _generate_stash_closure {
+sub _create_stash {
+  my $self = shift;
   my $stash = shift || +{};
   return sub {
     if(@_) {
@@ -38,19 +39,15 @@ sub _generate_stash_closure {
   };
 }
 
-sub _init_stash {
-  my ($env) = @_;
-  return $env->{PSGI_KEY} ||=
-    _generate_stash_closure;
-}
-
 sub call {
   my ($self, $env) = @_;
-  _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
 
@@ -61,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<PSGI_KEY> 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
 
@@ -79,8 +87,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 +112,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