Skip to content

Commit

Permalink
reuse Mojo::EventEmitter in Mojolicious::Plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 31, 2011
1 parent 0260ebd commit 6827e18
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 62 deletions.
6 changes: 6 additions & 0 deletions Changes
@@ -1,6 +1,12 @@
This file documents the revision history for Perl extension Mojolicious.

2.19 2011-10-31 00:00:00
- Deprecated Mojolicious::Plugins->add_hook in favor of
Mojolicious::Plugins->on.
- Deprecated Mojolicious::Plugins->run_hook in favor of
Mojolicious::Plugins->emit_hook.
- Deprecated Mojolicious::Plugins->run_hook_reverse in favor of
Mojolicious::Plugins->emit_hook_reverse.
- Improved documentation.
- Improved tests.

Expand Down
12 changes: 6 additions & 6 deletions lib/Mojolicious.pm
Expand Up @@ -109,7 +109,7 @@ sub new {
sub build_tx {
my $self = shift;
my $tx = Mojo::Transaction::HTTP->new;
$self->plugins->run_hook(after_build_tx => $tx, $self);
$self->plugins->emit_hook(after_build_tx => $tx, $self);
return $tx;
}

Expand Down Expand Up @@ -143,11 +143,11 @@ sub dispatch {
$c->res->code(undef) if $tx->is_websocket;
$self->sessions->load($c);
my $plugins = $self->plugins;
$plugins->run_hook(before_dispatch => $c);
$plugins->emit_hook(before_dispatch => $c);

# Try to find a static file
$self->static->dispatch($c);
$plugins->run_hook_reverse(after_static_dispatch => $c);
$plugins->emit_hook_reverse(after_static_dispatch => $c);

# Routes
my $res = $tx->res;
Expand Down Expand Up @@ -207,7 +207,7 @@ sub helper {
# You better not breathe, you better not move.
# You're better off dead, I'm tellin' you, dude.
# Santa Claus is gunning you down!"
sub hook { shift->plugins->add_hook(@_) }
sub hook { shift->plugins->on(@_) }

sub plugin {
my $self = shift;
Expand Down Expand Up @@ -460,9 +460,9 @@ and the application object, as well as a function in C<ep> templates.
$app->hook(after_dispatch => sub {...});
Extend L<Mojolicious> by adding hooks to named events.
Extend L<Mojolicious> by adding hooks.
These events are currently available and run in the listed order:
These hooks are currently available and run in the listed order:
=over 2
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Controller.pm
Expand Up @@ -410,7 +410,7 @@ sub rendered {
unless ($stash->{'mojo.finished'}++) {
$res->code(200) unless $res->code;
my $app = $self->app;
$app->plugins->run_hook_reverse(after_dispatch => $self);
$app->plugins->emit_hook_reverse(after_dispatch => $self);
$app->sessions->store($self);
}
$self->tx->resume;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Plugin/PODRenderer.pm
Expand Up @@ -115,7 +115,7 @@ sub register {
# Combine everything to a proper response
$self->content_for(mojobar => $self->include(inline => $MOJOBAR));
$self->content_for(perldoc => "$dom");
$self->app->plugins->run_hook(before_perldoc => $self);
$self->app->plugins->emit_hook(before_perldoc => $self);
$self->render(
inline => $PERLDOC,
title => $title,
Expand Down
101 changes: 48 additions & 53 deletions lib/Mojolicious/Plugins.pm
@@ -1,18 +1,31 @@
package Mojolicious::Plugins;
use Mojo::Base -base;
use Mojo::Base 'Mojo::EventEmitter';

use Mojo::Util 'camelize';

has hooks => sub { {} };
has namespaces => sub { ['Mojolicious::Plugin'] };

# "Who would have thought Hell would really exist?
# And that it would be in New Jersey?"
has namespaces => sub { ['Mojolicious::Plugin'] };

# DEPRECATED in Leaf Fluttering In Wind!
sub add_hook {
my ($self, $name, $cb) = @_;
return $self unless $name && $cb;
$self->hooks->{$name} ||= [];
push @{$self->hooks->{$name}}, $cb;
warn <<EOF;
Mojolicious::Plugins->add_hook is DEPRECATED in favor of
Mojolicious::Plugins->on!
EOF
shift->on(@_);
}

sub emit_hook {
my $self = shift;
$_->(@_) for @{$self->subscribers(shift)};
return $self;
}

# "Everybody's a jerk. You, me, this jerk."
sub emit_hook_reverse {
my $self = shift;
$_->(@_) for reverse @{$self->subscribers(shift)};
return $self;
}

Expand Down Expand Up @@ -56,21 +69,22 @@ sub register_plugin {
$self->load_plugin($name)->register($app, ref $_[0] ? $_[0] : {@_});
}

# DEPRECATED in Leaf Fluttering In Wind!
sub run_hook {
my $self = shift;
return $self unless my $name = shift;
return $self unless my $hooks = $self->hooks->{$name};
for my $hook (@$hooks) { $hook->(@_) }
return $self;
warn <<EOF;
Mojolicious::Plugins->run_hook is DEPRECATED in favor of
Mojolicious::Plugins->emit_hook!
EOF
shift->emit_hook(@_);
}

# "Everybody's a jerk. You, me, this jerk."
# DEPRECATED in Leaf Fluttering In Wind!
sub run_hook_reverse {
my $self = shift;
return $self unless my $name = shift;
return $self unless my $hooks = $self->hooks->{$name};
for my $hook (reverse @$hooks) { $hook->(@_) }
return $self;
warn <<EOF;
Mojolicious::Plugins->run_hook_reverse is DEPRECATED in favor of
Mojolicious::Plugins->emit_hook_reverse!
EOF
shift->emit_hook_reverse(@_);
}

sub _load {
Expand Down Expand Up @@ -104,42 +118,38 @@ Mojolicious::Plugins - Plugins
=head1 DESCRIPTION
L<Mojolicious::Plugins> is the plugin manager of L<Mojolicious>.
In your application you will usually use it to load plugins.
To implement your own plugins see L<Mojolicious::Plugin> and the C<add_hook>
method below.
=head1 ATTRIBUTES
L<Mojolicious::Plugins> implements the following attributes.
=head2 C<hooks>
my $hooks = $plugins->hooks;
$plugins = $plugins->hooks({foo => [sub {...}]});
Hash reference containing all hooks that have been registered by loaded
plugins.
=head2 C<namespaces>
my $namespaces = $plugins->namespaces;
$plugins = $plugins->namespaces(['Mojolicious::Plugin']);
Namespaces to load plugins from.
You can add more namespaces to load application specific plugins.
push @{$plugins->namespaces}, 'MyApp::Plugins';
=head1 METHODS
L<Mojolicious::Plugins> inherits all methods from L<Mojo::Base> and
L<Mojolicious::Plugins> inherits all methods from L<Mojo::EventEmitter> and
implements the following new ones.
=head2 C<add_hook>
=head2 C<emit_hook>
$plugins = $plugins->emit_hook('foo');
$plugins = $plugins->emit_hook(foo => 123);
Emit events as hooks.
$plugins = $plugins->add_hook(event => sub {...});
=head2 C<emit_hook_reverse>
Hook into an event.
You can also add custom events by calling C<run_hook> and C<run_hook_reverse>
from your application.
$plugins = $plugins->emit_hook_reverse('foo');
$plugins = $plugins->emit_hook_reverse(foo => 123);
Emit events as hooks in reverse order.
=head2 C<load_plugin>
Expand All @@ -162,22 +172,7 @@ Load a plugin from the configured namespaces or by full module name.
$plugins->register_plugin('MyApp::Plugin::SomeThing', $app, {foo => 23});
Load a plugin from the configured namespaces or by full module name and run
C<register>.
Optional arguments are passed to register.
=head2 C<run_hook>
$plugins = $plugins->run_hook('foo');
$plugins = $plugins->run_hook(foo => 123);
Runs a hook.
=head2 C<run_hook_reverse>
$plugins = $plugins->run_hook_reverse('foo');
$plugins = $plugins->run_hook_reverse(foo => 123);
Runs a hook in reverse order.
C<register>, optional arguments are passed through.
=head1 SEE ALSO
Expand Down
2 changes: 1 addition & 1 deletion t/pod_coverage.t
Expand Up @@ -15,7 +15,7 @@ my @sunglasses = (
);

# DEPRECATED in Leaf Fluttering In Wind!
my @leaf = (qw/on_finish is_done/);
my @leaf = (qw/add_hook is_done on_finish run_hook run_hook_reverse/);

# "Marge, I'm going to miss you so much. And it's not just the sex.
# It's also the food preparation."
Expand Down

0 comments on commit 6827e18

Please sign in to comment.