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