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