lib/Mojolicious/Plugin/Fondation/Helpers.pm

filter: Statementstmt 86.3% 57 / 66 Branchbran 53.5% 15 / 28 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 83.3% 15 / 18 Podpod 0.0% 0 / 1 totaltotal 77.5% 90 / 116 CC 19 SCAR 16.3
CC
19
Cov
77%
__ANON__
12.0
__ANON__
12.0
__ANON__
8.0
CRAP 5.1
« lib/Mojolicious/Plugin/Fondation/Resolver.pm ↑ Summary lib/Mojolicious/Plugin/Fondation/Action/Controllers.pm »
1 package Mojolicious::Plugin::Fondation::Helpers;
2 # ABSTRACT: All Fondation helpers in one place -- keeps Fondation.pm minimal
3 16 $Mojolicious::Plugin::Fondation::Helpers::VERSION = '0.08';
4 16 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use Mojo::ByteStream 'b';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 70 ▶sub register ($class, $app, $manager) {
Subroutine register: called CC=4 SCAR=14.5
Pod undocumented
8  
9 # ═══════════════════════════════════════════════════════════════════════
10 # ── Core identity ──
11 # ═══════════════════════════════════════════════════════════════════════
12 70 ▶ $app->helper(manager => sub { $manager });
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14 # Stable public API (recommended over direct manager access)
15 70 ▶ $app->helper(fondation => sub { $manager->api });
Subroutine __ANON__: called CC=1 SCAR=0.0
16  
17 # ═══════════════════════════════════════════════════════════════════════
18 # ── Fallback helpers -- overridden by specialized plugins ──
19 # Must be registered BEFORE load_plugin_recursive so plugins can override.
20 # ═══════════════════════════════════════════════════════════════════════
21  
22 # Overridden by I18N-like plugins
23 70 ▶ $app->helper(l => sub { $_[1] });
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 # Overridden by I18N — returns ISO date string when I18N absent
26 0 ▶ $app->helper(format_date => sub ($c, $val) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
27 0 ▶ return '' unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 0 0
28 0 ▶ return $val->strftime('%Y-%m-%d %H:%M') if ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 0 0 0
29 0 return $val;
30 70 });
31  
32 # Fallback i18n_js -- injected by layout before app JS.
33 # Identity function when I18N absent; overridden by I18N-like plugins.
34 0 ▶ $app->helper(i18n_js => sub ($c) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 0 return Mojo::ByteStream->new(
36 q{<script>window.l=function(k){return k;};</script>}
37 );
38 70 });
39  
40 # Overridden by a Notification plugin
41 70 ▶ $app->helper(notify_user => sub { Mojo::Promise->resolve() });
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 # Overridden by a Authorization plugin -- permissive fallback (allow all)
44 70 ▶ $app->helper(check_group => sub { 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
45 70 ▶ $app->helper(check_perm => sub { 1 });
Subroutine __ANON__: called CC=1 SCAR=0.0
46  
47 # ═══════════════════════════════════════════════════════════════════════
48 # ── Route conditions ──
49 # ── check_perm/check_group are no-ops above until Authorization plugin
50 # ── overrides them.
51 # ═══════════════════════════════════════════════════════════════════════
52  
53 # ── Route conditions (only if not already set by a plugin) ──
54 # Auth/Authorization register their own versions during load_all;
55 # these fallbacks only apply when no plugin overrode them.
56  
57 10 ▶ $app->routes->add_condition('fondation.perm' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
58 10 my ($route, $c, $captures, $perm) = @_;
59 10 ▶ return 1 if $c->check_perm($perm);
Branch: 1 branch
BranchTrueFalseTotal
if $c->check_perm($perm) 3 7 10
60 7 $c->res->code(403);
61 7 ▶ $c->stash('fondation.denied' => { status => 403, title => 'Forbidden' })
Branch: 1 branch
BranchTrueFalseTotal
unless $c->stash("fondation.denied") 5 2 7
62 unless $c->stash('fondation.denied');
63 7 return undef;
64 70 ▶ }) unless $app->routes->conditions->{'fondation.perm'};
Branch: 1 branch
BranchTrueFalseTotal
unless $app->routes->conditions->{"fondation.perm"} 70 0 70
65  
66 4 ▶ $app->routes->add_condition('fondation.group' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
67 4 my ($route, $c, $captures, $group) = @_;
68 4 ▶ return 1 if $c->check_group($group);
Branch: 1 branch
BranchTrueFalseTotal
if $c->check_group($group) 2 2 4
69 2 $c->res->code(403);
70 2 ▶ $c->stash('fondation.denied' => { status => 403, title => 'Forbidden' })
Branch: 1 branch
BranchTrueFalseTotal
unless $c->stash("fondation.denied") 2 0 2
71 unless $c->stash('fondation.denied');
72 2 return undef;
73 70 ▶ }) unless $app->routes->conditions->{'fondation.group'};
Branch: 1 branch
BranchTrueFalseTotal
unless $app->routes->conditions->{"fondation.group"} 70 0 70
74  
75 0 ▶ $app->routes->add_condition('fondation.authenticated' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
76 0 my ($route, $c, $captures, $required) = @_;
77 # Default: nobody is authenticated (overridden by Fondation::Auth)
78 0 ▶ return 1 unless $required;
Branch: 1 branch
BranchTrueFalseTotal
unless $required 0 0 0
79 0 $c->res->code(403);
80 0 ▶ $c->stash('fondation.denied' => { status => 403, title => 'Forbidden' })
Branch: 1 branch
BranchTrueFalseTotal
unless $c->stash("fondation.denied") 0 0 0
81 unless $c->stash('fondation.denied');
82 0 return undef;
83 70 ▶ }) unless $app->routes->conditions->{'fondation.authenticated'};
Branch: 1 branch
BranchTrueFalseTotal
unless $app->routes->conditions->{"fondation.authenticated"} 70 0 70
84  
85  
86 # ═══════════════════════════════════════════════════════════════════════
87 # ── Real helpers (not no-ops) ──
88 # ═══════════════════════════════════════════════════════════════════════
89  
90 # Check whether a helper exists (Mojo helpers are not visible via $c->can).
91 82 ▶ $app->helper(has_helper => sub ($c, $name) {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 82 return exists $c->app->renderer->helpers->{$name};
93 70 });
94  
95  
96  
97 # ═══════════════════════════════════════════════════════════════════════
98 # ── Zone system ──
99 # ═══════════════════════════════════════════════════════════════════════
100  
101 6 ▶ $app->helper(render_zone_type => sub ($c, $type, $zone) {
Subroutine __ANON__: called CC=8 SCAR=20.8
102 6 my $manager = $c->app->manager;
103 6 my $output = '';
104  
105 6 for my $long (@{$manager->load_order}) {
106 14 my $entry = $manager->registry->{$long};
107 14 ▶ next unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 0 14 14
108  
109 14 ▶ my $files = $entry->{zones}{$type}{$zone} // [];
Condition100%2/2
$entry->{"zones"}{$type}{$zone} // []
l!l
410
MC/DC100%1/1
$entry->{"zones"}{$type}{$zone} // []
$entry->{"zones"}{$type}{$zone}
Truth table100%2/2
A
A= $entry->{"zones"}{$type}{$zone}
Aresult
11
00
110 14 ▶ next unless @$files;
Branch: 1 branch
BranchTrueFalseTotal
unless @$files 10 4 14
111  
112 4 ▶ if ($type eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'html') { } 3 1 4
113 3 for my $template (@$files) {
114 3 $output .= $c->render_to_string(
115 template => $template,
116 layout => undef,
117 );
118 }
119 }
120 else {
121 1 for my $content (@$files) {
122 1 $output .= $content;
123 }
124 }
125 }
126  
127 6 return $output;
128 70 });
129  
130 4 ▶ $app->helper(render_zone => sub ($c, $zone) {
Subroutine __ANON__: called CC=1 SCAR=0.0
131 4 return $c->render_zone_type('html', $zone);
132 70 });
133  
134 2 ▶ $app->helper(render_zone_js => sub ($c, $zone) {
Subroutine __ANON__: called CC=1 SCAR=0.0
135 2 return $c->render_zone_type('js', $zone);
136 70 });
137  
138 70 return;
139 }
140  
141 16 1;
142  
143 __END__
« lib/Mojolicious/Plugin/Fondation/Resolver.pm ↑ Summary lib/Mojolicious/Plugin/Fondation/Action/Controllers.pm »