File Coverage

lib/Mojolicious/Plugin/Fondation/Action/Templates.pm
Criterion Covered Total %
statement 74 74 100.0
branch 11 12 91.6
condition 8 15 53.3
subroutine 7 7 100.0
pod 0 1 0.0
total 100 109 91.7


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Fondation::Action::Templates;
2             $Mojolicious::Plugin::Fondation::Action::Templates::VERSION = '0.03';
3             # ABSTRACT: Registers templates and zones from plugin share directories
4              
5 13     13   72 use Mojo::Base 'Mojolicious::Plugin::Fondation::Action::Base', -signatures;
  13         23  
  13         109  
6              
7 13     13   1369 use Mojolicious::Plugin::Fondation::Utils qw(share_relative);
  13         48  
  13         11115  
8              
9 161     161 0 235 sub after_load ($self, $long, $conf, $share_dir) {
  161         238  
  161         231  
  161         187  
  161         205  
  161         177  
10 161 100 66     541 return unless $share_dir && -d $share_dir;
11              
12 139         4533 my $templates_dir = $share_dir->child('templates');
13 139 100       2600 return unless -d $templates_dir;
14              
15 87         1432 my $manager = $self->manager;
16 87         468 my $app = $manager->app;
17 87         337 my $short = $manager->registry->{$long}{short_name};
18              
19             # Add templates directory (priority: "earlier in load_order = higher priority")
20 87         361 push @{$app->renderer->paths}, $templates_dir->to_string;
  87         282  
21 87         1168 $self->log->debug("Added templates path: " . share_relative($templates_dir));
22              
23             # Register templates in the registry
24 87         1246 $self->_register_templates($long, $short, $templates_dir);
25 87         2262 $self->_register_zones($long, $short, $templates_dir);
26             }
27              
28 87     87   163 sub _register_templates ($self, $long, $short, $templates_dir) {
  87         115  
  87         132  
  87         114  
  87         111  
  87         110  
29 87         157 my $manager = $self->manager;
30 87   50     305 my $entry = $manager->registry->{$long} //= {};
31              
32 87   50     718 $entry->{templates} //= {};
33              
34 87         158 my @found_templates = ();
35              
36 183     183   26382 $templates_dir->list_tree({ dir => 0 })->each(sub ($file, $idx) {
  183         268  
  183         246  
  183         216  
37             # Accept .html.ep, .ep, .txt.ep, etc.
38 183 100       512 return unless $file->basename =~ /\.(?:html?|txt|xml|json)\.ep$/i;
39              
40 171         8648 my $rel_path = $file->to_rel($templates_dir)->to_string;
41 171         13910 my $template_name = $rel_path;# =~ s/\.ep$//r;
42              
43 171   50     347 $entry->{templates}{$template_name} = {
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 171         32251 push @found_templates, $template_name;
51 171         551 $self->log->debug("Registered template: $template_name");
52 87         424 });
53              
54             }
55              
56 87     87   133 sub _register_zones ($self, $long, $short, $templates_dir) {
  87         158  
  87         140  
  87         110  
  87         111  
  87         114  
57 87         265 my $zone_base = $templates_dir->child('zones');
58 87 100       1595 return unless -d $zone_base;
59              
60 18         375 my $manager = $self->manager;
61 18         93 my $entry = $manager->registry->{$long};
62              
63 18   50     163 $entry->{zones} //= {};
64              
65 30     30   7562 $zone_base->list_tree({ dir => 0 })->each(sub ($file, $idx) {
  30         52  
  30         38  
  30         36  
66 30         82 my $basename = $file->basename;
67 30 50       1347 return unless $basename =~ /\.(html|js)\.ep$/i;
68              
69 30         83 my $type = lc $1;
70             # Normalize Windows backslashes to forward slashes for regex matching below
71 30         96 (my $rel = $file->to_rel($templates_dir)->to_string) =~ s{\\}{/}g;
72 30         2935 my ($zone) = $rel =~ m{^zones/$type/(.+)/[^/]+$};
73              
74 30   50     185 $entry->{zones}{$type} //= {};
75 30   50     145 $entry->{zones}{$type}{$zone} //= [];
76              
77             # HTML: store template name for render_to_string
78             # JS: pre-slurp content (no render needed)
79 30 100       83 if ($type eq 'html') {
80 18         69 my $template_name = $rel =~ s/\.html\.ep$//r;
81 18         29 push @{$entry->{zones}{$type}{$zone}}, $template_name;
  18         60  
82             }
83             else {
84 12         17 push @{$entry->{zones}{$type}{$zone}}, $file->slurp;
  12         52  
85             }
86              
87 30         1320 $self->log->debug("Registered zone: $rel ($type/$zone)");
88 18         74 });
89             }
90              
91             1;
92              
93             __END__