| 1 | package Catalyst::Plugin::MessageStack; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 3 | $Catalyst::Plugin::MessageStack::VERSION = '0.03'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: A Catalyst plugin for gracefully handling messaging (and more) that follows the Post/Redirect/Get pattern. | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Message::Stack; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use MRO::Compat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 15 | ▶ | sub message { | ||||||||||||||||||||||||||
|
Subroutine
message: called CC=14 SCAR=35.1
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 15 | my ( $c, $message ) = @_; | |||||||||||||||||||||||||||
| 18 | 15 | my $config = $c->config->{'Plugin::MessageStack'}; | |||||||||||||||||||||||||||
| 19 | 15 | ▶ | my $default = $config->{default_type} || 'success'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{'default_type'} || 'success'
MC/DC0%0/1
$config->{'default_type'} || 'success'
$config->{'default_type'}
Truth table50%1/2
A
A= $config->{'default_type'}
| |||||||||||||||||||||||||||||
| 20 | 15 | ▶ | my $stash_key = $config->{stash_key} || 'messages'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'stash_key'} || 'messages'
MC/DC100%1/1
$config->{'stash_key'} || 'messages'
$config->{'stash_key'}
Truth table100%2/2
A
A= $config->{'stash_key'}
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 15 | ▶ | if ( not defined $c->stash->{$stash_key} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 23 | 7 | ▶ | if ( $config->{model} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 0 | $c->stash->{$stash_key} = $c->model($config->{model})->messages; | |||||||||||||||||||||||||||
| 25 | } else { | ||||||||||||||||||||||||||||
| 26 | 7 | $c->stash->{$stash_key} = Message::Stack->new; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | ▶ | elsif ( not blessed $c->stash->{$stash_key} and | |||||||||||||||||||||||||||
|
Condition33%1/3
builtin::blessed($c->stash->{$stash_key}) || $c->stash->{$stash_key}->isa('Message::Stack')
MC/DC0%0/2
builtin::blessed($c->stash->{$stash_key}) || $c->stash->{$stash_key}->isa('Message::Stack')
builtin::blessed($c->stash->{$stash_key})
$c->stash->{$stash_key}->isa('Message::Stack')
Truth table33%1/3
A || B
A= builtin::blessed($c->stash->{$stash_key})B= $c->stash->{$stash_key}->isa('Message::Stack')
| |||||||||||||||||||||||||||||
| 30 | not $c->stash->{$stash_key}->isa('Message::Stack') ) | ||||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||
| 32 | 0 | $c->log->error("Unable to add messages into the stash, the stash has data at $stash_key already, and it isn't a Message::Stack"); | |||||||||||||||||||||||||||
| 33 | 0 | return; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 15 | my $stash = $c->stash->{$stash_key}; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 15 | ▶ | return $stash unless $message; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 8 | ▶ | if ( blessed $message ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | $stash->add($message); | |||||||||||||||||||||||||||
| 41 | } else { | ||||||||||||||||||||||||||||
| 42 | 8 | my $s = { scope => 'global' }; | |||||||||||||||||||||||||||
| 43 | 8 | ▶ | if ( ref $message ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $s->{level} = $message->{type} || $default; | ||||||||||||||||||||||||||
|
Condition0%0/3
$message->{'type'} || $default
MC/DC0%0/2
$message->{'type'} || $default
$message->{'type'}
$default
Truth table0%0/3
A || B
A= $message->{'type'}B= $default
| |||||||||||||||||||||||||||||
| 45 | 0 | $s->{id} = $message->{message}; | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | $s->{scope} = $message->{scope} || 'global'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$message->{'scope'} || 'global'
MC/DC0%0/1
$message->{'scope'} || 'global'
$message->{'scope'}
Truth table0%0/2
A
A= $message->{'scope'}
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | $s->{subject} = $message->{subject} if($message->{subject}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $s->{params} = $message->{params} if($message->{params}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | } else { | ||||||||||||||||||||||||||||
| 50 | 8 | $s->{level} = $default; | |||||||||||||||||||||||||||
| 51 | 8 | $s->{id} = $message; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 8 | $stash->add($s); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 8 | $c->stash->{$stash_key} = $stash; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 8 | return $stash; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 8 | ▶ | sub has_messages { | ||||||||||||||||||||||||||
|
Subroutine
has_messages: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 8 | my ( $c, $scope ) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 8 | ▶ | my $stash_key = $c->config->{'Plugin::MessageStack'}->{stash_key} || 'messages'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$c->config->{'Plugin::MessageStack'}{'stash_key'} || 'messages'
MC/DC100%1/1
$c->config->{'Plugin::MessageStack'}{'stash_key'} || 'messages'
$c->config->{'Plugin::MessageStack'}{'stash_key'}
Truth table100%2/2
A
A= $c->config->{'Plugin::MessageStack'}{'stash_key'}
| |||||||||||||||||||||||||||||
| 66 | 8 | my $stack = $c->stash->{$stash_key}; | |||||||||||||||||||||||||||
| 67 | 8 | ▶ | return 0 unless defined $stack; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 7 | ▶ | if ( $scope ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | return $stack->for_scope($scope)->has_messages; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | 7 | return $stack->has_messages; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | sub reset_messages { | ||||||||||||||||||||||||||
|
Subroutine
reset_messages: called CC=4 SCAR=23.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | 1 | my ( $c, $scope ) = @_; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | my $stash_key = $c->config->{'Plugin::MessageStack'}->{stash_key} || 'messages'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$c->config->{'Plugin::MessageStack'}{'stash_key'} || 'messages'
MC/DC0%0/1
$c->config->{'Plugin::MessageStack'}{'stash_key'} || 'messages'
$c->config->{'Plugin::MessageStack'}{'stash_key'}
Truth table50%1/2
A
A= $c->config->{'Plugin::MessageStack'}{'stash_key'}
| |||||||||||||||||||||||||||||
| 79 | 1 | my $stack = $c->stash->{$stash_key}; | |||||||||||||||||||||||||||
| 80 | 0 | ▶ | return 0 unless defined $stack; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | my $count = $stack->count; | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ( $scope ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | $count = $stack->for_scope($scope)->count; | |||||||||||||||||||||||||||
| 85 | 0 | $stack->reset_scope($scope); | |||||||||||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||||||||||
| 87 | 0 | $stack->reset; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 0 | return $count; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 22 | ▶ | sub dispatch { | ||||||||||||||||||||||||||
|
Subroutine
dispatch: called CC=17 SCAR=32.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 22 | my $c = shift; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 22 | my $config = $c->config->{'Plugin::MessageStack'}; | |||||||||||||||||||||||||||
| 97 | 22 | ▶ | my $stash_key = $config->{stash_key} || 'messages'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'stash_key'} || 'messages'
MC/DC100%1/1
$config->{'stash_key'} || 'messages'
$config->{'stash_key'}
Truth table100%2/2
A
A= $config->{'stash_key'}
| |||||||||||||||||||||||||||||
| 98 | 22 | ▶ | my $flash_key = $config->{flash_key} || '_messages'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'flash_key'} || '_messages'
MC/DC100%1/1
$config->{'flash_key'} || '_messages'
$config->{'flash_key'}
Truth table100%2/2
A
A= $config->{'flash_key'}
| |||||||||||||||||||||||||||||
| 99 | 22 | ▶ | my $rflash_key = $config->{results_flash_key} || '_results'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'results_flash_key'} || '_results'
MC/DC100%1/1
$config->{'results_flash_key'} || '_results'
$config->{'results_flash_key'}
Truth table100%2/2
A
A= $config->{'results_flash_key'}
| |||||||||||||||||||||||||||||
| 100 | 22 | ▶ | my $rstash_key = $config->{results_stash_key} || 'results'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{'results_stash_key'} || 'results'
MC/DC100%1/1
$config->{'results_stash_key'} || 'results'
$config->{'results_stash_key'}
Truth table100%2/2
A
A= $config->{'results_stash_key'}
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # Copy to the stash | ||||||||||||||||||||||||||||
| 103 | 22 | ▶ | if ( $c->can('flash') and $c->flash->{$flash_key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$c->can("flash") and $c->flash->{$flash_key}
MC/DC50%1/2
$c->can("flash") and $c->flash->{$flash_key}
$c->can("flash")
$c->flash->{$flash_key}
Truth table66%2/3
A and B
A= $c->can("flash")B= $c->flash->{$flash_key}
| |||||||||||||||||||||||||||||
| 104 | 9 | $c->stash->{$stash_key} = delete $c->flash->{$flash_key}; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 22 | ▶ | if ( $c->can('flash') and $c->flash->{$rflash_key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$c->can("flash") and $c->flash->{$rflash_key}
MC/DC0%0/2
$c->can("flash") and $c->flash->{$rflash_key}
$c->can("flash")
$c->flash->{$rflash_key}
Truth table33%1/3
A and B
A= $c->can("flash")B= $c->flash->{$rflash_key}
| |||||||||||||||||||||||||||||
| 108 | 0 | $c->stash->{$rstash_key} = delete $c->flash->{$rflash_key}; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 22 | my $ret = $c->next::method(@_); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 22 | ▶ | return $ret unless defined $c->res->location; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Redirect? | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 9 | my $messages = $c->stash->{$stash_key}; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # No messages in stash, but check if we have something in the Data::Manager | ||||||||||||||||||||||||||||
| 121 | # model. | ||||||||||||||||||||||||||||
| 122 | 9 | ▶ | if ( not $messages and $config->{model} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $messages and $config->{"model"}
MC/DC0%0/2
not $messages and $config->{"model"}
not $messages
$config->{"model"}
Truth table33%1/3
not(A) and B
A= not $messagesB= $config->{"model"}
| |||||||||||||||||||||||||||||
| 123 | 0 | $messages = $c->model($config->{model})->messages; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 9 | ▶ | return $ret unless defined $messages; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 9 | ▶ | if ( $messages->has_messages and $c->response->location) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$messages->has_messages and $c->response->location
MC/DC0%0/2
$messages->has_messages and $c->response->location
$messages->has_messages
$c->response->location
Truth table33%1/3
A and B
A= $messages->has_messagesB= $c->response->location
| |||||||||||||||||||||||||||||
| 129 | 9 | $c->flash->{$flash_key} = $messages; | |||||||||||||||||||||||||||
| 130 | 9 | $c->keep_flash($flash_key); | |||||||||||||||||||||||||||
| 131 | 9 | ▶ | if ( $config->{model} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | ▶ | $c->flash->{$rflash_key} ||= $c->model($config->{model})->results; | ||||||||||||||||||||||||||
|
Condition0%0/3
$c->flash->{$rflash_key} ||= $c->model($config->{"model"})->results
MC/DC0%0/2
$c->flash->{$rflash_key} ||= $c->model($config->{"model"})->results
$c->flash->{$rflash_key}
$c->model($config->{"model"})->results
Truth table0%0/3
A ||= B
A= $c->flash->{$rflash_key}B= $c->model($config->{"model"})->results
| |||||||||||||||||||||||||||||
| 133 | 0 | $c->keep_flash($rflash_key); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 9 | return $ret; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 3 | 1; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||||||||||