| 1 | package Myriad::Plugin; | ||
| 2 | |||
| 3 | 1 | ▶ | use Myriad::Class; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = '1.001'; # VERSION | ||
| 6 | our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY | ||
| 7 | |||
| 8 | =encoding utf8 | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Myriad::Plugin - extensible L<Myriad> functionality using attributes | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | The plugin system allows sharing of various features between service implementations. | ||
| 17 | Examples might include database or API access. | ||
| 18 | |||
| 19 | Plugins will be loaded automatically if an as-yet-unknown attribute is used. | ||
| 20 | |||
| 21 | For example, a hypothetical C<< async method example : Reverse() { } >> service method | ||
| 22 | definition would attempt to use the registered C<Reverse> handler, and if none was found | ||
| 23 | would proceed to load C<< Myriad::Plugin::Reverse >> and try again. | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | # Normal access is through this singleton, but we do allow | ||
| 28 | # separate instances in unit tests. Note that this is the top-level | ||
| 29 | # Myriad::Plugin instance only, and does not affect the subclass | ||
| 30 | # instances. | ||
| 31 | our $REGISTRY = __PACKAGE__->new; | ||
| 32 | |||
| 33 | has $plugin; | ||
| 34 | |||
| 35 | =head1 METHODS | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | =head2 register | ||
| 40 | |||
| 41 | Example: | ||
| 42 | |||
| 43 | has $db; | ||
| 44 | register SQL => async method ($code, %args) { | ||
| 45 | return sub ($srv, @args) { | ||
| 46 | my ($sql, @bind) = $srv->$code(@args); | ||
| 47 | return $db->query( | ||
| 48 | $sql => @bind | ||
| 49 | )->row_hashrefs | ||
| 50 | } | ||
| 51 | }; | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | async method register ($attr, $code) { | ||
| 56 | $log->tracef('Registering plugin %s for %s', $code, $attr); | ||
| 57 | die 'already have ' . $attr if exists $plugin->{$attr}; | ||
| 58 | $plugin->{$attr} = $code; | ||
| 59 | } | ||
| 60 | |||
| 61 | async method apply_to_service ($srv, $method, $attr) { | ||
| 62 | $log->tracef('Applying plugin for %s to %s on %s', $attr, $method, $srv); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub import { | ||
| 66 | my ($called_on, %args) = @_; | ||
| 67 | return undef unless $called_on eq __PACKAGE__; | ||
| 68 | my $pkg = caller(0); | ||
| 69 | my $meta = Myriad::Class->import(target => $pkg); | ||
| 70 | no strict 'refs'; | ||
| 71 | *{$pkg . '::register'} = sub ($attr, $code) { | ||
| 72 | $REGISTRY->register($attr, $code); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; | ||
| 77 | |||
| 78 | =head1 AUTHOR | ||
| 79 | |||
| 80 | Deriv Group Services Ltd. C<< DERIV@cpan.org >>. | ||
| 81 | |||
| 82 | See L<Myriad/CONTRIBUTORS> for full details. | ||
| 83 | |||
| 84 | =head1 LICENSE | ||
| 85 | |||
| 86 | Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself. | ||
| 87 | |||