« blib/lib/Mojolicious/Plugin/Fondation/Controller/Welcome.pm
↑ Summary
lib/Mojolicious/Plugin/Fondation/Resolver.pm »
| 1 | package Mojolicious::Plugin::Fondation::Manager; | ||||||||||||||||||||||||||||
| 2 | 16 | $Mojolicious::Plugin::Fondation::Manager::VERSION = '0.08'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Plugin registry, post-load actions, and finalyze initialization | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use Mojo::Base -base, -signatures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 16 | ▶ | use Mojolicious::Plugin::Fondation::Utils qw(find_share_dir share_relative short_name); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 16 | has 'app'; | |||||||||||||||||||||||||||
| 9 | 16 | has 'config'; | |||||||||||||||||||||||||||
| 10 | 16 | has 'api'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 72 | ▶ | has registry => sub { {} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 72 | ▶ | has load_order => sub { [] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | has fixture_sets => sub { [] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 15 | 16 | has log => undef; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 70 | ▶ | has action_classes => sub ($self) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 18 | 70 | ▶ | my $short_list = $self->config->{actions} // ['Templates', 'Controllers', 'Static']; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->config->{"actions"} // ["Templates", "Controllers", "Static"]
MC/DC100%1/1
$self->config->{"actions"} // ["Templates", "Controllers", "Static"]
$self->config->{"actions"}
Truth table100%2/2
A
A= $self->config->{"actions"}
| |||||||||||||||||||||||||||||
| 19 | 70 | my %seen = map { $_ => 1 } @$short_list; | |||||||||||||||||||||||||||
| 20 | 70 | my @all = @$short_list; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 70 | for my $long (@{$self->load_order}) { | |||||||||||||||||||||||||||
| 23 | 165 | ▶ | my $meta = $self->registry->{$long}{fondation_meta} // {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->registry->{$long}{"fondation_meta"} // {}
MC/DC0%0/1
$self->registry->{$long}{"fondation_meta"} // {}
$self->registry->{$long}{"fondation_meta"}
Truth table50%1/2
A
A= $self->registry->{$long}{"fondation_meta"}
| |||||||||||||||||||||||||||||
| 24 | 165 | ▶ | for my $short (@{ $meta->{provides_actions} // [] }) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$meta->{"provides_actions"} // []
MC/DC100%1/1
$meta->{"provides_actions"} // []
$meta->{"provides_actions"}
Truth table100%2/2
A
A= $meta->{"provides_actions"}
| |||||||||||||||||||||||||||||
| 25 | 5 | ▶ | next if $seen{$short}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 4 | push @all, $short; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 70 | return [ map { $self->_resolve_action_class($_) } @all ]; | |||||||||||||||||||||||||||
| 31 | 16 | }; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 34 | # load_all -- instantiate plugins from a pre-sorted list (from Resolver) | ||||||||||||||||||||||||||||
| 35 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 36 | 70 | ▶ | sub load_all ($self, $sorted_specs) { | ||||||||||||||||||||||||||
|
Subroutine
load_all: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 70 | ▶ | $self->{log} //= $self->app->log->context('[Fondation]'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"log"} //= $self->app->log->context("[Fondation]")
MC/DC0%0/2
$self->{"log"} //= $self->app->log->context("[Fondation]")
$self->{"log"}
$self->app->log->context("[Fondation]")
Truth table33%1/3
A //= B
A= $self->{"log"}B= $self->app->log->context("[Fondation]")
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 70 | for my $spec (@$sorted_specs) { | |||||||||||||||||||||||||||
| 40 | 165 | my $long = $spec->{long}; | |||||||||||||||||||||||||||
| 41 | 165 | my $short = $spec->{short}; | |||||||||||||||||||||||||||
| 42 | 165 | my $merged = $spec->{config}; | |||||||||||||||||||||||||||
| 43 | 165 | my $meta = $spec->{meta}; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 165 | ▶ | next if $self->registry->{$long}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Fondation itself is registered by the caller | ||||||||||||||||||||||||||||
| 48 | 95 | ▶ | next if $long eq 'Mojolicious::Plugin::Fondation'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 95 | $self->log->debug("Loading plugin $short"); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 95 | my $instance = $self->_prepare_and_register($long, $short, $merged); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 95 | my $share_dir = find_share_dir($long, $merged->{share_dir}); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 95 | $self->registry->{$long} = { | |||||||||||||||||||||||||||
| 57 | instance => $instance, | ||||||||||||||||||||||||||||
| 58 | short_name => $short, | ||||||||||||||||||||||||||||
| 59 | share_dir => $share_dir, | ||||||||||||||||||||||||||||
| 60 | config => $merged, | ||||||||||||||||||||||||||||
| 61 | loaded_at => time, | ||||||||||||||||||||||||||||
| 62 | metadata => { has_templates => 0, has_assets => 0 }, | ||||||||||||||||||||||||||||
| 63 | fondation_meta => $meta, | ||||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 95 | push @{$self->load_order}, $long; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 95 | ▶ | sub _prepare_and_register ($self, $long, $short, $merged) { | ||||||||||||||||||||||||||
|
Subroutine
_prepare_and_register: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 71 | 95 | my $app = $self->app; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # 1. Loading the Instance (without Register) | ||||||||||||||||||||||||||||
| 74 | 95 | my $instance = $app->plugins->load_plugin($long); | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # 2. Injection of the contextual logger before register | ||||||||||||||||||||||||||||
| 77 | 95 | $instance->{log} = $app->log->context("[$short]"); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # 3. Inject the ->log method if it doesn't exist | ||||||||||||||||||||||||||||
| 80 | 95 | ▶ | unless ($instance->can('log')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | Mojo::Util::monkey_patch(ref($instance), log => sub { shift->{log} }); | ||||||||||||||||||||||||||
|
Subroutine
log: called CC=1 SCAR=0.4
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # 4. Calling the register (the logger is now available)disponible) | ||||||||||||||||||||||||||||
| 85 | 95 | $instance->register($app, $merged); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 95 | return $instance; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 210 | ▶ | sub _resolve_action_class ($self, $short) { | ||||||||||||||||||||||||||
|
Subroutine
_resolve_action_class: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 91 | 210 | ▶ | return $short if $short =~ /^Mojolicious::/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 210 | for my $long (@{$self->load_order}) { | |||||||||||||||||||||||||||
| 94 | 492 | ▶ | my $meta = $self->registry->{$long}{fondation_meta} // {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->registry->{$long}{"fondation_meta"} // {}
MC/DC0%0/1
$self->registry->{$long}{"fondation_meta"} // {}
$self->registry->{$long}{"fondation_meta"}
Truth table50%1/2
A
A= $self->registry->{$long}{"fondation_meta"}
| |||||||||||||||||||||||||||||
| 95 | 492 | ▶ | my $provides = $meta->{provides_actions} // []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$meta->{"provides_actions"} // []
MC/DC100%1/1
$meta->{"provides_actions"} // []
$meta->{"provides_actions"}
Truth table100%2/2
A
A= $meta->{"provides_actions"}
| |||||||||||||||||||||||||||||
| 96 | return "${long}::Action::${short}" | ||||||||||||||||||||||||||||
| 97 | 492 | ▶ | if grep { $_ eq $short } @$provides; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 205 | return "Mojolicious::Plugin::Fondation::Action::${short}"; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 165 | ▶ | sub _run_post_load_actions_for ($self, $long, $conf = {}) { | ||||||||||||||||||||||||||
|
Subroutine
_run_post_load_actions_for: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 103 | 165 | my $plugin = $self->registry->{$long}{instance}; | |||||||||||||||||||||||||||
| 104 | 165 | ▶ | return unless $plugin && ref $plugin; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$plugin and ref $plugin
MC/DC0%0/2
$plugin and ref $plugin
$plugin
ref $plugin
Truth table33%1/3
A and B
A= $pluginB= ref $plugin
| |||||||||||||||||||||||||||||
| 105 | 165 | my $share_dir = $self->registry->{$long}{share_dir}; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 165 | for my $action_class (@{$self->action_classes}) { | |||||||||||||||||||||||||||
| 108 | 498 | ▶ | eval "require $action_class; 1" or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 4 | $self->log->warn("Action $action_class not loaded: $@"); | |||||||||||||||||||||||||||
| 110 | 4 | next; | |||||||||||||||||||||||||||
| 111 | }; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 494 | my ($action_short) = $action_class =~ /::Action::(.+)$/; | |||||||||||||||||||||||||||
| 114 | 494 | my $shotname = short_name($long); | |||||||||||||||||||||||||||
| 115 | 494 | my $action_log = $self->app->log->context("[$shotname > $action_short]"); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 494 | my $action = $action_class->new( | |||||||||||||||||||||||||||
| 118 | manager => $self, | ||||||||||||||||||||||||||||
| 119 | log => $action_log, | ||||||||||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||||||||||
| 121 | 494 | $action->after_load($long, $conf, $share_dir); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 70 | ▶ | sub run_post_load_actions ($self) { | ||||||||||||||||||||||||||
|
Subroutine
run_post_load_actions: called CC=6 SCAR=18.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 126 | 70 | my $count = scalar @{$self->action_classes}; | |||||||||||||||||||||||||||
| 127 | 70 | $self->log->debug("Running post-load actions for all plugins ($count configured)"); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 70 | for my $long (@{$self->load_order}) { | |||||||||||||||||||||||||||
| 130 | 165 | $self->_run_post_load_actions_for($long); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # Add local 'share/templates' | ||||||||||||||||||||||||||||
| 134 | 70 | my $app_templates = $self->app->home->child('share', 'templates'); | |||||||||||||||||||||||||||
| 135 | 70 | ▶ | if (-d $app_templates) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 1 | unshift @{$self->app->renderer->paths}, $app_templates->to_string; | |||||||||||||||||||||||||||
| 137 | 1 | $self->log->debug("App templates priority: " . share_relative($app_templates)); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # Add local 'public' | ||||||||||||||||||||||||||||
| 141 | 70 | my $app_public = $self->app->home->child('public'); | |||||||||||||||||||||||||||
| 142 | 70 | ▶ | if (-d $app_public) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 1 | unshift @{$self->app->static->paths}, $app_public->to_string; | |||||||||||||||||||||||||||
| 144 | 1 | $self->log->debug("App public priority: " . share_relative($app_public)); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # Add local controller namespace (Mojolicious::Lite clears it). | ||||||||||||||||||||||||||||
| 148 | # Only when the app actually has local controllers. | ||||||||||||||||||||||||||||
| 149 | 70 | my $moniker = $self->app->moniker; | |||||||||||||||||||||||||||
| 150 | 70 | my $local_ns = Mojo::Util::camelize($moniker); | |||||||||||||||||||||||||||
| 151 | 70 | my $ctrl_path = $local_ns =~ s{::}{/}gr; | |||||||||||||||||||||||||||
| 152 | 70 | my $local_ctrl_dir = $self->app->home->child('lib', split('/', $ctrl_path), 'Controller'); | |||||||||||||||||||||||||||
| 153 | 70 | ▶ | if (-d $local_ctrl_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | my $ns = $self->app->routes->namespaces; | |||||||||||||||||||||||||||
| 155 | 0 | my $ctrl_ns = "${local_ns}::Controller"; | |||||||||||||||||||||||||||
| 156 | 0 | ▶ | unless (grep { $_ eq $ctrl_ns } @$ns) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | unshift @$ns, $ctrl_ns; | |||||||||||||||||||||||||||
| 158 | 0 | $self->log->debug("App controller namespace: $ctrl_ns"); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 70 | ▶ | sub run_finalyze ($self) { | ||||||||||||||||||||||||||
|
Subroutine
run_finalyze: called CC=8 SCAR=27.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 164 | 70 | $self->log->debug("Running fondation_finalyze in loading order"); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # If schema_class is configured but MySchema.pm doesn't exist yet, | ||||||||||||||||||||||||||||
| 167 | # skip all finalyze — plugins with DBIC dependencies would crash. | ||||||||||||||||||||||||||||
| 168 | # db bootstrap-schema (a CLI command) runs before finalyze and | ||||||||||||||||||||||||||||
| 169 | # creates the file; the next restart will pass this check. | ||||||||||||||||||||||||||||
| 170 | 70 | my $c = $self->app->build_controller; | |||||||||||||||||||||||||||
| 171 | 70 | ▶ | if ($c->has_helper('schema_class')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | my $sc = eval { $c->schema_class }; | |||||||||||||||||||||||||||
| 173 | 0 | ▶ | if ($sc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | eval "require $sc; 1" or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 0 | $self->log->warn( | |||||||||||||||||||||||||||
| 176 | "Schema '$sc' not found. " | ||||||||||||||||||||||||||||
| 177 | . "Run 'db bootstrap-schema' first. " | ||||||||||||||||||||||||||||
| 178 | . "Skipping all fondation_finalyze."); | ||||||||||||||||||||||||||||
| 179 | 0 | return; | |||||||||||||||||||||||||||
| 180 | }; | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 70 | for my $long (@{$self->load_order}) { | |||||||||||||||||||||||||||
| 185 | 165 | my $entry = $self->registry->{$long}; | |||||||||||||||||||||||||||
| 186 | 165 | my $plugin = $entry->{instance}; | |||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 165 | ▶ | if (! defined $plugin) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 0 | $self->log->info("Skipped finalyze for $long"); | |||||||||||||||||||||||||||
| 190 | 0 | next; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 165 | ▶ | if ($plugin->can('fondation_finalyze')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | eval { | ||||||||||||||||||||||||||||
| 195 | 27 | $plugin->fondation_finalyze($self->app, $long); | |||||||||||||||||||||||||||
| 196 | 27 | 1; | |||||||||||||||||||||||||||
| 197 | 27 | ▶ | } or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | die "Error in fondation_finalyze for $long: $@"; | |||||||||||||||||||||||||||
| 199 | }; | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 16 | 1; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||||||||