lib/Mojolicious/Plugin/Fondation.pm

filter: Statementstmt 98.4% 65 / 66 Branchbran 62.5% 5 / 8 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 90.0% 90 / 100 CC 10 SCAR 11.6
CC
10
Cov
92%
__ANON__
6.6
register
3.0
__ANON__
2.1
CRAP 3.2
« lib/Mojolicious/Plugin/Fondation/TestHelper.pm ↑ Summary lib/Mojolicious/Plugin/Fondation/Action/Static.pm »
1 package Mojolicious::Plugin::Fondation;
2 16 $Mojolicious::Plugin::Fondation::VERSION = '0.08';
3 # ABSTRACT: Hierarchical plugin loader with configuration priority and resource sharing
4  
5 16 ▶use Mojo::Base 'Mojolicious::Plugin', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Mojolicious::Plugin::Fondation::Resolver;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Mojolicious::Plugin::Fondation::Manager;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Mojolicious::Plugin::Fondation::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Mojolicious::Plugin::Fondation::Helpers;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Mojo::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Mojolicious::Plugin::Fondation::Utils qw(merge short_name find_share_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 72 ▶sub register ($self, $app, $config = {}) {
Subroutine register: called CC=3 SCAR=11.0
Pod documented
14  
15 my $merged_config = merge(
16 $config,
17 72 ▶ $app->config->{'Fondation'} // {},
Condition100%2/2
$app->config->{"Fondation"} // {}
l!l
963
MC/DC100%1/1
$app->config->{"Fondation"} // {}
$app->config->{"Fondation"}
Truth table100%2/2
A
A= $app->config->{"Fondation"}
Aresult
11
00
18 );
19  
20 72 my $manager = Mojolicious::Plugin::Fondation::Manager->new(
21 app => $app,
22 config => $merged_config,
23 );
24  
25 # API shares the Manager's registry and load_order -- no circular ref
26 72 $manager->{api} = Mojolicious::Plugin::Fondation::API->new(
27 registry => $manager->registry,
28 load_order => $manager->load_order,
29 );
30  
31 # ── Register command namespace ──
32 72 push @{$app->commands->namespaces},
33 'Mojolicious::Plugin::Fondation::Command';
34  
35 # ── Install helper wrapper: first to register wins ──
36 # During plugin loading, $app->helper won't overwrite a helper already
37 # registered by a dependency (loaded earlier). Fondation's own helpers
38 # are registered last, so they only take effect when no plugin provides
39 # an alternative — true fallbacks.
40 72 my $original_helper = $app->can('helper');
41 828 ▶ Mojo::Util::monkey_patch(ref($app), helper => sub ($self, $name, $sub) {
Subroutine __ANON__: called CC=2 SCAR=7.2
Subroutine helper: called CC=2 SCAR=7.2
42 828 ▶ return if exists $self->renderer->helpers->{$name};
Branch: 2 branches
BranchTrueFalseTotal
if exists $self->renderer->helpers->{$name} 0 828 828
if exists $self->renderer->helpers->{$name} 0 297 297
43 828 $original_helper->($self, $name, $sub);
44 72 });
45  
46 # ── Resolve the full dependency graph (with cycle detection) ──
47 72 my $resolver = Mojolicious::Plugin::Fondation::Resolver->new(app => $app);
48 72 my $sorted = $resolver->resolve('Fondation', $merged_config);
49  
50 # ── Pre-register Fondation itself so load_all skips it ──
51 70 my $short = short_name('Mojolicious::Plugin::Fondation');
52 $manager->registry->{'Mojolicious::Plugin::Fondation'} = {
53 instance => $self,
54 short_name => $short,
55 70 share_dir => find_share_dir('Mojolicious::Plugin::Fondation', $merged_config->{share_dir}),
56 config => $merged_config,
57 loaded_at => time,
58 metadata => { has_templates => 0, has_assets => 0 },
59 fondation_meta => $resolver->_discover_meta('Mojolicious::Plugin::Fondation'),
60 };
61 70 push @{$manager->load_order}, 'Mojolicious::Plugin::Fondation';
62  
63 70 $self->{log} = $app->log->context("[$short]");
64  
65 # ── Instantiate all plugins in resolved order ──
66 70 $manager->load_all($sorted);
67  
68 # ── Move Fondation to end of load_order ──
69 # All other plugins' templates/controllers/static are now ahead of
70 # Fondation's, and their helpers have been registered (first wins).
71 # Fondation's helpers only take effect when no plugin provided them.
72 70 my @reordered = grep { $_ ne 'Mojolicious::Plugin::Fondation' } @{$manager->load_order};
73 70 push @reordered, 'Mojolicious::Plugin::Fondation';
74 70 $manager->{load_order} = \@reordered;
75  
76 # ── Register Fondation helpers as true fallbacks ──
77 70 Mojolicious::Plugin::Fondation::Helpers->register($app, $manager);
78  
79 # ── Restore original helper method ──
80 70 Mojo::Util::monkey_patch(ref($app), helper => $original_helper);
81  
82 # ── Post-load actions and finalyze ──
83 70 $manager->run_post_load_actions();
84  
85 70 $manager->run_finalyze();
86  
87 70 $app->plugins->emit_hook(fondation_after_finalyze => $app, $manager);
88  
89 70 $manager->log->info("=== Fondation loaded successfully ===");
90  
91 70 $app->routes->get('/')->to(
92 controller => 'Welcome',
93 action => 'index'
94 );
95  
96 # ── Deferred denial rendering (route conditions must NOT render) ──
97 # Registered LAST so this hook wraps the dispatch directly (after all
98 # plugin around_dispatch hooks, e.g. the async Bearer lookup of
99 # Fondation::Auth::Token). Mojo evaluates route conditions DURING the
100 # route tree walk, before knowing whether the route fully matches
101 # (patterns are anchored at the start only, so a list route like /foo
102 # also prefix-matches /foo/:id). A condition that renders a response
103 # can therefore be evaluated twice in a single request — the second
104 # render dies with "A response has already been rendered" and kills the
105 # worker. Conditions must not render: on failure they set the response
106 # code (so Mojo's not_found is skipped) plus the 'fondation.denied'
107 # stash marker, and return undef. This hook renders the marker ONCE,
108 # after the walk completed (synchronously or inside the async dispatch
109 # continuation).
110 25 ▶ $app->hook(around_dispatch => sub ($next, $c) {
Subroutine __ANON__: called CC=6 SCAR=18.8
111 25 my $result = $next->();
112 24 my $denied = $c->stash->{'fondation.denied'};
113 24 ▶ if ($denied && !$c->stash->{'mojo.rendered'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($denied and not $c->stash->{"mojo.rendered"}) 7 17 24
Condition66%2/3
$denied and not $c->stash->{"mojo.rendered"}
!ll&&!rl&&r
1707
MC/DC50%1/2
$denied and not $c->stash->{"mojo.rendered"}
$denied not $c->stash->{"mojo.rendered"}
Truth table66%2/3
A and not(B)
A= $deniedB= not $c->stash->{"mojo.rendered"}
ABresult
0X0
100
111
114 7 delete $c->stash->{'fondation.denied'};
115 7 ▶ if ($c->has_helper('problem')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->has_helper('problem')) { } 0 7 7
116 0 $c->problem(%$denied);
117 }
118 else {
119 7 ▶ $c->res->code($denied->{status} // 403);
Condition50%1/2
$denied->{"status"} // 403
l!l
70
MC/DC0%0/1
$denied->{"status"} // 403
$denied->{"status"}
Truth table50%1/2
A
A= $denied->{"status"}
Aresult
11
00
120 7 ▶ $c->render(text => $denied->{title} // 'Forbidden');
Condition50%1/2
$denied->{"title"} // "Forbidden"
l!l
70
MC/DC0%0/1
$denied->{"title"} // "Forbidden"
$denied->{"title"}
Truth table50%1/2
A
A= $denied->{"title"}
Aresult
11
00
121 }
122 }
123 24 return $result;
124 70 });
125  
126 70 return $self;
127 }
128  
129 16 1;
130  
131 __END__
« lib/Mojolicious/Plugin/Fondation/TestHelper.pm ↑ Summary lib/Mojolicious/Plugin/Fondation/Action/Static.pm »