Skip to content

Commit

Permalink
fixed small chained hook bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 17, 2013
1 parent eeff0ff commit 587b100
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -4,6 +4,7 @@
as $_. (jberger, sri)
- Fixed a few error reporting bugs in Mojo::IOLoop::Client and
Mojo::IOLoop::Server.
- Fixed small chained hook bug.

4.14 2013-06-10
- Improved url_for performance slightly.
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojolicious/Plugins.pm
Expand Up @@ -19,9 +19,8 @@ sub emit_chain {
my $next = $wrapper;
$wrapper = sub { $cb->($next, @args) };
}
$wrapper->();

return $self;
!$wrapper ? return : return $wrapper->();
}

sub emit_hook_reverse {
Expand Down Expand Up @@ -151,8 +150,8 @@ implements the following new ones.
=head2 emit_chain
$plugins = $plugins->emit_chain('foo');
$plugins = $plugins->emit_chain(foo => 123);
$plugins->emit_chain('foo');
$plugins->emit_chain(foo => 123);
Emit events as chained hooks.
Expand Down
28 changes: 14 additions & 14 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -139,36 +139,36 @@ sub _class {
}

sub _controller {
my ($self, $c, $field, $nested) = @_;
my ($self, $old, $field, $nested) = @_;

# Load and instantiate controller/application
my $app;
unless ($app = $self->_class($c, $field)) { return defined $app ? 1 : undef }
my $new;
unless ($new = $self->_class($old, $field)) { return !!defined $new }

# Application
my $continue;
my $class = ref $app;
my $log = $c->app->log;
if (my $sub = $app->can('handler')) {
my $class = ref $new;
my $log = $old->app->log;
if (my $sub = $new->can('handler')) {
$log->debug(qq{Routing to application "$class".});

# Try to connect routes
if (my $sub = $app->can('routes')) {
my $r = $app->$sub;
weaken $r->parent($c->match->endpoint)->{parent} unless $r->parent;
if (my $sub = $new->can('routes')) {
my $r = $new->$sub;
weaken $r->parent($old->match->endpoint)->{parent} unless $r->parent;
}
$app->$sub($c);
$c->stash->{'mojo.routed'}++;
$new->$sub($old);
$old->stash->{'mojo.routed'}++;
}

# Action
elsif (my $method = $field->{action}) {
if (!$self->is_hidden($method)) {
$log->debug(qq{Routing to controller "$class" and action "$method".});

if (my $sub = $app->can($method)) {
$c->stash->{'mojo.routed'}++ unless $nested;
$continue = $sub->(local $_ = $app);
if (my $sub = $new->can($method)) {
$old->stash->{'mojo.routed'}++ unless $nested;
$continue = $sub->(local $_ = $new);
}

else { $log->debug('Action not found in controller.') }
Expand Down
8 changes: 8 additions & 0 deletions t/mojolicious/app.t
Expand Up @@ -100,6 +100,14 @@ ok $t->app->routes->is_hidden('url_for'), 'is hidden';
ok $t->app->routes->is_hidden('write'), 'is hidden';
ok $t->app->routes->is_hidden('write_chunk'), 'is hidden';

# Unknown hooks
ok !$t->app->plugins->emit_chain('does_not_exist'),
'unknown chained hook has been emitted';
ok !!$t->app->plugins->emit_hook('does_not_exist'),
'unknown hook has been emitted';
ok !!$t->app->plugins->emit_hook_reverse('does_not_exist'),
'unknown hook has been emitted';

# MojoliciousTest::Command::test_command (with abbreviation)
is $t->app->start(qw(test_command --to)), 'works too!', 'right result';

Expand Down

0 comments on commit 587b100

Please sign in to comment.