Skip to content

Commit

Permalink
made all Mojolicious after_* plugin hooks run in reverse order
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 11, 2010
1 parent dcc26a4 commit e8e3c1f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -25,6 +25,7 @@ This file documents the revision history for Perl extension Mojo.
- Added version command.
- Added after_build_tx plugin hook.
- Added timer support to Mojo::IOLoop.
- Made all Mojolicious after_* plugin hooks run in reverse order.
- Made param decoding more defensive and allow malformed data to pass
through for debugging.
- Reduced Mojolicious log output outside of development mode.
Expand Down
6 changes: 3 additions & 3 deletions lib/Mojolicious.pm
Expand Up @@ -33,7 +33,7 @@ sub new {
my $tx = Mojo::Transaction::Single->new;

# Hook
$self->plugins->run_hook(after_build_tx => $tx);
$self->plugins->run_hook_reverse(after_build_tx => $tx);

return $tx;
}
Expand Down Expand Up @@ -100,7 +100,7 @@ sub dispatch {
my $e = $self->static->dispatch($c);

# Hook
$self->plugins->run_hook(after_static_dispatch => $c);
$self->plugins->run_hook_reverse(after_static_dispatch => $c);

# Use routes if we don't have a response yet
$e = $self->routes->dispatch($c) if $e;
Expand All @@ -112,7 +112,7 @@ sub dispatch {
elsif ($e) { $c->render_not_found }

# Hook
$self->plugins->run_hook(after_dispatch => $c);
$self->plugins->run_hook_reverse(after_dispatch => $c);
}

# Bite my shiny metal ass!
Expand Down
19 changes: 19 additions & 0 deletions lib/Mojolicious/Plugins.pm
Expand Up @@ -87,6 +87,20 @@ sub run_hook {
return $self;
}

sub run_hook_reverse {
my $self = shift;

# Shortcut
my $name = shift;
return $self unless $name;
return unless $self->hooks->{$name};

# Run
$self->$_(@_) for reverse @{$self->hooks->{$name}};

return $self;
}

1;
__END__
Expand Down Expand Up @@ -136,4 +150,9 @@ implements the following new ones.
$plugins = $plugins->run_hook('foo');
$plugins = $plugins->run_hook(foo => 123);
=head2 C<run_hook_reverse>
$plugins = $plugins->run_hook_reverse('foo');
$plugins = $plugins->run_hook_reverse(foo => 123);
=cut

0 comments on commit e8e3c1f

Please sign in to comment.