File Coverage

blib/lib/Mojolicious/Plugin/Module/Loader.pm
Criterion Covered Total %
statement 41 41 100.0
branch n/a
condition 6 6 100.0
subroutine 9 9 100.0
pod 1 1 100.0
total 57 57 100.0


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Module::Loader 0.02;
2 7     7   7448509 use v5.26;
  7         27  
3 7     7   42 use warnings;
  7         12  
  7         537  
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 and
29             L, and calls these automatically at registration time
30             on the contents of the C and C configuration
31             parameters, respectively.
32              
33             =head1 METHODS
34              
35             L inherits all methods from
36             L and implements the following new ones
37              
38             =head2 register( \%config )
39              
40             Register plugin in L 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 on
46              
47             =head4 controller_namespaces
48              
49             ArrayRef of namespaces to automatically call L on
50              
51             =head4 plugin_namespaces
52              
53             ArrayRef of namespaces to automatically call L on
54              
55             =head2 add_command_namespace( $str )
56              
57             Adds the given namespace to the Mojolicious Commands
58             L array.
59             Packages inheriting from L 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 array.
66              
67             =head2 add_plugin_namespace( $str )
68              
69             Searches the given namespace for packages inheriting from L
70             and loads them via L
71              
72             =cut
73              
74 7     7   663 use Mojo::Base 'Mojolicious::Plugin';
  7         8333  
  7         71  
75 7     7   7201 use Module::Find;
  7         13633  
  7         782  
76              
77 7     7   3176 use experimental qw(signatures);
  7         13744  
  7         52  
78              
79 7     7 1 465 sub register($self, $app, $conf) {
  7         15  
  7         13  
  7         11  
  7         13  
80 2         7 $app->helper(
81 2     2   522 add_command_namespace => sub($c, $ns) {
  2         5  
  2         4  
82 2         16 push($app->commands->namespaces->@*, $ns);
83             }
84 7         114 );
85              
86 2         5 $app->helper(
87 2     2   624 add_controller_namespace => sub($c, $ns) {
  2         4  
  2         4  
88 2         11 push($app->routes->namespaces->@*, $ns);
89             }
90 7         1104 );
91              
92 2         4 $app->helper(
93 2     2   409 add_plugin_namespace => sub($c, $ns) {
  2         22  
  2         4  
94 2         4 $app->plugin($_) foreach (map {Module::Find::findallmod($_)} $ns);
  2         9  
95             }
96 7         630 );
97              
98 7   100     572 $app->add_command_namespace($_) foreach (($conf->{command_namespaces} // [])->@*);
99 7   100     163 $app->add_controller_namespace($_) foreach (($conf->{controller_namespaces} // [])->@*);
100 7   100     117 $app->add_plugin_namespace($_) foreach (($conf->{plugin_namespaces} // [])->@*);
101             }
102              
103             =head1 AUTHOR
104              
105             Mark Tyrrell C<< >>
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__