fix spelling to satisfy t/author/spelling.t
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Middleware / Stash.pm
CommitLineData
6561feae 1use strict;
2use warnings;
561098d1 3
4package Catalyst::Middleware::Stash;
5
6561feae 6use base 'Plack::Middleware';
561098d1 7use Exporter 'import';
561098d1 8use Carp 'croak';
9
10our @EXPORT_OK = qw(stash get_stash);
6561feae 11
4b0b7489 12sub PSGI_KEY () { 'Catalyst.Stash.v1' }
6561feae 13
300b04ce 14sub get_stash {
15 my $env = shift;
4b0b7489 16 return $env->{+PSGI_KEY} ||
17 croak "You requested a stash, but one does not exist.";
300b04ce 18}
561098d1 19
300b04ce 20sub stash {
21 my ($host, @args) = @_;
22 return get_stash($host->env)
23 ->(@args);
24}
25
e77ad76e 26sub _create_stash {
561098d1 27 my $stash = shift || +{};
28 return sub {
29 if(@_) {
30 my $new_stash = @_ > 1 ? {@_} : $_[0];
31 croak('stash takes a hash or hashref')
32 unless ref $new_stash;
300b04ce 33 foreach my $key (keys %$new_stash) {
561098d1 34 $stash->{$key} = $new_stash->{$key};
35 }
36 }
37 $stash;
38 };
39}
40
6561feae 41sub call {
42 my ($self, $env) = @_;
4b0b7489 43 my $new_env = +{ %$env };
44 my %stash = %{ ($env->{+PSGI_KEY} || sub {})->() || +{} };
45
46 $new_env->{+PSGI_KEY} = _create_stash( \%stash );
47 return $self->app->($new_env);
6561feae 48}
49
0dd88998 50=head1 NAME
6561feae 51
52Catalyst::Middleware::Stash - The Catalyst stash - in middleware
53
54=head1 DESCRIPTION
55
56We've moved the L<Catalyst> stash to middleware. Please don't use this
57directly since it is likely to move off the Catalyst namespace into a stand
58alone distribution
59
561098d1 60We store a coderef under the C<PSGI_KEY> which can be dereferenced with
566678d0 61key values or nothing to access the underlying hashref.
6561feae 62
4b0b7489 63The stash middleware is designed so that you can 'nest' applications that
64use it. If for example you have a L<Catalyst> application that is called
65by a controller under a parent L<Catalyst> application, the child application
66will inherit the full stash of the parent BUT any new keys added by the child
67will NOT bubble back up to the parent. However, children of children will.
68
69For more information the current test case t/middleware-stash.t is the best
70documentation.
71
561098d1 72=head1 SUBROUTINES
73
74This class defines the following subroutines.
6561feae 75
76=head2 PSGI_KEY
77
300b04ce 78Returns the hash key where we store the stash. You should not assume
79the string value here will never change! Also, its better to use
80L</get_stash> or L</stash>.
6561feae 81
561098d1 82=head2 get_stash
6561feae 83
300b04ce 84Expect: $psgi_env.
85
86Exportable subroutine.
87
e77ad76e 88Get the stash out of the C<$env>.
6561feae 89
561098d1 90=head2 stash
91
300b04ce 92Expects: An object that does C<env> and arguments
93
561098d1 94Exportable subroutine.
95
96Given an object with a method C<env> get or set stash values, either
97as a method or via hashref modification. This stash is automatically
98reset for each request (it is not persistent or shared across connected
99clients. Stash key / value are stored in memory.
100
300b04ce 101 use Plack::Request;
102 use Catalyst::Middleware::Stash 'stash';
561098d1 103
300b04ce 104 my $app = sub {
105 my $env = shift;
106 my $req = Plack::Request->new($env);
107 my $stashed = $req->stash->{in_the_stash}; # Assume the stash was previously populated.
561098d1 108
300b04ce 109 return [200, ['Content-Type' => 'text/plain'],
110 ["I found $stashed in the stash!"]];
111 };
561098d1 112
4b0b7489 113If the stash does not yet exist, an exception is thrown.
561098d1 114
115=head1 METHODS
116
117This class defines the following methods.
118
6561feae 119=head2 call
120
121Used by plack to call the middleware
122
123=cut
124
1251;