| 1 | package Mojolicious::Plugin::Module::Loader 0.02; | ||
| 2 | 1 | ▶ | use v5.26; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | # ABSTRACT: Automatically load mojolicious namespaces | ||
| 6 | |||
| 7 | =encoding UTF-8 | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Mojolicious::Plugin::Module::Loader - Automatically load mojolicious namespaces | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | $app->plugin('Module::Loader' => { | ||
| 16 | command_namespaces => ['MyApp::Command'], | ||
| 17 | plugin_namespaces => ['MyApp::Plugin'] | ||
| 18 | }); | ||
| 19 | |||
| 20 | # Or | ||
| 21 | $app->plugin('Module::Loader'); | ||
| 22 | ... | ||
| 23 | $app->add_command_namespace('Dynamically::Loaded::Module::Command'); | ||
| 24 | $app->add_plugin_namespace('Dynamically::Loaded::Module::Plugin'); | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | This module simply adds two mojolicious helpers, L</add_command_namespace> and | ||
| 29 | L</add_plugin_namespace>, and calls these automatically at registration time | ||
| 30 | on the contents of the C<command_namespaces> and C<plugin_namespaces> configuration | ||
| 31 | parameters, respectively. | ||
| 32 | |||
| 33 | =head1 METHODS | ||
| 34 | |||
| 35 | L<Mojolicious::Plugin::Cron::Scheduler> inherits all methods from | ||
| 36 | L<Mojolicious::Plugin> and implements the following new ones | ||
| 37 | |||
| 38 | =head2 register( \%config ) | ||
| 39 | |||
| 40 | Register plugin in L<Mojolicious> application. Accepts a HashRef of parameters | ||
| 41 | with two supprted keys: | ||
| 42 | |||
| 43 | =head4 command_namespaces | ||
| 44 | |||
| 45 | ArrayRef of namespaces to automatically call L</add_command_namespace> on | ||
| 46 | |||
| 47 | =head4 controller_namespaces | ||
| 48 | |||
| 49 | ArrayRef of namespaces to automatically call L</add_controller_namespace> on | ||
| 50 | |||
| 51 | =head4 plugin_namespaces | ||
| 52 | |||
| 53 | ArrayRef of namespaces to automatically call L</add_plugin_namespace> on | ||
| 54 | |||
| 55 | =head2 add_command_namespace( $str ) | ||
| 56 | |||
| 57 | Adds the given namespace to the Mojolicious Commands | ||
| 58 | L<namespaces|https://metacpan.org/pod/Mojolicious::Commands#namespaces> array. | ||
| 59 | Packages inheriting from L<Mojolicious::Command> in these namespaces are loaded | ||
| 60 | as runnable commands from the mojo entrypoint script. | ||
| 61 | |||
| 62 | =head2 add_controller_namespace( $str ) | ||
| 63 | |||
| 64 | Adds the given namespace to the Mojolicious routes | ||
| 65 | L<namespaces|https://metacpan.org/pod/Mojolicious::Routes#namespaces> array. | ||
| 66 | |||
| 67 | =head2 add_plugin_namespace( $str ) | ||
| 68 | |||
| 69 | Searches the given namespace for packages inheriting from L<Mojolicious::Plugin> | ||
| 70 | and loads them via L<https://metacpan.org/pod/Mojolicious#plugin> | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | 0 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 75 | use Module::Find; | ||
| 76 | |||
| 77 | use experimental qw(signatures); | ||
| 78 | |||
| 79 | sub register($self, $app, $conf) { | ||
| 80 | $app->helper( | ||
| 81 | add_command_namespace => sub($c, $ns) { | ||
| 82 | push($app->commands->namespaces->@*, $ns); | ||
| 83 | } | ||
| 84 | ); | ||
| 85 | |||
| 86 | $app->helper( | ||
| 87 | add_controller_namespace => sub($c, $ns) { | ||
| 88 | push($app->routes->namespaces->@*, $ns); | ||
| 89 | } | ||
| 90 | ); | ||
| 91 | |||
| 92 | $app->helper( | ||
| 93 | add_plugin_namespace => sub($c, $ns) { | ||
| 94 | $app->plugin($_) foreach (map {Module::Find::findallmod($_)} $ns); | ||
| 95 | } | ||
| 96 | ); | ||
| 97 | |||
| 98 | $app->add_command_namespace($_) foreach (($conf->{command_namespaces} // [])->@*); | ||
| 99 | $app->add_controller_namespace($_) foreach (($conf->{controller_namespaces} // [])->@*); | ||
| 100 | $app->add_plugin_namespace($_) foreach (($conf->{plugin_namespaces} // [])->@*); | ||
| 101 | } | ||
| 102 | |||
| 103 | =head1 AUTHOR | ||
| 104 | |||
| 105 | Mark Tyrrell C<< <mark@tyrrminal.dev> >> | ||
| 106 | |||
| 107 | =head1 LICENSE | ||
| 108 | |||
| 109 | Copyright (c) 2024 Mark Tyrrell | ||
| 110 | |||
| 111 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 112 | of this software and associated documentation files (the "Software"), to deal | ||
| 113 | in the Software without restriction, including without limitation the rights | ||
| 114 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 115 | copies of the Software, and to permit persons to whom the Software is | ||
| 116 | furnished to do so, subject to the following conditions: | ||
| 117 | |||
| 118 | The above copyright notice and this permission notice shall be included in all | ||
| 119 | copies or substantial portions of the Software. | ||
| 120 | |||
| 121 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 122 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 123 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 124 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 125 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 126 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 127 | SOFTWARE. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | 1; | ||
| 132 | |||
| 133 | __END__ | ||