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