| 1 | package MooX::PluginKit::Core; | ||||||||||||||||||||
| 2 | 5 | ▶ | use 5.008001; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 5 | ▶ | use strictures 2; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 5 | our $VERSION = '0.06'; | |||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | MooX::PluginKit::Core - The PluginKit internal guts. | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | =head2 DESCRIPTION | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | This module tracks metadata about consumers and plugins as well as | ||||||||||||||||||||
| 13 | providing much of the underlying logic behind the other PluginKit | ||||||||||||||||||||
| 14 | modules. | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | Currently this module is not documented because it is not intended | ||||||||||||||||||||
| 17 | to be used directly. This may change. | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 5 | ▶ | use Carp qw( croak ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 5 | ▶ | use Moo::Role qw(); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 5 | ▶ | use Module::Runtime qw( require_module is_module_name ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | require UNIVERSAL::DOES | ||||||||||||||||||||
| 26 | 5 | ▶ | unless defined &UNIVERSAL::DOES; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 5 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 5 | ▶ | use Exporter qw( import ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 5 | our @EXPORT = qw( | |||||||||||||||||||
| 33 | init_plugin | ||||||||||||||||||||
| 34 | is_plugin | ||||||||||||||||||||
| 35 | resolve_plugin | ||||||||||||||||||||
| 36 | does_plugin_apply | ||||||||||||||||||||
| 37 | find_applicable_plugins | ||||||||||||||||||||
| 38 | build_class_with_plugins | ||||||||||||||||||||
| 39 | set_plugin_applies_to | ||||||||||||||||||||
| 40 | get_plugin_applies_to | ||||||||||||||||||||
| 41 | set_plugin_includes | ||||||||||||||||||||
| 42 | get_plugin_includes | ||||||||||||||||||||
| 43 | init_consumer | ||||||||||||||||||||
| 44 | is_consumer | ||||||||||||||||||||
| 45 | get_consumer_moo_extends | ||||||||||||||||||||
| 46 | get_consumer_moo_with | ||||||||||||||||||||
| 47 | get_consumer_moo_has | ||||||||||||||||||||
| 48 | set_consumer_namespace | ||||||||||||||||||||
| 49 | get_consumer_namespace | ||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 5 | my %plugins; # Metadata about roles. | |||||||||||||||||||
| 53 | my %consumers; # Metadata about classes. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 16 | ▶ | sub init_plugin { | ||||||||||||||||||
|
Subroutine
init_plugin: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 56 | 16 | my ($plugin_name) = @_; | |||||||||||||||||||
| 57 | 16 | $plugins{$plugin_name} = {}; | |||||||||||||||||||
| 58 | 16 | return; | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 0 | ▶ | sub is_plugin { | ||||||||||||||||||
|
Subroutine
is_plugin: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 62 | 0 | my ($plugin_name) = @_; | |||||||||||||||||||
| 63 | 0 | ▶ | return $plugins{$plugin_name} ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 22 | ▶ | sub resolve_plugin { | ||||||||||||||||||
|
Subroutine
resolve_plugin: called CC=8 SCAR=23.7
Pod
undocumented
| |||||||||||||||||||||
| 67 | 22 | my ($plugin_name, $namespace) = @_; | |||||||||||||||||||
| 68 | 22 | local $Carp::Internal{ (__PACKAGE__) } = 1; | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 22 | ▶ | croak "An undefined plugin name cannot be resolved" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | if !defined $plugin_name; | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 22 | ▶ | if ($plugin_name =~ m{^::}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 8 | ▶ | croak "The relative plugin $plugin_name cannot be made absolute without a namespace" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | if !defined $namespace; | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 8 | $plugin_name = $namespace . $plugin_name; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 22 | ▶ | croak "The plugin $plugin_name does not appear to be a valid module name" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | if !is_module_name( $plugin_name ); | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 22 | ▶ | return $plugin_name if exists $plugins{$plugin_name}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | # Go ahead and shortcircuit here as Moo::Role does not add inlined packages into | ||||||||||||||||||||
| 86 | # %INC so the require_module() call could fail in some cases if the module has already | ||||||||||||||||||||
| 87 | # been setup but isn't on the filesystem in the expected locations. | ||||||||||||||||||||
| 88 | 14 | ▶ | return $plugin_name if Moo::Role->is_role( $plugin_name ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | require_module( $plugin_name ); | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | ▶ | croak "Plugin $plugin_name does not appear to be a Moo::Role" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | if !Moo::Role->is_role( $plugin_name ); | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 0 | return $plugin_name; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 73 | ▶ | sub does_plugin_apply { | ||||||||||||||||||
|
Subroutine
does_plugin_apply: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 99 | 73 | my ($plugin_name, $class) = @_; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 73 | my $sub = get_plugin_applies_to( $plugin_name ); | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 73 | ▶ | return $sub->( $class ) ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 39 | ▶ | sub find_applicable_plugins { | ||||||||||||||||||
|
Subroutine
find_applicable_plugins: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 107 | 39 | my ($class, @plugins) = @_; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 39 | my @final_plugins; | |||||||||||||||||||
| 110 | 39 | ▶ | while (@plugins) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | 45 | my $plugin = shift( @plugins ); | |||||||||||||||||||
| 112 | 45 | ▶ | next if !does_plugin_apply( $plugin, $class ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | 38 | push @final_plugins, $plugin; | |||||||||||||||||||
| 114 | 38 | unshift @plugins, @{ get_plugin_includes( $plugin ) }; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 39 | return \@final_plugins; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 39 | ▶ | sub build_class_with_plugins { | ||||||||||||||||||
|
Subroutine
build_class_with_plugins: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 121 | 39 | my ($base_class, @plugins) = @_; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 39 | my $roles = find_applicable_plugins( $base_class, @plugins ); | |||||||||||||||||||
| 124 | 39 | ▶ | return $base_class if !@$roles; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 19 | return Moo::Role->create_class_with_roles( | |||||||||||||||||||
| 127 | $base_class, | ||||||||||||||||||||
| 128 | @$roles, | ||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 11 | ▶ | sub set_plugin_applies_to { | ||||||||||||||||||
|
Subroutine
set_plugin_applies_to: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 133 | 11 | my ($plugin_name, $sub) = @_; | |||||||||||||||||||
| 134 | 11 | my $plugin = $plugins{$plugin_name}; | |||||||||||||||||||
| 135 | 11 | local $Carp::Internal{ (__PACKAGE__) } = 1; | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | croak "The applies_to for the $plugin_name plugin has already been set" | ||||||||||||||||||||
| 138 | 11 | ▶ | if exists $plugin->{applies_to}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 11 | ▶ | if (!ref $sub) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 141 | 6 | my $package = $sub; | |||||||||||||||||||
| 142 | 6 | ▶ | $sub = sub{ $_[0]->isa( $package ) or $_[0]->DOES( $package ) }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | elsif (ref($sub) eq 'ARRAY') { | ||||||||||||||||||||
| 145 | 1 | my $methods = $sub; | |||||||||||||||||||
| 146 | 4 | ▶ | $sub = sub{ | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 147 | 4 | foreach my $method (@$methods) { | |||||||||||||||||||
| 148 | 5 | ▶ | next if $_[0]->can($method); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 149 | 3 | return 0; | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | 1 | return 1; | |||||||||||||||||||
| 152 | 1 | }; | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | elsif (ref($sub) eq 'Regexp') { | ||||||||||||||||||||
| 155 | 1 | my $re = $sub; | |||||||||||||||||||
| 156 | 4 | ▶ | $sub = sub{ | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 157 | 4 | ▶ | return ($_[0] =~ $re) ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | 1 | }; | |||||||||||||||||||
| 159 | } | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 11 | ▶ | croak 'Plugin applies_to must be a class name, arrayref of methods, regex, or code ref' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 162 | if ref($sub) ne 'CODE'; | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 11 | $plugin->{applies_to} = $sub; | |||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 11 | return; | |||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 73 | ▶ | sub get_plugin_applies_to { | ||||||||||||||||||
|
Subroutine
get_plugin_applies_to: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 170 | 73 | my ($plugin_name) = @_; | |||||||||||||||||||
| 171 | 73 | my $plugin = $plugins{$plugin_name}; | |||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 73 | ▶ | return $plugin->{applies_to} || sub{ 1 }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$plugin->{'applies_to'} || sub {
1;
}
MC/DC0%0/2
$plugin->{'applies_to'} || sub {
1;
}
$plugin->{'applies_to'}
sub {
1;
}
Truth table66%2/3
A || B
A= $plugin->{'applies_to'}B= sub {
1;
}
| |||||||||||||||||||||
| 174 | } | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | 6 | ▶ | sub set_plugin_includes { | ||||||||||||||||||
|
Subroutine
set_plugin_includes: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 177 | 6 | my ($plugin_name, @includes) = @_; | |||||||||||||||||||
| 178 | 6 | my $plugin = $plugins{$plugin_name}; | |||||||||||||||||||
| 179 | 6 | local $Carp::Internal{ (__PACKAGE__) } = 1; | |||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | croak "The includes for the $plugin_name plugin has already been set" | ||||||||||||||||||||
| 182 | 6 | ▶ | if exists $plugin->{includes}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | $plugin->{includes} = [ | ||||||||||||||||||||
| 185 | 6 | map { resolve_plugin($_, $plugin_name) } | |||||||||||||||||||
| 186 | @includes | ||||||||||||||||||||
| 187 | ]; | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 6 | return; | |||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | 38 | ▶ | sub get_plugin_includes { | ||||||||||||||||||
|
Subroutine
get_plugin_includes: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 193 | 38 | my ($plugin_name) = @_; | |||||||||||||||||||
| 194 | 38 | my $plugin = $plugins{$plugin_name}; | |||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | 38 | ▶ | return $plugin->{includes} || []; | ||||||||||||||||||
|
Condition100%2/2
$plugin->{'includes'} || []
MC/DC100%1/1
$plugin->{'includes'} || []
$plugin->{'includes'}
Truth table100%2/2
A
A= $plugin->{'includes'}
| |||||||||||||||||||||
| 197 | } | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 12 | ▶ | sub init_consumer { | ||||||||||||||||||
|
Subroutine
init_consumer: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 200 | 12 | my ($consumer_name) = @_; | |||||||||||||||||||
| 201 | 12 | my $consumer = $consumers{$consumer_name} = {}; | |||||||||||||||||||
| 202 | 12 | $consumer->{moo_extends} = $consumer_name->can('extends'); | |||||||||||||||||||
| 203 | 12 | $consumer->{moo_with} = $consumer_name->can('with'); | |||||||||||||||||||
| 204 | 12 | $consumer->{moo_has} = $consumer_name->can('has'); | |||||||||||||||||||
| 205 | 12 | return; | |||||||||||||||||||
| 206 | } | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 11 | ▶ | sub is_consumer { | ||||||||||||||||||
|
Subroutine
is_consumer: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||
| 209 | 11 | my ($consumer_name) = @_; | |||||||||||||||||||
| 210 | 11 | ▶ | return $consumers{$consumer_name} ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 211 | } | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | 12 | ▶ | sub get_consumer_moo_extends { | ||||||||||||||||||
|
Subroutine
get_consumer_moo_extends: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 214 | 12 | my ($consumer_name) = @_; | |||||||||||||||||||
| 215 | 12 | my $consumer = $consumers{$consumer_name}; | |||||||||||||||||||
| 216 | 12 | return $consumer->{moo_extends}; | |||||||||||||||||||
| 217 | } | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 12 | ▶ | sub get_consumer_moo_with { | ||||||||||||||||||
|
Subroutine
get_consumer_moo_with: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 220 | 12 | my ($consumer_name) = @_; | |||||||||||||||||||
| 221 | 12 | my $consumer = $consumers{$consumer_name}; | |||||||||||||||||||
| 222 | 12 | return $consumer->{moo_with}; | |||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | 9 | ▶ | sub get_consumer_moo_has { | ||||||||||||||||||
|
Subroutine
get_consumer_moo_has: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 226 | 9 | my ($consumer_name) = @_; | |||||||||||||||||||
| 227 | 9 | my $consumer = $consumers{$consumer_name}; | |||||||||||||||||||
| 228 | 9 | return $consumer->{moo_has}; | |||||||||||||||||||
| 229 | } | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | 1 | ▶ | sub set_consumer_namespace { | ||||||||||||||||||
|
Subroutine
set_consumer_namespace: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||
| 232 | 1 | my ($consumer_name, $namespace) = @_; | |||||||||||||||||||
| 233 | 1 | my $consumer = $consumers{$consumer_name}; | |||||||||||||||||||
| 234 | 1 | local $Carp::Internal{ (__PACKAGE__) } = 1; | |||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | croak "The plugin namespace for $consumer has already been set" | ||||||||||||||||||||
| 237 | 1 | ▶ | if exists $consumer->{namespace}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | 1 | ▶ | croak "An undefined plugin namespace cannot be set" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 240 | if !defined $namespace; | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | 1 | ▶ | croak "The plugin namespace $namespace does not appear to be a valid module name" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 243 | if !is_module_name( $namespace ); | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | 1 | $consumer->{namespace} = $namespace; | |||||||||||||||||||
| 246 | |||||||||||||||||||||
| 247 | 1 | return; | |||||||||||||||||||
| 248 | } | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | 36 | ▶ | sub get_consumer_namespace { | ||||||||||||||||||
|
Subroutine
get_consumer_namespace: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||
| 251 | 36 | my ($consumer_name) = @_; | |||||||||||||||||||
| 252 | 36 | my $consumer = $consumers{$consumer_name}; | |||||||||||||||||||
| 253 | 36 | local $Carp::Internal{ (__PACKAGE__) } = 1; | |||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | 36 | ▶ | return $consumer->{namespace} || $consumer_name; | ||||||||||||||||||
|
Condition66%2/3
$consumer->{'namespace'} || $consumer_name
MC/DC0%0/2
$consumer->{'namespace'} || $consumer_name
$consumer->{'namespace'}
$consumer_name
Truth table66%2/3
A || B
A= $consumer->{'namespace'}B= $consumer_name
| |||||||||||||||||||||
| 256 | } | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | 5 | 1; | |||||||||||||||||||
| 259 | __END__ | ||||||||||||||||||||