Mojolicious/Plugins.pm

filter: Statementstmt 97.6% 41 / 42 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 97.1% 68 / 70 CC 11 SCAR 8.9
CC
11
Cov
96%
load_plugin
4.0
_load
3.0
emit_chain
3.0
CRAP 2.4
« Mojo/Content/Single.pm ↑ Summary Mojo/EventEmitter.pm »
1 package Mojolicious::Plugins;
2 20 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 20 ▶use Mojo::Loader qw(load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Mojo::Util qw(camelize);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 83 ▶has namespaces => sub { ['Mojolicious::Plugin'] };
Subroutine __ANON__: called CC=1 SCAR=0.0
8  
9 256 ▶sub emit_chain {
Subroutine emit_chain: called CC=3 SCAR=11.0
Pod documented
10 256 my ($self, $name, @args) = @_;
11  
12 256 my $wrapper;
13 256 for my $cb (reverse @{$self->subscribers($name)}) {
14 468 my $next = $wrapper;
15 468 ▶ $wrapper = sub { $cb->($next, @args) };
Subroutine __ANON__: called CC=1 SCAR=0.0
16 }
17  
18 256 ▶ !$wrapper ? return : return $wrapper->();
Branch: 1 branch
BranchTrueFalseTotal
!$wrapper ? : 6 250 256
19 }
20  
21 962 ▶sub emit_hook {
Subroutine emit_hook: called CC=2 SCAR=6.9
Pod documented
22 962 my $self = shift;
23 962 for my $cb (@{$self->subscribers(shift)}) { $cb->(@_) }
24 962 return $self;
25 }
26  
27 173 ▶sub emit_hook_reverse {
Subroutine emit_hook_reverse: called CC=2 SCAR=6.9
Pod documented
28 173 my $self = shift;
29 173 for my $cb (reverse @{$self->subscribers(shift)}) { $cb->(@_) }
30 173 return $self;
31 }
32  
33 493 ▶sub load_plugin {
Subroutine load_plugin: called CC=4 SCAR=13.9
Pod documented
34 493 my ($self, $name) = @_;
35  
36 # Try all namespaces and full module name
37 493 ▶ my $suffix = $name =~ /^[a-z]/ ? camelize $name : $name;
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^[a-z]/u ? : 21 472 493
38 493 my @classes = map {"${_}::$suffix"} @{$self->namespaces};
39 493 ▶ for my $class (@classes, $name) { return $class->new if _load($class) }
Branch: 1 branch
BranchTrueFalseTotal
if _load($class) 493 49 542
40  
41 # Not found
42 0 die qq{Plugin "$name" missing, maybe you need to install it?\n};
43 }
44  
45 493 ▶sub register_plugin { shift->load_plugin(shift)->register(shift, ref $_[0] ? $_[0] : {@_}) }
Subroutine register_plugin: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 3 490 493
46  
47 542 ▶sub _load {
Subroutine _load: called CC=3 SCAR=11.1
48 542 my $module = shift;
49 542 ▶ return $module->isa('Mojolicious::Plugin') unless my $e = load_class $module;
Branch: 1 branch
BranchTrueFalseTotal
unless my $e = load_class($module) 493 49 542
50 49 ▶ ref $e ? die $e : return undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $e ? : 0 49 49
51 }
52  
53 20 1;
54  
55 =encoding utf8
56  
57 =head1 NAME
58  
59 Mojolicious::Plugins - Plugin manager
60  
61 =head1 SYNOPSIS
62  
63 use Mojolicious::Plugins;
64  
65 my $plugins = Mojolicious::Plugins->new;
66 push @{$plugins->namespaces}, 'MyApp::Plugin';
67  
68 =head1 DESCRIPTION
69  
70 L<Mojolicious::Plugins> is the plugin manager of L<Mojolicious>.
71  
72 =head1 PLUGINS
73  
74 The following plugins are included in the L<Mojolicious> distribution as examples.
75  
76 =over 2
77  
78 =item L<Mojolicious::Plugin::Config>
79  
80 Perl-ish configuration files.
81  
82 =item L<Mojolicious::Plugin::DefaultHelpers>
83  
84 General purpose helper collection, loaded automatically.
85  
86 =item L<Mojolicious::Plugin::EPLRenderer>
87  
88 Renderer for plain embedded Perl templates, loaded automatically.
89  
90 =item L<Mojolicious::Plugin::EPRenderer>
91  
92 Renderer for more sophisticated embedded Perl templates, loaded automatically.
93  
94 =item L<Mojolicious::Plugin::HeaderCondition>
95  
96 Route condition for all kinds of headers, loaded automatically.
97  
98 =item L<Mojolicious::Plugin::JSONConfig>
99  
100 JSON configuration files.
101  
102 =item L<Mojolicious::Plugin::Mount>
103  
104 Mount whole L<Mojolicious> applications.
105  
106 =item L<Mojolicious::Plugin::NotYAMLConfig>
107  
108 YAML configuration files.
109  
110 =item L<Mojolicious::Plugin::TagHelpers>
111  
112 Template specific helper collection, loaded automatically.
113  
114 =back
115  
116 =head1 EVENTS
117  
118 L<Mojolicious::Plugins> inherits all events from L<Mojo::EventEmitter>.
119  
120 =head1 ATTRIBUTES
121  
122 L<Mojolicious::Plugins> implements the following attributes.
123  
124 =head2 namespaces
125  
126 my $namespaces = $plugins->namespaces;
127 $plugins = $plugins->namespaces(['Mojolicious::Plugin']);
128  
129 Namespaces to load plugins from, defaults to L<Mojolicious::Plugin>.
130  
131 # Add another namespace to load plugins from
132 push @{$plugins->namespaces}, 'MyApp::Plugin';
133  
134 =head1 METHODS
135  
136 L<Mojolicious::Plugins> inherits all methods from L<Mojo::EventEmitter> and implements the following new ones.
137  
138 =head2 emit_chain
139  
140 $plugins->emit_chain('foo');
141 $plugins->emit_chain(foo => 123);
142  
143 Emit events as chained hooks.
144  
145 =head2 emit_hook
146  
147 $plugins = $plugins->emit_hook('foo');
148 $plugins = $plugins->emit_hook(foo => 123);
149  
150 Emit events as hooks.
151  
152 =head2 emit_hook_reverse
153  
154 $plugins = $plugins->emit_hook_reverse('foo');
155 $plugins = $plugins->emit_hook_reverse(foo => 123);
156  
157 Emit events as hooks in reverse order.
158  
159 =head2 load_plugin
160  
161 my $plugin = $plugins->load_plugin('some_thing');
162 my $plugin = $plugins->load_plugin('SomeThing');
163 my $plugin = $plugins->load_plugin('MyApp::Plugin::SomeThing');
164  
165 Load a plugin from the configured namespaces or by full module name.
166  
167 =head2 register_plugin
168  
169 $plugins->register_plugin('some_thing', Mojolicious->new);
170 $plugins->register_plugin('some_thing', Mojolicious->new, foo => 23);
171 $plugins->register_plugin('some_thing', Mojolicious->new, {foo => 23});
172 $plugins->register_plugin('SomeThing', Mojolicious->new);
173 $plugins->register_plugin('SomeThing', Mojolicious->new, foo => 23);
174 $plugins->register_plugin('SomeThing', Mojolicious->new, {foo => 23});
175 $plugins->register_plugin('MyApp::Plugin::SomeThing', Mojolicious->new);
176 $plugins->register_plugin(
177 'MyApp::Plugin::SomeThing', Mojolicious->new, foo => 23);
178 $plugins->register_plugin(
179 'MyApp::Plugin::SomeThing', Mojolicious->new, {foo => 23});
180  
181 Load a plugin from the configured namespaces or by full module name and run C<register>, optional arguments are passed
182 through.
183  
184 =head1 SEE ALSO
185  
186 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
187  
188 =cut
« Mojo/Content/Single.pm ↑ Summary Mojo/EventEmitter.pm »