« 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
Condition66%2/3
$share_dir and -d $share_dir
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
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 143 | my $templates_dir = $share_dir->child('templates'); | |||||||||||||||||||||||||||
| 13 | 143 | ▶ | return unless -d $templates_dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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} //= {}
MC/DC0%0/1
$manager->registry->{$long} //= {}
$manager->registry->{$long}
Truth table50%1/2
A
A= $manager->registry->{$long}
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 91 | ▶ | $entry->{templates} //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$entry->{"templates"} //= {}
MC/DC0%0/1
$entry->{"templates"} //= {}
$entry->{"templates"}
Truth table50%1/2
A
A= $entry->{"templates"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$file->stat->mtime // 0
$file->stat->mtime
Truth table50%1/2
A
A= $file->stat->mtime
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 18 | my $manager = $self->manager; | |||||||||||||||||||||||||||
| 61 | 18 | my $entry = $manager->registry->{$long}; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 18 | ▶ | $entry->{zones} //= {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$entry->{"zones"} //= {}
MC/DC0%0/1
$entry->{"zones"} //= {}
$entry->{"zones"}
Truth table50%1/2
A
A= $entry->{"zones"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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} //= {}
MC/DC0%0/1
$entry->{"zones"}{$type} //= {}
$entry->{"zones"}{$type}
Truth table50%1/2
A
A= $entry->{"zones"}{$type}
| |||||||||||||||||||||||||||||
| 75 | 30 | ▶ | $entry->{zones}{$type}{$zone} //= []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$entry->{"zones"}{$type}{$zone} //= []
MC/DC0%0/1
$entry->{"zones"}{$type}{$zone} //= []
$entry->{"zones"}{$type}{$zone}
Truth table50%1/2
A
A= $entry->{"zones"}{$type}{$zone}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||