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 164     164   2086 use strict;
  164         502  
  164         5504  
2 164     164   1037 use warnings;
  164         540  
  164         7219  
3              
4              
5             use base 'Plack::Middleware';
6 164     164   13599 use Exporter 'import';
  164         524  
  164         17514  
7 164     164   1168 use Carp 'croak';
  164         431  
  164         5368  
8 164     164   1013  
  164         423  
  164         60048  
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 2416     2416 1 4784 }
16 2416   33     9239  
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 922     922   2262 croak('stash takes a hash or hashref')
28 922   50     4568 unless ref $new_stash;
29             foreach my $key (keys %$new_stash) {
30 2416 100   2416   6745 $stash->{$key} = $new_stash->{$key};
31 627 100       3623 }
32 627 50       2513 }
33             $stash;
34 627         2623 };
35 629         2903 }
36              
37             my ($self, $env) = @_;
38 2416         8139 $env->{+PSGI_KEY} = $self->_create_stash
39 922         5274 unless exists($env->{+PSGI_KEY});
40              
41             return $self->app->($env);
42             }
43 925     925 1 230091  
44             =head1 NAME
45 925 100       5293  
46             Catalyst::Middleware::Stash - The Catalyst stash - in middleware
47 925         3638  
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;