Newer Older
211 lines | 5.057kb
copy gitweblite soruce code
root authored on 2012-11-23
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
  $wrapper->();
23

            
24
  return $self;
25
}
26

            
27
sub emit_hook_reverse {
28
  my $self = shift;
29
  $_->(@_) for reverse @{$self->subscribers(shift)};
30
  return $self;
31
}
32

            
33
sub load_plugin {
34
  my ($self, $name) = @_;
35

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
36
  # Try all namespaces
copy gitweblite soruce code
root authored on 2012-11-23
37
  my $class = $name =~ /^[a-z]/ ? camelize($name) : $name;
38
  for my $namespace (@{$self->namespaces}) {
39
    my $module = "${namespace}::$class";
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
40
    return $module->new if _load($module);
copy gitweblite soruce code
root authored on 2012-11-23
41
  }
42

            
43
  # Full module name
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
44
  return $name->new if _load($name);
copy gitweblite soruce code
root authored on 2012-11-23
45

            
46
  # Not found
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
47
  die qq{Plugin "$name" missing, maybe you need to install it?\n};
copy gitweblite soruce code
root authored on 2012-11-23
48
}
49

            
50
sub register_plugin {
51
  shift->load_plugin(shift)->register(shift, ref $_[0] ? $_[0] : {@_});
52
}
53

            
54
sub _load {
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
55
  my $module = shift;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
56
  if (my $e = Mojo::Loader->new->load($module)) {
57
    ref $e ? die $e : return undef;
58
  }
copy gitweblite soruce code
root authored on 2012-11-23
59
  return $module->isa('Mojolicious::Plugin') ? 1 : undef;
60
}
61

            
62
1;
63

            
64
=head1 NAME
65

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
66
Mojolicious::Plugins - Plugin manager
copy gitweblite soruce code
root authored on 2012-11-23
67

            
68
=head1 SYNOPSIS
69

            
70
  use Mojolicious::Plugins;
71

            
72
  my $plugins = Mojolicious::Plugin->new;
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
73
  push @{$plugins->namespaces}, 'MyApp::Plugin';
copy gitweblite soruce code
root authored on 2012-11-23
74

            
75
=head1 DESCRIPTION
76

            
77
L<Mojolicious::Plugins> is the plugin manager of L<Mojolicious>.
78

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
79
=head1 PLUGINS
80

            
81
The following plugins are included in the L<Mojolicious> distribution as
82
examples.
83

            
84
=over 2
85

            
86
=item L<Mojolicious::Plugin::Charset>
87

            
88
Change the application charset.
89

            
90
=item L<Mojolicious::Plugin::Config>
91

            
92
Perl-ish configuration files.
93

            
94
=item L<Mojolicious::Plugin::DefaultHelpers>
95

            
96
General purpose helper collection, loaded automatically.
97

            
98
=item L<Mojolicious::Plugin::EPLRenderer>
99

            
100
Renderer for plain embedded Perl templates, loaded automatically.
101

            
102
=item L<Mojolicious::Plugin::EPRenderer>
103

            
104
Renderer for more sophisiticated embedded Perl templates, loaded
105
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::PoweredBy>
125

            
126
Add an C<X-Powered-By> header to outgoing responses, loaded automatically.
127

            
128
=item L<Mojolicious::Plugin::RequestTimer>
129

            
130
Log timing information, loaded automatically.
131

            
132
=item L<Mojolicious::Plugin::TagHelpers>
133

            
134
Template specific helper collection, loaded automatically.
135

            
136
=back
137

            
138
=head1 EVENTS
139

            
140
L<Mojolicious::Plugins> inherits all events from L<Mojo::EventEmitter>.
141

            
copy gitweblite soruce code
root authored on 2012-11-23
142
=head1 ATTRIBUTES
143

            
144
L<Mojolicious::Plugins> implements the following attributes.
145

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
146
=head2 namespaces
copy gitweblite soruce code
root authored on 2012-11-23
147

            
148
  my $namespaces = $plugins->namespaces;
149
  $plugins       = $plugins->namespaces(['Mojolicious::Plugin']);
150

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
151
Namespaces to load plugins from, defaults to L<Mojolicious::Plugin>.
copy gitweblite soruce code
root authored on 2012-11-23
152

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
153
  # Add another namespace to load plugins from
154
  push @{$plugins->namespaces}, 'MyApp::Plugin';
copy gitweblite soruce code
root authored on 2012-11-23
155

            
156
=head1 METHODS
157

            
158
L<Mojolicious::Plugins> inherits all methods from L<Mojo::EventEmitter> and
159
implements the following new ones.
160

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
161
=head2 emit_chain
copy gitweblite soruce code
root authored on 2012-11-23
162

            
163
  $plugins = $plugins->emit_chain('foo');
164
  $plugins = $plugins->emit_chain(foo => 123);
165

            
166
Emit events as chained hooks.
167

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
168
=head2 emit_hook
copy gitweblite soruce code
root authored on 2012-11-23
169

            
170
  $plugins = $plugins->emit_hook('foo');
171
  $plugins = $plugins->emit_hook(foo => 123);
172

            
173
Emit events as hooks.
174

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
175
=head2 emit_hook_reverse
copy gitweblite soruce code
root authored on 2012-11-23
176

            
177
  $plugins = $plugins->emit_hook_reverse('foo');
178
  $plugins = $plugins->emit_hook_reverse(foo => 123);
179

            
180
Emit events as hooks in reverse order.
181

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
182
=head2 load_plugin
copy gitweblite soruce code
root authored on 2012-11-23
183

            
184
  my $plugin = $plugins->load_plugin('some_thing');
185
  my $plugin = $plugins->load_plugin('SomeThing');
186
  my $plugin = $plugins->load_plugin('MyApp::Plugin::SomeThing');
187

            
188
Load a plugin from the configured namespaces or by full module name.
189

            
update Mojolicious and added...
Yuki Kimoto authored on 2013-03-20
190
=head2 register_plugin
copy gitweblite soruce code
root authored on 2012-11-23
191

            
upgraded Mojolicious to v3.7...
Yuki Kimoto authored on 2013-01-28
192
  $plugins->register_plugin('some_thing', Mojolicious->new);
193
  $plugins->register_plugin('some_thing', Mojolicious->new, foo => 23);
194
  $plugins->register_plugin('some_thing', Mojolicious->new, {foo => 23});
195
  $plugins->register_plugin('SomeThing', Mojolicious->new);
196
  $plugins->register_plugin('SomeThing', Mojolicious->new, foo => 23);
197
  $plugins->register_plugin('SomeThing', Mojolicious->new, {foo => 23});
198
  $plugins->register_plugin('MyApp::Plugin::SomeThing', Mojolicious->new);
199
  $plugins->register_plugin(
200
    'MyApp::Plugin::SomeThing', Mojolicious->new, foo => 23);
201
  $plugins->register_plugin(
202
    'MyApp::Plugin::SomeThing', Mojolicious->new, {foo => 23});
copy gitweblite soruce code
root authored on 2012-11-23
203

            
204
Load a plugin from the configured namespaces or by full module name and run
205
C<register>, optional arguments are passed through.
206

            
207
=head1 SEE ALSO
208

            
209
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
210

            
211
=cut