lib/Mojolicious/Plugin/Fondation/Action/Templates.pm

filter: Statementstmt 100% 50 / 50 Branchbran 91.6% 11 / 12 Conditioncond 53.3% 8 / 15 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 82.8% 77 / 93 CC 14 SCAR 12.6
CC
14
Cov
90%
__ANON__
5.1
after_load
4.0
_register_templates
3.0
CRAP 3.5
« lib/Mojolicious/Plugin/Fondation/Utils.pm ↑ Summary lib/Mojolicious/Plugin/Fondation/TestHelper.pm »
1 package Mojolicious::Plugin::Fondation::Action::Templates;
2 15 $Mojolicious::Plugin::Fondation::Action::Templates::VERSION = '0.08';
3 # ABSTRACT: Registers templates and zones from plugin share directories
4  
5 15 ▶use Mojo::Base 'Mojolicious::Plugin::Fondation::Action::Base', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 ▶use Mojolicious::Plugin::Fondation::Utils qw(share_relative);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 165 ▶sub after_load ($self, $long, $conf, $share_dir) {
Subroutine after_load: called CC=4 SCAR=13.9
Pod undocumented
10 165 ▶ return unless $share_dir && -d $share_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $share_dir and -d $share_dir 22 143 165
Condition66%2/3
$share_dir and -d $share_dir
!ll&&!rl&&r
022143
MC/DC50%1/2
$share_dir and -d $share_dir
$share_dir -d $share_dir
Truth table66%2/3
A and B
A= $share_dirB= -d $share_dir
ABresult
0X0
100
111
11  
12 143 my $templates_dir = $share_dir->child('templates');
13 143 ▶ return unless -d $templates_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $templates_dir 52 91 143
14  
15 91 my $manager = $self->manager;
16 91 my $app = $manager->app;
17 91 my $short = $manager->registry->{$long}{short_name};
18  
19 # Add templates directory (priority: "earlier in load_order = higher priority")
20 91 push @{$app->renderer->paths}, $templates_dir->to_string;
21 91 $self->log->debug("Added templates path: " . share_relative($templates_dir));
22  
23 # Register templates in the registry
24 91 $self->_register_templates($long, $short, $templates_dir);
25 91 $self->_register_zones($long, $short, $templates_dir);
26 }
27  
28 91 ▶sub _register_templates ($self, $long, $short, $templates_dir) {
Subroutine _register_templates: called CC=3 SCAR=11.1
29 91 my $manager = $self->manager;
30 91 ▶ my $entry = $manager->registry->{$long} //= {};
Condition50%1/2
$manager->registry->{$long} //= {}
l!l
910
MC/DC0%0/1
$manager->registry->{$long} //= {}
$manager->registry->{$long}
Truth table50%1/2
A
A= $manager->registry->{$long}
Aresult
11
00
31  
32 91 ▶ $entry->{templates} //= {};
Condition50%1/2
$entry->{"templates"} //= {}
l!l
091
MC/DC0%0/1
$entry->{"templates"} //= {}
$entry->{"templates"}
Truth table50%1/2
A
A= $entry->{"templates"}
Aresult
11
00
33  
34 91 my @found_templates = ();
35  
36 331 ▶ $templates_dir->list_tree({ dir => 0 })->each(sub ($file, $idx) {
Subroutine __ANON__: called CC=3 SCAR=11.0
37 # Accept .html.ep, .ep, .txt.ep, etc.
38 331 ▶ return unless $file->basename =~ /\.(?:html?|txt|xml|json)\.ep$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $file->basename =~ /\.(?:html?|txt|xml|json)\.ep$/iu 12 319 331
39  
40 319 my $rel_path = $file->to_rel($templates_dir)->to_string;
41 319 my $template_name = $rel_path;# =~ s/\.ep$//r;
42  
43 319 ▶ $entry->{templates}{$template_name} = {
Condition50%1/2
$file->stat->mtime // 0
l!l
3190
MC/DC0%0/1
$file->stat->mtime // 0
$file->stat->mtime
Truth table50%1/2
A
A= $file->stat->mtime
Aresult
11
00
44 full_path => $file->to_string,
45 rel_path => $rel_path,
46 basename => $file->basename,
47 last_modified => $file->stat->mtime // 0,
48 };
49  
50 319 push @found_templates, $template_name;
51 319 $self->log->debug("Registered template: $template_name");
52 91 });
53  
54 }
55  
56 91 ▶sub _register_zones ($self, $long, $short, $templates_dir) {
Subroutine _register_zones: called CC=3 SCAR=11.0
57 91 my $zone_base = $templates_dir->child('zones');
58 91 ▶ return unless -d $zone_base;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $zone_base 73 18 91
59  
60 18 my $manager = $self->manager;
61 18 my $entry = $manager->registry->{$long};
62  
63 18 ▶ $entry->{zones} //= {};
Condition50%1/2
$entry->{"zones"} //= {}
l!l
018
MC/DC0%0/1
$entry->{"zones"} //= {}
$entry->{"zones"}
Truth table50%1/2
A
A= $entry->{"zones"}
Aresult
11
00
64  
65 30 ▶ $zone_base->list_tree({ dir => 0 })->each(sub ($file, $idx) {
Subroutine __ANON__: called CC=5 SCAR=16.2
66 30 my $basename = $file->basename;
67 30 ▶ return unless $basename =~ /\.(html|js)\.ep$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $basename =~ /\.(html|js)\.ep$/iu 0 30 30
68  
69 30 my $type = lc $1;
70 # Normalize Windows backslashes to forward slashes for regex matching below
71 30 (my $rel = $file->to_rel($templates_dir)->to_string) =~ s{\\}{/}g;
72 30 my ($zone) = $rel =~ m{^zones/$type/(.+)/[^/]+$};
73  
74 30 ▶ $entry->{zones}{$type} //= {};
Condition50%1/2
$entry->{"zones"}{$type} //= {}
l!l
030
MC/DC0%0/1
$entry->{"zones"}{$type} //= {}
$entry->{"zones"}{$type}
Truth table50%1/2
A
A= $entry->{"zones"}{$type}
Aresult
11
00
75 30 ▶ $entry->{zones}{$type}{$zone} //= [];
Condition50%1/2
$entry->{"zones"}{$type}{$zone} //= []
l!l
030
MC/DC0%0/1
$entry->{"zones"}{$type}{$zone} //= []
$entry->{"zones"}{$type}{$zone}
Truth table50%1/2
A
A= $entry->{"zones"}{$type}{$zone}
Aresult
11
00
76  
77 # HTML: store template name for render_to_string
78 # JS: pre-slurp content (no render needed)
79 30 ▶ if ($type eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'html') { } 18 12 30
80 18 my $template_name = $rel =~ s/\.html\.ep$//r;
81 18 push @{$entry->{zones}{$type}{$zone}}, $template_name;
82 }
83 else {
84 12 push @{$entry->{zones}{$type}{$zone}}, $file->slurp;
85 }
86  
87 30 $self->log->debug("Registered zone: $rel ($type/$zone)");
88 18 });
89 }
90  
91 15 1;
92  
93 __END__
« lib/Mojolicious/Plugin/Fondation/Utils.pm ↑ Summary lib/Mojolicious/Plugin/Fondation/TestHelper.pm »