| 1 | package Dancer::Plugin::Controller; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '0.15'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Dancer::Plugin::Controller - interface between a model and view | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =cut | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # YourApp.pm | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use Dancer ':syntax'; | ||||||||||||||||||||||||||||
| 16 | use Dancer::Plugin::Controller '0.15'; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | use YouApp::Action::Index; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | get '/' => sub { | ||||||||||||||||||||||||||||
| 22 | controller( | ||||||||||||||||||||||||||||
| 23 | action => 'Index', # lib/<config:action_prefix>/Index.pm | ||||||||||||||||||||||||||||
| 24 | template => 'index', # views/index.[tt|tpl|...] | ||||||||||||||||||||||||||||
| 25 | layout => 'page_custom_layout', # if you need other than default layout | ||||||||||||||||||||||||||||
| 26 | redirect_404 => '404.html' # redirect to if action method return undef | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # YourApp::Action::Index.pm | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | sub main { | ||||||||||||||||||||||||||||
| 34 | my ($self) = @_; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | my $params = $self->params; # $params - contains Dancer::params() and Dancer::vars() | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | ... | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | return $template_params_hashref; | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # config.yml | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | plugins: | ||||||||||||||||||||||||||||
| 47 | "Controller": | ||||||||||||||||||||||||||||
| 48 | # this is prefiix for module with implementation of action | ||||||||||||||||||||||||||||
| 49 | action_prefix: 'MyActionPrefix' # default: 'Action' | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 2 | ▶ | use Dancer ':syntax'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 2 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | register controller => sub { | ||||||||||||||||||||||||||||
| 63 | 6 | ▶ | my ($self, %params) = plugin_args(@_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=29.2
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 6 | ▶ | my $template_name = $params{template} || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$params{'template'} || ''
MC/DC0%0/1
$params{'template'} || ''
$params{'template'}
Truth table50%1/2
A
A= $params{'template'}
| |||||||||||||||||||||||||||||
| 66 | 6 | ▶ | my $custom_layout = $params{layout} || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$params{'layout'} || ''
MC/DC0%0/1
$params{'layout'} || ''
$params{'layout'}
Truth table50%1/2
A
A= $params{'layout'}
| |||||||||||||||||||||||||||||
| 67 | 6 | ▶ | my $action_name = $params{action} || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$params{'action'} || ''
MC/DC0%0/1
$params{'action'} || ''
$params{'action'}
Truth table50%1/2
A
A= $params{'action'}
| |||||||||||||||||||||||||||||
| 68 | 6 | ▶ | my $redirect_404 = $params{redirect_404} || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$params{'redirect_404'} || ''
MC/DC100%1/1
$params{'redirect_404'} || ''
$params{'redirect_404'}
Truth table100%2/2
A
A= $params{'redirect_404'}
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 6 | my $conf = plugin_setting(); | |||||||||||||||||||||||||||
| 71 | 6 | ▶ | my $action_prefix = $conf->{action_prefix} || 'Action'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$conf->{'action_prefix'} || 'Action'
MC/DC0%0/1
$conf->{'action_prefix'} || 'Action'
$conf->{'action_prefix'}
Truth table50%1/2
A
A= $conf->{'action_prefix'}
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 6 | my $action_base_class = sprintf('%s::%s', Dancer::config->{appname}, $action_prefix); | |||||||||||||||||||||||||||
| 74 | 6 | my $action_class = sprintf('%s::%s', $action_base_class, $action_name); | |||||||||||||||||||||||||||
| 75 | my $action_params = { | ||||||||||||||||||||||||||||
| 76 | Dancer::params(), | ||||||||||||||||||||||||||||
| 77 | 6 | %{Dancer::vars()} | |||||||||||||||||||||||||||
| 78 | }; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 6 | my $action_result = {}; | |||||||||||||||||||||||||||
| 81 | 6 | ▶ | if ($action_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | 6 | *{$action_class. '::ISA'} = ($action_base_class); | |||||||||||||||||||||||||||
| 84 | 6 | ▶ | *{$action_class. '::params'} = sub { $_[0]->{params} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | use strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 6 | my $action_obj = bless { params => $action_params }, $action_class; | |||||||||||||||||||||||||||
| 88 | 6 | $action_result = $action_obj->main; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if (not defined $action_result and $redirect_404) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $action_result and $redirect_404
MC/DC0%0/2
not defined $action_result and $redirect_404
not defined $action_result
$redirect_404
Truth table0%0/3
not(A) and B
A= not defined $action_resultB= $redirect_404
| |||||||||||||||||||||||||||||
| 92 | 0 | return redirect $redirect_404; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | else { | ||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($template_name) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | return Dancer::template( | ||||||||||||||||||||||||||||
| 97 | $template_name, | ||||||||||||||||||||||||||||
| 98 | $action_result, | ||||||||||||||||||||||||||||
| 99 | { layout => $custom_layout || Dancer::config->{layout} } | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$custom_layout || Dancer::config()->{'layout'}
MC/DC0%0/2
$custom_layout || Dancer::config()->{'layout'}
$custom_layout
Dancer::config()->{'layout'}
Truth table0%0/3
A || B
A= $custom_layoutB= Dancer::config()->{'layout'}
| |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | else { | ||||||||||||||||||||||||||||
| 103 | 0 | return $action_result; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 2 | }; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2 | register_plugin; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 2 | 1; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Mikhail N Bogdanov C<< <mbogdanov at cpan.org> >> | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||||||