File Coverage

blib/lib/Mojolicious/Plugins.pm
Criterion Covered Total %
statement 39 39 100.0
branch 11 12 91.6
condition n/a
subroutine 10 10 100.0
pod 5 5 100.0
total 65 66 98.4


line stmt bran cond sub pod time code
1             package Mojolicious::Plugins;
2 54     54   532 use Mojo::Base 'Mojo::EventEmitter';
  54         135  
  54         465  
3              
4 54     54   429 use Mojo::Loader qw(load_class);
  54         174  
  54         4619  
5 54     54   426 use Mojo::Util qw(camelize);
  54         139  
  54         58729  
6              
7             has namespaces => sub { ['Mojolicious::Plugin'] };
8              
9             sub emit_chain {
10 1779     1779 1 6089 my ($self, $name, @args) = @_;
11              
12 1779         3051 my $wrapper;
13 1779         3297 for my $cb (reverse @{$self->subscribers($name)}) {
  1779         6523  
14 3007         5550 my $next = $wrapper;
15 3007     3003   16515 $wrapper = sub { $cb->($next, @args) };
  3003         10668  
16             }
17              
18 1779 100       6594 !$wrapper ? return : return $wrapper->();
19             }
20              
21             sub emit_hook {
22 5366     5366 1 10110 my $self = shift;
23 5366         9087 for my $cb (@{$self->subscribers(shift)}) { $cb->(@_) }
  5366         18622  
  623         6946  
24 5366         13510 return $self;
25             }
26              
27             sub emit_hook_reverse {
28 1010     1010 1 2310 my $self = shift;
29 1010         2066 for my $cb (reverse @{$self->subscribers(shift)}) { $cb->(@_) }
  1010         4122  
  384         1582  
30 1010         3413 return $self;
31             }
32              
33             sub load_plugin {
34 629     629 1 1814 my ($self, $name) = @_;
35              
36             # Try all namespaces and full module name
37 629 100       3116 my $suffix = $name =~ /^[a-z]/ ? camelize $name : $name;
38 629         1202 my @classes = map {"${_}::$suffix"} @{$self->namespaces};
  656         3532  
  629         2138  
39 629 100       1749 for my $class (@classes, $name) { return $class->new if _load($class) }
  663         1772  
40              
41             # Not found
42 1         11 die qq{Plugin "$name" missing, maybe you need to install it?\n};
43             }
44              
45 629 100   629 1 2189 sub register_plugin { shift->load_plugin(shift)->register(shift, ref $_[0] ? $_[0] : {@_}) }
46              
47             sub _load {
48 663     663   1278 my $module = shift;
49 663 100       2755 return $module->isa('Mojolicious::Plugin') unless my $e = load_class $module;
50 35 50       233 ref $e ? die $e : return undef;
51             }
52              
53             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 is the plugin manager of L.
71              
72             =head1 PLUGINS
73              
74             The following plugins are included in the L distribution as examples.
75              
76             =over 2
77              
78             =item L
79              
80             Perl-ish configuration files.
81              
82             =item L
83              
84             General purpose helper collection, loaded automatically.
85              
86             =item L
87              
88             Renderer for plain embedded Perl templates, loaded automatically.
89              
90             =item L
91              
92             Renderer for more sophisticated embedded Perl templates, loaded automatically.
93              
94             =item L
95              
96             Route condition for all kinds of headers, loaded automatically.
97              
98             =item L
99              
100             JSON configuration files.
101              
102             =item L
103              
104             Mount whole L applications.
105              
106             =item L
107              
108             YAML configuration files.
109              
110             =item L
111              
112             Template specific helper collection, loaded automatically.
113              
114             =back
115              
116             =head1 EVENTS
117              
118             L inherits all events from L.
119              
120             =head1 ATTRIBUTES
121              
122             L 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.
130              
131             # Add another namespace to load plugins from
132             push @{$plugins->namespaces}, 'MyApp::Plugin';
133              
134             =head1 METHODS
135              
136             L inherits all methods from L 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, optional arguments are passed
182             through.
183              
184             =head1 SEE ALSO
185              
186             L, L, L.
187              
188             =cut