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;fp=lib%2FCatalyst%2FMiddleware%2FStash.pm;h=e8746ad2bdbb40ee8fc26d4ecae56c2ae7c45824;hp=8560828e4a905141ba2a286d23e46b77b5d8ce47;hb=561098d14347a2492a5471fc7c9352ad2e6520bc;hpb=817ed8ab62b7b59cc37e82d67aa45824211f75f6 diff --git a/lib/Catalyst/Middleware/Stash.pm b/lib/Catalyst/Middleware/Stash.pm index 8560828..e8746ad 100644 --- a/lib/Catalyst/Middleware/Stash.pm +++ b/lib/Catalyst/Middleware/Stash.pm @@ -1,24 +1,44 @@ -package ## Hide from pause - Catalyst::Middleware::Stash; - -# Please don't use this, this is likely to go away before stable version is -# released. Ideally this could be a stand alone distribution. -# - use strict; use warnings; + +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 get_stash { return shift->{PSGI_KEY} } + +sub generate_stash_closure { + 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 ) { + $stash->{$key} = $new_stash->{$key}; + } + } + $stash; + }; +} + sub _init_stash { my ($self, $env) = @_; - $env->{&PSGI_KEY} = bless +{}, 'Catalyst::Stash'; + return $env->{PSGI_KEY} ||= + generate_stash_closure; } -sub get { - my ($class, $env) = @_; - return $env->{&PSGI_KEY}; +sub stash { + my ($host, @args) = @_; + return get_stash($host->env)->(@args) if + $host->can('env'); } sub call { @@ -37,18 +57,44 @@ We've moved the L stash to middleware. Please don't use this directly since it is likely to move off the Catalyst namespace into a stand alone distribution -=head1 METHODS +We store a coderef under the C which can be dereferenced with +key values or nothing to access the underly hashref. -This class defines the following methods +=head1 SUBROUTINES + +This class defines the following subroutines. =head2 PSGI_KEY Returns the hash key where we store the stash -=head2 get +=head2 get_stash Get the stash out of the C<$env> +=head2 stash + +Exportable subroutine. + +Given an object with a method C get or set stash values, either +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'; + + $c->stash->{foo} = $bar; + $c->stash( { moose => 'majestic', qux => 0 } ); + $c->stash( bar => 1, gorch => 2 ); # equivalent to passing a hashref + +=head2 generate_stash_closure + +Creates the closure which is stored in the L environment. + +=head1 METHODS + +This class defines the following methods. + =head2 call Used by plack to call the middleware