File Coverage

blib/lib/Catalyst/Middleware/Stash.pm
Criterion Covered Total %
statement 29 31 93.5
branch 7 8 87.5
condition 2 5 40.0
subroutine 9 10 90.0
pod 3 3 100.0
total 50 57 87.7


line stmt bran cond sub pod time code
1 163     163   1762 use strict;
  163         437  
  163         4331  
2 163     163   1051 use warnings;
  163         529  
  163         5885  
3              
4              
5             use base 'Plack::Middleware';
6 163     163   944 use Exporter 'import';
  163         701  
  163         24723  
7 163     163   1141 use Carp 'croak';
  163         429  
  163         4370  
8 163     163   942  
  163         490  
  163         51704  
9             our @EXPORT_OK = qw(stash get_stash);
10              
11              
12             my $env = shift;
13             return $env->{+PSGI_KEY} ||
14             croak "You requested a stash, but one does not exist.";
15 2414     2414 1 3673 }
16 2414   33     7033  
17             my ($host, @args) = @_;
18             return get_stash($host->env)
19             ->(@args);
20             }
21 0     0 1 0  
22 0         0 my $self = shift;
23             my $stash = shift || +{};
24             return sub {
25             if(@_) {
26             my $new_stash = @_ > 1 ? {@_} : $_[0];
27 920     920   1617 croak('stash takes a hash or hashref')
28 920   50     3395 unless ref $new_stash;
29             foreach my $key (keys %$new_stash) {
30 2414 100   2414   5445 $stash->{$key} = $new_stash->{$key};
31 627 100       2597 }
32 627 50       1690 }
33             $stash;
34 627         1828 };
35 629         1928 }
36              
37             my ($self, $env) = @_;
38 2414         6332 $env->{+PSGI_KEY} = $self->_create_stash
39 920         3901 unless exists($env->{+PSGI_KEY});
40              
41             return $self->app->($env);
42             }
43 923     923 1 178921  
44             =head1 NAME
45 923 100       3723  
46             Catalyst::Middleware::Stash - The Catalyst stash - in middleware
47 923         2657  
48             =head1 DESCRIPTION
49              
50             We've moved the L<Catalyst> stash to middleware. Please don't use this
51             directly since it is likely to move off the Catalyst namespace into a stand
52             alone distribution
53              
54             We store a coderef under the C<PSGI_KEY> which can be dereferenced with
55             key values or nothing to access the underlying hashref.
56              
57             Anything placed into the stash will be available in the stash of any 'mounted'
58             Catalyst applications. A mounted Catalyst application may set the stash and
59             'pass back' information to the parent application. Non Catalyst applications
60             may use this middleware to access and set stash values.
61              
62             Please note I highly recommend having a stronger interface than a stash key
63             between applications.
64              
65             For more information the current test case t/middleware-stash.t is the best
66             documentation.
67              
68             =head1 SUBROUTINES
69              
70             This class defines the following subroutines.
71              
72             =head2 PSGI_KEY
73              
74             Returns the hash key where we store the stash. You should not assume
75             the string value here will never change! Also, its better to use
76             L</get_stash> or L</stash>.
77              
78             =head2 get_stash
79              
80             Expect: $psgi_env.
81              
82             Exportable subroutine.
83              
84             Get the stash out of the C<$env>.
85              
86             =head2 stash
87              
88             Expects: An object that does C<env> and arguments
89              
90             Exportable subroutine.
91              
92             Given an object with a method C<env> get or set stash values, either
93             as a method or via hashref modification. This stash is automatically
94             reset for each request (it is not persistent or shared across connected
95             clients. Stash key / value are stored in memory.
96              
97             use Plack::Request;
98             use Catalyst::Middleware::Stash 'stash';
99              
100             my $app = sub {
101             my $env = shift;
102             my $req = Plack::Request->new($env);
103             my $stashed = $req->stash->{in_the_stash}; # Assume the stash was previously populated.
104              
105             return [200, ['Content-Type' => 'text/plain'],
106             ["I found $stashed in the stash!"]];
107             };
108              
109             If the stash does not yet exist, an exception is thrown.
110              
111             =head1 METHODS
112              
113             This class defines the following methods.
114              
115             =head2 call
116              
117             Used by plack to call the middleware
118              
119             =cut
120              
121             1;