« lib/Mojolicious/Plugin/Fondation/Manager.pm
↑ Summary
lib/Mojolicious/Plugin/Fondation/Helpers.pm »
| 1 | package Mojolicious::Plugin::Fondation::Resolver; | ||||||||||||||||||||||||||||
| 2 | 16 | $Mojolicious::Plugin::Fondation::Resolver::VERSION = '0.08'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Dependency graph resolver with cycle detection and topological sort | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use Mojo::Base -base, -signatures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 16 | ▶ | use Mojolicious::Plugin::Fondation::Utils qw(long_name short_name merge); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 16 | has 'app'; | |||||||||||||||||||||||||||
| 9 | 0 | ▶ | has 'states' => sub { {} }; # $long_name => 'visiting' | 'visited' | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 10 | 0 | ▶ | has 'result' => sub { [] }; # topologically sorted specs | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 13 | # resolve -- entry point | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # Returns an arrayref of plugin specs in topological order (deps first). | ||||||||||||||||||||||||||||
| 16 | # Each spec: { long => $long, short => $short, config => $merged, meta => $meta } | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | # Dies on dependency cycles with a clear message. | ||||||||||||||||||||||||||||
| 19 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 20 | 86 | ▶ | sub resolve ($self, $name_or_short, $direct_conf = {}) { | ||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 86 | my $long = long_name($name_or_short); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Reset internal state for fresh resolution | ||||||||||||||||||||||||||||
| 24 | 86 | $self->{states} = {}; | |||||||||||||||||||||||||||
| 25 | 86 | $self->{result} = []; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # ── Dev plugin discovery: scan dev_plugins_dir for Mojolicious-Plugin-Fondation-*/lib ── | ||||||||||||||||||||||||||||
| 28 | 86 | ▶ | if (my $dev_root = $direct_conf->{dev_plugins_dir}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 1 | $self->_add_dev_plugins_to_inc($dev_root); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Phase 1 — Discover all plugins via the original dependency graph | ||||||||||||||||||||||||||||
| 33 | 86 | my $graph = {}; | |||||||||||||||||||||||||||
| 34 | 86 | $self->_discover_graph($long, $direct_conf, $graph); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Phase 2 — Augment with before / after ordering hints | ||||||||||||||||||||||||||||
| 37 | 86 | $self->_augment_graph($graph); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Phase 3 — Topological sort on the augmented graph | ||||||||||||||||||||||||||||
| 40 | 86 | $self->{states} = {}; | |||||||||||||||||||||||||||
| 41 | 86 | $self->_sort($long, $graph); | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 80 | return $self->{result}; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 47 | # Phase 1 — _discover_graph: DFS to collect every reachable plugin. | ||||||||||||||||||||||||||||
| 48 | # Builds a flat map: $long => { deps => [...], config => {...}, meta => {...} } | ||||||||||||||||||||||||||||
| 49 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 50 | 223 | ▶ | sub _discover_graph ($self, $long, $direct_conf, $graph) { | ||||||||||||||||||||||||||
|
Subroutine
_discover_graph: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||
| 51 | 223 | ▶ | return if $graph->{$long}; # already discovered | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 197 | my $meta = $self->_discover_meta($long); | |||||||||||||||||||||||||||
| 54 | 197 | my $short = short_name($long); | |||||||||||||||||||||||||||
| 55 | my $merged = merge( | ||||||||||||||||||||||||||||
| 56 | $direct_conf, | ||||||||||||||||||||||||||||
| 57 | $self->app->config->{$short} // {}, | ||||||||||||||||||||||||||||
| 58 | $meta->{defaults} // {} | ||||||||||||||||||||||||||||
| 59 | 197 | ▶ | ); | ||||||||||||||||||||||||||
|
Condition75%3/4
$self->app->config->{$short} // {}
$meta->{"defaults"} // {}
MC/DC100%1/1
$self->app->config->{$short} // {}
$self->app->config->{$short}
MC/DC0%0/1
$meta->{"defaults"} // {}
$meta->{"defaults"}
Truth table100%2/2
A
A= $self->app->config->{$short}
Truth table50%1/2
A
A= $meta->{"defaults"}
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 197 | ▶ | my $deps = $merged->{dependencies} // $meta->{dependencies} // []; | ||||||||||||||||||||||||||
|
Condition60%3/5
($merged->{"dependencies"} // $meta->{"dependencies"}) // []
$merged->{"dependencies"} // $meta->{"dependencies"}
MC/DC0%0/2
($merged->{"dependencies"} // $meta->{"dependencies"}) // []
$merged->{"dependencies"}
$meta->{"dependencies"}
Truth table66%2/3
A // B
A= $merged->{"dependencies"}B= $meta->{"dependencies"}
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 197 | $graph->{$long} = { | |||||||||||||||||||||||||||
| 64 | deps => [@$deps], # shallow copy of spec list | ||||||||||||||||||||||||||||
| 65 | config => $merged, | ||||||||||||||||||||||||||||
| 66 | meta => $meta, | ||||||||||||||||||||||||||||
| 67 | }; | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # Recurse into declared dependencies | ||||||||||||||||||||||||||||
| 70 | 197 | for my $dep_spec (@$deps) { | |||||||||||||||||||||||||||
| 71 | 137 | my ($dep_name, $dep_conf) = $self->_parse_dep($dep_spec); | |||||||||||||||||||||||||||
| 72 | 137 | $self->_discover_graph(long_name($dep_name), $dep_conf, $graph); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 77 | # Phase 2 — _augment_graph: add edges from before / after declarations. | ||||||||||||||||||||||||||||
| 78 | # | ||||||||||||||||||||||||||||
| 79 | # after => ['B'] → B must load before me → B is a dependency of me | ||||||||||||||||||||||||||||
| 80 | # before => ['B'] → I must load before B → I am a dependency of B | ||||||||||||||||||||||||||||
| 81 | # | ||||||||||||||||||||||||||||
| 82 | # Silently ignored when the target plugin is not in the graph. | ||||||||||||||||||||||||||||
| 83 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 84 | 86 | ▶ | sub _augment_graph ($self, $graph) { | ||||||||||||||||||||||||||
|
Subroutine
_augment_graph: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||
| 85 | 86 | for my $long (keys %$graph) { | |||||||||||||||||||||||||||
| 86 | 197 | my $entry = $graph->{$long}; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # after — the named plugin should be loaded before me | ||||||||||||||||||||||||||||
| 89 | 197 | ▶ | my $after = $entry->{config}{after} // $entry->{meta}{after} // []; | ||||||||||||||||||||||||||
|
Condition80%4/5
($entry->{"config"}{"after"} // $entry->{"meta"}{"after"}) // []
$entry->{"config"}{"after"} // $entry->{"meta"}{"after"}
MC/DC50%1/2
($entry->{"config"}{"after"} // $entry->{"meta"}{"after"}) // []
$entry->{"config"}{"after"}
$entry->{"meta"}{"after"}
Truth table66%2/3
A // B
A= $entry->{"config"}{"after"}B= $entry->{"meta"}{"after"}
| |||||||||||||||||||||||||||||
| 90 | 197 | for my $target_short (@$after) { | |||||||||||||||||||||||||||
| 91 | 2 | my $target = long_name($target_short); | |||||||||||||||||||||||||||
| 92 | 2 | ▶ | next unless $graph->{$target}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 2 | push @{$entry->{deps}}, $target; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # before — I should be loaded before the named plugin | ||||||||||||||||||||||||||||
| 97 | 197 | ▶ | my $before = $entry->{config}{before} // $entry->{meta}{before} // []; | ||||||||||||||||||||||||||
|
Condition80%4/5
($entry->{"config"}{"before"} // $entry->{"meta"}{"before"}) // []
$entry->{"config"}{"before"} // $entry->{"meta"}{"before"}
MC/DC50%1/2
($entry->{"config"}{"before"} // $entry->{"meta"}{"before"}) // []
$entry->{"config"}{"before"}
$entry->{"meta"}{"before"}
Truth table66%2/3
A // B
A= $entry->{"config"}{"before"}B= $entry->{"meta"}{"before"}
| |||||||||||||||||||||||||||||
| 98 | 197 | for my $target_short (@$before) { | |||||||||||||||||||||||||||
| 99 | 5 | my $target = long_name($target_short); | |||||||||||||||||||||||||||
| 100 | 5 | ▶ | next unless $graph->{$target}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 4 | push @{$graph->{$target}{deps}}, $long; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 107 | # Phase 3 — _sort: topological sort via DFS with 3-state cycle detection. | ||||||||||||||||||||||||||||
| 108 | # | ||||||||||||||||||||||||||||
| 109 | # Cycles caused by conflicting before/after are caught here just like | ||||||||||||||||||||||||||||
| 110 | # dependency cycles. | ||||||||||||||||||||||||||||
| 111 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 112 | 228 | ▶ | sub _sort ($self, $long, $graph) { | ||||||||||||||||||||||||||
|
Subroutine
_sort: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 113 | 228 | my $state = $self->states->{$long}; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 228 | ▶ | if (defined $state && $state eq 'visiting') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $state and $state eq "visiting"
MC/DC100%2/2
defined $state and $state eq "visiting"
defined $state
$state eq "visiting"
Truth table100%3/3
A and B
A= defined $stateB= $state eq "visiting"
| |||||||||||||||||||||||||||||
| 116 | 6 | die "Dependency cycle detected: $long is part of a circular dependency chain.\n"; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 222 | ▶ | return if defined $state && $state eq 'visited'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $state and $state eq "visited"
MC/DC50%1/2
defined $state and $state eq "visited"
defined $state
$state eq "visited"
Truth table66%2/3
A and B
A= defined $stateB= $state eq "visited"
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 197 | $self->states->{$long} = 'visiting'; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 197 | my $entry = $graph->{$long}; | |||||||||||||||||||||||||||
| 123 | 197 | for my $dep_spec (@{$entry->{deps}}) { | |||||||||||||||||||||||||||
| 124 | 142 | my ($dep_name) = $self->_parse_dep($dep_spec); | |||||||||||||||||||||||||||
| 125 | 142 | $self->_sort(long_name($dep_name), $graph); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 183 | $self->states->{$long} = 'visited'; | |||||||||||||||||||||||||||
| 129 | 183 | push @{$self->result}, { | |||||||||||||||||||||||||||
| 130 | long => $long, | ||||||||||||||||||||||||||||
| 131 | short => short_name($long), | ||||||||||||||||||||||||||||
| 132 | config => $entry->{config}, | ||||||||||||||||||||||||||||
| 133 | meta => $entry->{meta}, | ||||||||||||||||||||||||||||
| 134 | 183 | }; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 138 | # _parse_dep -- normalize a dependency spec (string or hashref). | ||||||||||||||||||||||||||||
| 139 | # Returns ($name, $conf). | ||||||||||||||||||||||||||||
| 140 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 141 | 279 | ▶ | sub _parse_dep ($self, $dep_spec) { | ||||||||||||||||||||||||||
|
Subroutine
_parse_dep: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 142 | 279 | ▶ | if (ref $dep_spec eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 106 | my ($name) = keys %$dep_spec; | |||||||||||||||||||||||||||
| 144 | 106 | ▶ | return ($name, $dep_spec->{$name} // {}); | ||||||||||||||||||||||||||
|
Condition50%1/2
$dep_spec->{$name} // {}
MC/DC0%0/1
$dep_spec->{$name} // {}
$dep_spec->{$name}
Truth table50%1/2
A
A= $dep_spec->{$name}
| |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | 173 | return ($dep_spec, {}); | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 150 | # _add_dev_plugins_to_inc — scan dev_plugins_dir for plugin repos and add | ||||||||||||||||||||||||||||
| 151 | # their lib/ directories to @INC (before installed modules, so dev wins). | ||||||||||||||||||||||||||||
| 152 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 153 | 1 | ▶ | sub _add_dev_plugins_to_inc ($self, $dev_root) { | ||||||||||||||||||||||||||
|
Subroutine
_add_dev_plugins_to_inc: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 154 | 1 | my @dirs = sort glob("$dev_root/Mojolicious-Plugin-Fondation-*"); | |||||||||||||||||||||||||||
| 155 | 1 | for my $dir (@dirs) { | |||||||||||||||||||||||||||
| 156 | 1 | my $lib = "$dir/lib"; | |||||||||||||||||||||||||||
| 157 | 1 | ▶ | next unless -d $lib; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 1 | unshift @INC, $lib; | |||||||||||||||||||||||||||
| 159 | 1 | $self->app->log->debug("Dev plugin lib added to \@INC: $lib"); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 164 | # _discover_meta -- load fondation_meta from a plugin class without | ||||||||||||||||||||||||||||
| 165 | # instantiating it (i.e. without calling register). | ||||||||||||||||||||||||||||
| 166 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 167 | 267 | ▶ | sub _discover_meta ($self, $long_name) { | ||||||||||||||||||||||||||
|
Subroutine
_discover_meta: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 168 | 267 | my $class = $long_name; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # If the module is already loaded in %INC, do not reload it | ||||||||||||||||||||||||||||
| 171 | 267 | my $pm_file = $class =~ s{::}{/}gr . '.pm'; | |||||||||||||||||||||||||||
| 172 | 267 | ▶ | if ($INC{$pm_file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 216 | ▶ | return $class->can('fondation_meta') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | ? $class->fondation_meta | ||||||||||||||||||||||||||||
| 175 | : { dependencies => [], defaults => {} }; | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 51 | my $err = Mojo::Loader::load_class($class); | |||||||||||||||||||||||||||
| 179 | 51 | ▶ | if ($err) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 0 | $self->app->log->warn("Resolver: could not load $class -- $err"); | |||||||||||||||||||||||||||
| 181 | 0 | return { dependencies => [], defaults => {} }; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 51 | ▶ | return $class->can('fondation_meta') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | ? $class->fondation_meta | ||||||||||||||||||||||||||||
| 186 | : { dependencies => [], defaults => {} }; | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 16 | 1; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | __END__ | ||||||||||||||||||||||||||||