RenderCGI.pm

filter: Statementstmt 94.0% 63 / 67 Branchbran 78.5% 22 / 28 Conditioncond 53.1% 17 / 32 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 90.0% 9 / 10 Podpod 33.3% 1 / 3 totaltotal 70.8% 112 / 158 CC 24 SCAR 25.3
CC
24
Cov
80%
handler
20.3
error
6.3
register
3.1
CRAP 12.5
↑ Summary RenderCGI/Template.pm »
1 package Mojolicious::Plugin::RenderCGI;
2  
3 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mojolicious::Plugin::RenderCGI::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Mojo::Util qw(encode md5_sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.102';
8 1 my $pkg = __PACKAGE__;
9  
10 1 has qw(app);
11 1 has handler_name => 'cgi.pl';
12 1 has default => 0;
13 1 ▶has cgi_import => sub { [qw(:html :form)] };
Subroutine __ANON__: called CC=1 SCAR=0.0
14 0 ▶has exception => sub { {'handler'=>'ep', 'layout' => undef,} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
15 2 ▶has cache => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
16  
17 2 ▶sub register {
Subroutine register: called CC=3 SCAR=11.4
Pod documented
18 2 my ($plugin, $app, $conf) = @_;
19
20 2 $plugin->app($app);
21
22 2 map $plugin->$_($conf->{$_}), grep defined($conf->{$_}), qw(name default import exception);
23 #~ $app->renderer->default_handler($plugin->handler_name) не работает
24 2 ▶ $app->log->debug("Set default render handler ".$plugin->handler_name)
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->default 2 0 2
Condition50%2/4
$plugin->default && ($app->log->debug('Set default render handler ' . $plugin->handler_name) && $app->defaults('handler', $plugin->handler_name))
!ll
02
$app->log->debug('Set default render handler ' . $plugin->handler_name) and $app->defaults("handler", $plugin->handler_name)
!ll
02
MC/DC0%0/3
$plugin->default && ($app->log->debug('Set default render handler ' . $plugin->handler_name) && $app->defaults('handler', $plugin->handler_name))
$plugin->default $app->log->debug('Set default render handler ' . $plugin->handler_name) $app->defaults("handler", $plugin->handler_name)
Truth table0%0/4
A && B and C
A= $plugin->defaultB= $app->log->debug('Set default render handler ' . $plugin->handler_name)C= $app->defaults("handler", $plugin->handler_name)
ABCresult
0XX0
10X0
1100
1111
25 and $app->defaults('handler'=>$plugin->handler_name)
26 if $plugin->default;
27
28 $app->renderer->add_handler(
29 14 ▶ $plugin->handler_name => sub {$plugin->handler(@_)}
Subroutine __ANON__: called CC=1 SCAR=0.0
30 2 );
31 }
32  
33 14 ▶sub handler {
Subroutine handler: called CC=18 SCAR=30.1
Pod undocumented
34 14 my ($plugin, $renderer, $c, $output, $options) = @_;
35 14 my $app = $c->app;
36 #~ $app->log->debug($app->dumper($options));
37
38 # относительный путь шаблона
39 14 my $content = $options->{inline};# встроенный шаблон
40 14 ▶ my $name = defined $content ? md5_sum encode('UTF-8', $content) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 1 13 14
41 14 ▶ return unless defined($name //= $renderer->template_path($options) || $renderer->template_name($options));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($name //= $renderer->template_path($options) || $renderer->template_name($options)) 0 14 14
Condition50%3/6
$name //= $renderer->template_path($options) || $renderer->template_name($options)
l!l&&r!l&&!r
1130
$renderer->template_path($options) || $renderer->template_name($options)
l!l&&r!l&&!r
0130
MC/DC0%0/3
$name //= $renderer->template_path($options) || $renderer->template_name($options)
$name $renderer->template_path($options) $renderer->template_name($options)
Truth table0%0/4
A //= B || C
A= $nameB= $renderer->template_path($options)C= $renderer->template_name($options)
ABCresult
1XX1
01X1
0011
0000
42
43 #~ my $url = Mojo::URL->new($name);
44 #~ ($name, my $param) = (url_unescape($url->path), $url->query->to_hash);
45 #~ utf8::decode($name);
46
47 14 my ($template, $from) = ($plugin->cache->{$name}, 'cache');# подходящий шаблон из кэша
48
49 14 my $stash = $c->stash($pkg);
50 14 ▶ $c->stash($pkg => {stack => []})
Branch: 1 branch
BranchTrueFalseTotal
unless $stash 9 5 14
51 unless $stash;
52 14 ▶ $stash ||= $c->stash($pkg);
Condition66%2/3
$stash ||= $c->stash($pkg)
l!l&&r!l&&!r
590
MC/DC0%0/2
$stash ||= $c->stash($pkg)
$stash $c->stash($pkg)
Truth table66%2/3
A ||= B
A= $stashB= $c->stash($pkg)
ABresult
1X1
011
000
53 14 my $last_template = $stash->{stack}[-1];
54 #~ if ($last_template && $last_template eq $name) {
55 #~ $$output = $plugin->error("Stop looping template [$name]!", $c);
56 #~ return;
57 #~ }
58 14 push @{$stash->{stack}}, $name;
59
60 14 $$output = '';
61
62 14 ▶ unless ($template) {#не кэш
Branch: 1 branch
BranchTrueFalseTotal
unless ($template) 13 1 14
63 13 ▶ if (defined $content) {# инлайн
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) { } 1 12 13
64 1 $from = 'inline';
65 } else {
66 # подходящий шаблон в секции DATA
67 12 ($content, $from) = ($renderer->get_data_template($options), 'DATA section');#,, $name
68
69 12 ▶ unless (defined $content) {# file
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 3 9 12
70 # абсолютный путь шаблона
71 3 ▶ if (my $path = $renderer->template_path($options)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = $renderer->template_path($options)) { } 0 3 3
72 0 my $file = Mojo::Asset::File->new(path => $path);
73 0 ($content, $from) = ($file->slurp, 'file');
74
75 } else {
76 3 $$output = $plugin->error(sprintf(qq{Template "%s" does not found}, $name), $c);
77 3 return;
78 }
79 }
80 }
81
82 10 ▶ $app->log->debug(sprintf(qq{Empty or nothing template "%s"}, $name))
Branch: 1 branch
BranchTrueFalseTotal
unless $content =~ /\w/u 3 7 10
Condition75%3/4
$content =~ /\w/u || $app->log->debug(sprintf('Empty or nothing template "%s"', $name)) && (return)
l!l
73
$app->log->debug(sprintf('Empty or nothing template "%s"', $name)) and return
!ll
03
MC/DC0%0/2
$content =~ /\w/u || $app->log->debug(sprintf('Empty or nothing template "%s"', $name)) && (return)
$content =~ /\w/u $app->log->debug(sprintf('Empty or nothing template "%s"', $name))
Truth table66%2/3
A || B
A= $content =~ /\w/uB= $app->log->debug(sprintf('Empty or nothing template "%s"', $name))
ABresult
1X1
011
000
83 and return
84 unless $content =~ /\w/;
85
86 7 utf8::decode($content);
87
88 7 $template = Mojolicious::Plugin::RenderCGI::Template->new(_import=>$plugin->cgi_import, _plugin=>$plugin, );
89  
90 7 my $err = $template->_compile($content);
91
92 7 ▶ $$output = $plugin->error(sprintf(qq{Compile time error for template "%s" from the %s: %s}, $name, $from, $err), $c)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $err 0 7 7
Condition25%1/4
ref $err || ($$output = $plugin->error(sprintf('Compile time error for template "%s" from the %s: %s', $name, $from, $err), $c)) && (return)
l!l
70
$$output = $plugin->error(sprintf('Compile time error for template "%s" from the %s: %s', $name, $from, $err), $c) and return
!ll
00
MC/DC0%0/2
ref $err || ($$output = $plugin->error(sprintf('Compile time error for template "%s" from the %s: %s', $name, $from, $err), $c)) && (return)
ref $err $$output = $plugin->error(sprintf('Compile time error for template "%s" from the %s: %s', $name, $from, $err), $c)
Truth table33%1/3
A || B
A= ref $errB= $$output = $plugin->error(sprintf('Compile time error for template "%s" from the %s: %s', $name, $from, $err), $c)
ABresult
1X1
011
000
93 and return
94 unless ref $err; # ref success
95
96 }
97
98 8 $app->log->debug(sprintf(qq{Rendering template "%s" from the %s}, $name, $from,));
99 8 ▶ $plugin->cache->{$name} ||= $template;
Condition66%2/3
$plugin->cache->{$name} ||= $template
l!l&&r!l&&!r
170
MC/DC0%0/2
$plugin->cache->{$name} ||= $template
$plugin->cache->{$name} $template
Truth table66%2/3
A ||= B
A= $plugin->cache->{$name}B= $template
ABresult
1X1
011
000
100
101 8 my @out = eval { $template->_run($c)};
102 8 ▶ $$output = $plugin->error(sprintf(qq{Runtime error for template "%s" from the %s:\n%s}, $name, $from, $@), $c)
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 6 8
Condition75%3/4
$@ && (($$output = $plugin->error(sprintf(qq[Runtime error for template "%s" from the %s:\n%s], $name, $from, $@), $c)) && (return))
!ll
62
$$output = $plugin->error(sprintf(qq[Runtime error for template "%s" from the %s:\n%s], $name, $from, $@), $c) and return
!ll
02
MC/DC0%0/2
$@ && (($$output = $plugin->error(sprintf(qq[Runtime error for template "%s" from the %s:\n%s], $name, $from, $@), $c)) && (return))
$@ $$output = $plugin->error(sprintf(qq[Runtime error for template "%s" from the %s:\n%s], $name, $from, $@), $c)
Truth table33%1/3
A && B
A= $@B= $$output = $plugin->error(sprintf(qq[Runtime error for template "%s" from the %s:\n%s], $name, $from, $@), $c)
ABresult
0X0
100
111
103 and return
104 if $@;
105
106 6 $$output = join "\n", grep defined, @out;
107
108 }
109  
110 5 ▶sub error {# харе
Subroutine error: called CC=5 SCAR=18.4
Pod undocumented
111 5 my ($plugin, $error, $c) = @_;
112 0 ▶ $c->stash(%{$plugin->exception})
Branch: 1 branch
BranchTrueFalseTotal
if ref $plugin->exception eq "HASH" 0 5 5
Condition25%1/4
ref $plugin->exception eq 'HASH' && ($c->stash(%{$plugin->exception;}) && die($error))
!ll
50
$c->stash(%{$plugin->exception;}) and die $error
!ll
00
MC/DC0%0/2
ref $plugin->exception eq 'HASH' && ($c->stash(%{$plugin->exception;}) && die($error))
ref $plugin->exception eq 'HASH' $c->stash(%{$plugin->exception;})
Truth table33%1/3
A && B
A= ref $plugin->exception eq 'HASH'B= $c->stash(%{$plugin->exception;})
ABresult
0X0
100
111
113 and die $error
114 if ref($plugin->exception) eq 'HASH';
115
116 5 $c->app->log->error($error);# лог после die!
117 5 ▶ return $error
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->exception eq "template" 4 1 5
118 if $plugin->exception eq 'template';
119
120 1 ▶ return "<!-- $error -->"
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->exception =~ /comment/u 0 1 1
121 if $plugin->exception =~ m'comment';
122 };
123  
124 1 1;
125  
126  
127 =pod
128  
129 =encoding utf8
130  
131 Доброго всем
132  
133 =head1 Mojolicious::Plugin::RenderCGI
134  
135 ¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !
136  
137 =head1 NAME
138  
139 Mojolicious::Plugin::RenderCGI - Rendering Mojoliciuos template by CGI.pm subs as tags.
140  
141 =head1 SYNOPSIS
142  
143 $app->plugin('RenderCGI');
144  
145 =head1 Template
146  
147 Template is a Perl code that generate content as list of statements. Similar to C<do BLOCK>. Template file name like "templates/foo/bar.html.cgi.pl"
148  
149 # There are predefined variables:
150 # $self is a Mojolicious::Plugin::RenderCGI::Template object
151 # $c is a current controller object
152 # $cgi is a CGI object
153
154 $c->layout('default', handler=>'ep',);# set handler 'ep' for all templates/includes !!! even default handler cgi
155 my $foo = $c->stash('foo')
156 or die "Where is your FOO?";
157
158 #=======================================
159 #======= content comma list! ===========
160 #=======================================
161
162 h1({}, "Welcome"),# but this template handlered CGI
163 div({-class=>"container"},
164 span('Okay here'),
165 p(['blah', 'blaz']),
166 ),
167
168 $c->include('foo', handler=>'cgi.pl'),# change handler against layout
169 $c->include('bar'); # handler still "ep" unless template "foo" (and its includes) didn`t changes it by $c->stash('handler'=>...)
170
171 <<END_HTML,
172 <!-- comment -->
173 END_HTML
174
175 $self->app->log->info("Template has done")
176 && undef,
177  
178 There are NO Mojolicious helpers without OO-style prefixes: C<< $c-> >>.
179  
180 B<REMEMBER!> Escapes untrusted data. No auto escapes!
181  
182 div({}, esc(...UNTRUSTED DATA...)),
183  
184 C<esc> is a shortcut for &CGI::escapeHTML.
185  
186 =head1 NOTE about autoloading subs and methods
187  
188 In template you can generate any tag:
189  
190 # <foo-tag class="class1">...</foo-tag>
191 foo_tag({-class=>"class1",}, '...'),
192 # same
193 $self->foo_tag({-class=>"class1",}, '...'),
194  
195 =head1 OPTIONS
196  
197 =head2 handler_name ( string )
198  
199 # Mojolicious::Lite
200 plugin RenderCGI => {handler_name => 'pl'};
201  
202 Handler name, defaults to B<cgi.pl>.
203  
204 =head2 default (bool)
205  
206 When C<true> then default handler. Defaults - 0 (no this default handler for app).
207  
208 default => 1,
209  
210 Is similar to C<< $app->defaults(handler=> <name above>); >>
211  
212 =head2 cgi_import ( string (space delims) | arrayref )
213  
214 What subs do you want from CGI.pm import
215  
216 $app->plugin('RenderCGI', cgi_import=>':html ...');
217 # or
218 $app->plugin('RenderCGI', cgi_import=>[qw(:html ...)]);
219  
220 See at perldoc CGI.pm section "USING THE FUNCTION-ORIENTED INTERFACE".
221 Default is ':html :form' (string) same as [qw(:html :form)] (arrayref).
222  
223 cgi_import=>[], # none import subs CGI
224  
225 =head2 exception ( string | hashref )
226  
227 To show fatal errors (not found, compile and runtime errors) as content of there template you must set string B<template>.
228  
229 Set string B<comment> same above but include html comment tag
230  
231 <!-- $error -->
232  
233 To show fatals as standard Mojolicious 'exception.<mode>.html.ep' page - set hashref like {'handler'=>'ep', 'layout' => undef,}.
234  
235 Overwise fatals are skips (empty string whole template).
236  
237 By default set to hashref C<< {'handler'=>'ep', 'layout' => undef,} >>.
238  
239 exception => 'template',
240  
241 =head1 Methods, subs, helpers...
242  
243 Implements register method only. Register new renderer handler. No new helpers.
244  
245 =head1 SEE ALSO
246  
247 L<CGI>
248  
249 L<CGI::HTML::Functions>
250  
251 L<Mojolicious::Plugin::TagHelpers>
252  
253 L<HTML::Tiny>
254  
255 =head1 AUTHOR
256  
257 Михаил Че (Mikhail Che), C<< <mche[-at-]cpan.org> >>
258  
259 =head1 BUGS / CONTRIBUTING
260  
261 Please report any bugs or feature requests at L<https://github.com/mche/Mojolicious-Plugin-RenderCGI/issues>. Pull requests also welcome.
262  
263 =head1 COPYRIGHT
264  
265 Copyright 2016+ Mikhail Che.
266  
267 This library is free software; you can redistribute it and/or modify
268 it under the same terms as Perl itself.
269  
270 =cut
↑ Summary RenderCGI/Template.pm »