| 1 | package oEdtk::Ext; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Extension points without a registry (docs/integration/PLAN.md, step S23, | ||||||||||||||||||||||||||||
| 4 | # decision D18). Two ways for a deployment to plug corporate code into | ||||||||||||||||||||||||||||
| 5 | # the generic oEdtk without oEdtk depending on oEUser: | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # (a) an optional companion module with a fixed name (oEUser::Lib, | ||||||||||||||||||||||||||||
| 8 | # oEUser::Schema), loaded on demand and simply absent otherwise; | ||||||||||||||||||||||||||||
| 9 | # (b) a callable named in the configuration (EDTK_ACQUIT_API), resolved to a | ||||||||||||||||||||||||||||
| 10 | # "Module::function" name, validated by a regular expression, and loaded | ||||||||||||||||||||||||||||
| 11 | # explicitly. | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # In both cases a missing extension is a documented default, not an error; an | ||||||||||||||||||||||||||||
| 14 | # invalid name is an explicit error. This module has no oEUser or corporate reference. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use feature 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 4 | our @EXPORT_OK = qw( call_named_callable load_named_callable require_optional ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 4 | our $VERSION = 2.1131; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 4 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # A qualified Perl name: "Module::function" (at least one "::"), each part a valid | ||||||||||||||||||||||||||||
| 31 | # identifier. | ||||||||||||||||||||||||||||
| 32 | 4 | Readonly my $NAMED_CALLABLE_RE => qr{\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z}; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # A bare module name (a leading identifier followed by optional "::" parts). | ||||||||||||||||||||||||||||
| 35 | 4 | Readonly my $MODULE_RE => qr{\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z}; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Load an optional companion module by its fixed name. Return 1 on success and 0 | ||||||||||||||||||||||||||||
| 38 | # when it is simply not installed; a syntactically invalid name is an error. The | ||||||||||||||||||||||||||||
| 39 | # caller treats 0 as the documented default (extension absent). | ||||||||||||||||||||||||||||
| 40 | 6 | ▶ | sub require_optional ($module) { | ||||||||||||||||||||||||||
|
Subroutine
require_optional: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 6 | ▶ | croak "oEdtk::Ext: invalid module name '$module'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $module and $module =~ /$MODULE_RE/
MC/DC0%0/2
defined $module and $module =~ /$MODULE_RE/
defined $module
$module =~ /$MODULE_RE/
Truth table33%1/3
A and B
A= defined $moduleB= $module =~ /$MODULE_RE/
| |||||||||||||||||||||||||||||
| 42 | unless defined $module && $module =~ $MODULE_RE; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # A variable require does not translate "::" to "/", so do it explicitly. A | ||||||||||||||||||||||||||||
| 45 | # dynamic name can only be required as a string. | ||||||||||||||||||||||||||||
| 46 | 6 | ( my $file = $module ) =~ s{::}{/}g; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # A die caught by eval still fires a global $SIG{__DIE__} handler, which oEdtk | ||||||||||||||||||||||||||||
| 49 | # installs (oEdtk::Tracking, at BEGIN) and which would leak the probe's | ||||||||||||||||||||||||||||
| 50 | # "Can't locate ..." message. Silence it for the probe: an absent optional | ||||||||||||||||||||||||||||
| 51 | # companion must leave no trace. | ||||||||||||||||||||||||||||
| 52 | 6 | my $loaded; | |||||||||||||||||||||||||||
| 53 | { | ||||||||||||||||||||||||||||
| 54 | 6 | local $SIG{__DIE__}; # a caught die must not reach a global handler | |||||||||||||||||||||||||||
| 55 | 0 | $loaded = eval { require "$file.pm"; 1 }; ## no critic (Modules::RequireBarewordIncludes) | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 6 | ▶ | return $loaded ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # Resolve a "Module::function" name read from configuration under $key. | ||||||||||||||||||||||||||||
| 61 | # absent or empty value -> undef (documented default: the caller does nothing) | ||||||||||||||||||||||||||||
| 62 | # syntactically invalid -> croak | ||||||||||||||||||||||||||||
| 63 | # valid but not resolvable -> croak (a named extension must be present) | ||||||||||||||||||||||||||||
| 64 | # Return the coderef. | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub load_named_callable ( $cfg, $key ) { | ||||||||||||||||||||||||||
|
Subroutine
load_named_callable: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 0 | my $name = $cfg->{$key}; | |||||||||||||||||||||||||||
| 67 | 0 | ▶ | return unless defined $name && length $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $name and length $name
MC/DC0%0/2
defined $name and length $name
defined $name
length $name
Truth table0%0/3
A and B
A= defined $nameB= length $name
| |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | croak "oEdtk::Ext: configuration '$key' = '$name' is not a valid Module::function name" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$name =~ /$NAMED_CALLABLE_RE/ and $name =~ /::/
MC/DC0%0/2
$name =~ /$NAMED_CALLABLE_RE/ and $name =~ /::/
$name =~ /$NAMED_CALLABLE_RE/
$name =~ /::/
Truth table0%0/3
A and B
A= $name =~ /$NAMED_CALLABLE_RE/B= $name =~ /::/
| |||||||||||||||||||||||||||||
| 69 | unless $name =~ $NAMED_CALLABLE_RE && $name =~ /::/; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | my ( $module, $func ) = $name =~ m{\A(.+)::([^:]+)\z}; | |||||||||||||||||||||||||||
| 72 | 0 | ▶ | croak | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | "oEdtk::Ext: configuration '$key' = '$name' names the module '$module', which is not installed" | ||||||||||||||||||||||||||||
| 74 | unless require_optional($module); | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | my $code = $module->can($func); | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | croak "oEdtk::Ext: configuration '$key' = '$name' names the function " | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | . "'$func', which the module '$module' does not provide" | ||||||||||||||||||||||||||||
| 79 | unless $code; | ||||||||||||||||||||||||||||
| 80 | 0 | return $code; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Resolve and call the configured callable with @args. Return undef when the | ||||||||||||||||||||||||||||
| 84 | # configuration key is absent (documented default: no call). | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | sub call_named_callable ( $cfg, $key, @args ) { | ||||||||||||||||||||||||||
|
Subroutine
call_named_callable: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 0 | my $code = load_named_callable( $cfg, $key ); | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | return unless $code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | return $code->(@args); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 4 | 1; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head1 NAME | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | oEdtk::Ext - plug corporate extensions into the generic oEdtk without a registry | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | use oEdtk::Ext qw(require_optional load_named_callable call_named_callable); | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # (a) optional companion module, by its fixed name | ||||||||||||||||||||||||||||
| 102 | if ( require_optional('oEUser::Schema') ) { | ||||||||||||||||||||||||||||
| 103 | my @tables = oEUser::Schema::extra_tables(); | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # (b) callable named in the configuration | ||||||||||||||||||||||||||||
| 107 | my $code = load_named_callable( $cfg, 'EDTK_ACQUIT_API' ); # undef if absent | ||||||||||||||||||||||||||||
| 108 | call_named_callable( $cfg, 'EDTK_ACQUIT_API', $docid, $date ); | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Two extension points that let a deployment add corporate behaviour to the | ||||||||||||||||||||||||||||
| 113 | generic C<oEdtk> without C<oEdtk> depending on the companion distribution | ||||||||||||||||||||||||||||
| 114 | C<oEUser> (decision D18): | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =over 4 | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =item * | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | C<require_optional> loads a companion module with a fixed name (for example | ||||||||||||||||||||||||||||
| 121 | C<oEUser::Lib> or C<oEUser::Schema>) and returns 1 on success, 0 when it is not | ||||||||||||||||||||||||||||
| 122 | installed. A missing extension is a documented default, not an error. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =item * | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | C<load_named_callable> resolves a C<Module::function> name read from the | ||||||||||||||||||||||||||||
| 127 | configuration (for example C<EDTK_ACQUIT_API>) and returns the coderef. The name | ||||||||||||||||||||||||||||
| 128 | is validated by a regular expression before any load. An absent or empty value | ||||||||||||||||||||||||||||
| 129 | returns C<undef> (the caller then does nothing); a syntactically invalid name, | ||||||||||||||||||||||||||||
| 130 | or a valid name that cannot be resolved, is an explicit error. | ||||||||||||||||||||||||||||
| 131 | C<call_named_callable> resolves and calls in one step. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =back | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | The configuration file is trusted administration data: a name that passes the | ||||||||||||||||||||||||||||
| 136 | regular expression is required explicitly, and the loaded module is responsible | ||||||||||||||||||||||||||||
| 137 | for its own behaviour. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head2 require_optional | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | my $ok = require_optional($module); | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Load a companion module by its fixed name; return 1 on success, 0 when it is not | ||||||||||||||||||||||||||||
| 146 | installed. A missing extension is a documented default, not an error. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 load_named_callable | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | my $code = load_named_callable($cfg, $key); | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Resolve the C<Module::function> name read from C<$cfg> under C<$key> and return | ||||||||||||||||||||||||||||
| 153 | the coderef, or C<undef> when the value is absent or empty. A syntactically | ||||||||||||||||||||||||||||
| 154 | invalid name, or a valid name that cannot be resolved, is an error. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head2 call_named_callable | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | call_named_callable($cfg, $key, @args); | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Resolve the callable named by C<$key> and call it with C<@args>. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | The oEdtk team. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =cut | ||||||||||||||||||||||||||||