| 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 | use strict; | ||
| 17 | use warnings; | ||
| 18 | |||
| 19 | use feature 'signatures'; | ||
| 20 | no warnings 'experimental::signatures'; | ||
| 21 | |||
| 22 | use Exporter 'import'; | ||
| 23 | our @EXPORT_OK = qw( call_named_callable load_named_callable require_optional ); | ||
| 24 | |||
| 25 | our $VERSION = 2.1129; | ||
| 26 | |||
| 27 | use Carp qw(croak); | ||
| 28 | use Readonly; | ||
| 29 | |||
| 30 | # A qualified Perl name: "Module::function" (at least one "::"), each part a valid | ||
| 31 | # identifier. | ||
| 32 | 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 | 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 | sub require_optional ($module) { | ||
| 41 | croak "oEdtk::Ext: invalid module name '$module'" | ||
| 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 | ( 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 | my $loaded; | ||
| 53 | { | ||
| 54 | local $SIG{__DIE__}; # a caught die must not reach a global handler | ||
| 55 | $loaded = eval { require "$file.pm"; 1 }; ## no critic (Modules::RequireBarewordIncludes) | ||
| 56 | } | ||
| 57 | return $loaded ? 1 : 0; | ||
| 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 | sub load_named_callable ( $cfg, $key ) { | ||
| 66 | my $name = $cfg->{$key}; | ||
| 67 | return unless defined $name && length $name; | ||
| 68 | croak "oEdtk::Ext: configuration '$key' = '$name' is not a valid Module::function name" | ||
| 69 | unless $name =~ $NAMED_CALLABLE_RE && $name =~ /::/; | ||
| 70 | |||
| 71 | my ( $module, $func ) = $name =~ m{\A(.+)::([^:]+)\z}; | ||
| 72 | croak | ||
| 73 | "oEdtk::Ext: configuration '$key' = '$name' names the module '$module', which is not installed" | ||
| 74 | unless require_optional($module); | ||
| 75 | |||
| 76 | my $code = $module->can($func); | ||
| 77 | croak "oEdtk::Ext: configuration '$key' = '$name' names the function " | ||
| 78 | . "'$func', which the module '$module' does not provide" | ||
| 79 | unless $code; | ||
| 80 | 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 | sub call_named_callable ( $cfg, $key, @args ) { | ||
| 86 | my $code = load_named_callable( $cfg, $key ); | ||
| 87 | return unless $code; | ||
| 88 | return $code->(@args); | ||
| 89 | } | ||
| 90 | |||
| 91 | 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 |