Permalink
Browse files

Deprecated the old plugin hook calling convention and added experimen…

…tal hook method to Mojolicious
  • Loading branch information...
1 parent 473f700 commit 3f59cda732c51e6e01ea069bbbc0e83e928bcbb7 Sebastian Riedel committed Oct 28, 2010
View
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.
0.999932 2010-10-26 00:00:00
+ - Deprecated the old plugin hook calling convention and added
+ EXPERIMENTAL hook method to Mojolicious.
- Fixed typos.
0.999931 2010-10-25 00:00:00
View
@@ -81,7 +81,7 @@ sub new {
my $tx = Mojo::Transaction::HTTP->new;
# Hook
- $self->plugins->run_hook(after_build_tx => $tx);
+ $self->plugins->run_hook(after_build_tx => ($tx, $self));
return $tx;
}
@@ -244,6 +244,11 @@ sub handler {
sub helper { shift->renderer->add_helper(@_) }
+sub hook {
+ my ($self, $name, $cb) = @_;
+ $self->plugins->add_hook($name, sub { shift; $cb->(@_) });
+}
+
sub plugin {
my $self = shift;
$self->plugins->register_plugin(shift, $self, @_);
@@ -589,6 +594,59 @@ Note that this method is EXPERIMENTAL and might change without warning!
# Template
<%= add 2, 3 %>
+=head2 C<hook>
+
+ $app->hook(after_dispatch => sub { ... });
+
+Add hooks to named events.
+Note that this method is EXPERIMENTAL and might change without warning!
+
+The following events are available and run in the listed order.
+
+=over 4
+
+=item after_build_tx
+
+Runs right after the transaction is built and before the HTTP request gets
+parsed.
+One use case would be upload progress bars.
+(Passed the transaction and application instances)
+
+ $app->hook(before_request => sub {
+ my ($tx, $app) = @_;
+ });
+
+=item before_dispatch
+
+Runs before the dispatchers determines what action to run.
+(Passed the default controller instance)
+
+ $app->hook(before_dispatch => sub {
+ my $self = shift;
+ });
+
+=item after_static_dispatch
+
+Runs after the static dispatcher determines if a static file should be
+served. (Passed the default controller instance)
+Note that the callbacks of this hook run in reverse order.
+
+ $app->hook(after_static_dispatch => sub {
+ my $self = shift;
+ });
+
+=item after_dispatch
+
+Runs after the dispatchers determines what action to run.
+(Passed the current controller instance)
+Note that the callbacks of this hook run in reverse order.
+
+ $app->hook(after_dispatch => sub {
+ my $self = shift;
+ });
+
+=back
+
=head2 C<plugin>
$app->plugin('something');
@@ -638,8 +638,7 @@ See C<render_data> for an alternative without encoding.
$c->rendered;
-Disable automatic rendering for response and run C<after_dispatch> plugin
-hook.
+Finalize response and run C<after_dispatch> plugin hook.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<send_message>
@@ -171,8 +171,8 @@ manually in your application.
sub startup {
my $self = shift;
- # Use plugin hook to set environment variable for every request
- $self->plugins->add_hook(
+ # Use event hook to set environment variable for every request
+ $self->hook(
before_dispatch => sub { $ENV{SYSTEMROOT} = 'c:\\winnt' }
);
}
@@ -15,21 +15,21 @@ sub register {
$conf ||= {};
# Set charset
- $app->plugins->add_hook(
+ $app->hook(
before_dispatch => sub {
- my ($self, $c) = @_;
+ my $self = shift;
# Got a charset
if (my $charset = $conf->{charset}) {
# This has to be done before params are cloned
- $c->tx->req->default_charset($charset);
+ $self->tx->req->default_charset($charset);
# Add charset to text/html content type
- my $type = $c->app->types->type('html');
+ my $type = $self->app->types->type('html');
unless ($type =~ /charset=/) {
$type .= ";charset=$charset";
- $c->app->types->type(html => $type);
+ $self->app->types->type(html => $type);
}
}
@@ -39,7 +39,7 @@ sub register {
defined $conf->{encoding}
? $conf->{encoding}
: $conf->{charset};
- $c->app->renderer->encoding($encoding) if $encoding;
+ $self->app->renderer->encoding($encoding) if $encoding;
}
);
}
@@ -29,24 +29,24 @@ sub register {
die qq/Couldn't initialize I18N class "$namespace": $@/ if $@;
# Start timer
- $app->plugins->add_hook(
+ $app->hook(
before_dispatch => sub {
- my ($self, $c) = @_;
+ my $self = shift;
# Header detection
my @languages = I18N::LangTags::implicate_supers(
I18N::LangTags::Detect->http_accept_langs(
- $c->req->headers->accept_language
+ $self->req->headers->accept_language
)
);
# Handler
- $c->stash->{i18n} =
+ $self->stash->{i18n} =
Mojolicious::Plugin::I18n::_Handler->new(
_namespace => $namespace);
# Languages
- $c->stash->{i18n}->languages(@languages, $default);
+ $self->stash->{i18n}->languages(@languages, $default);
}
);
@@ -216,7 +216,7 @@ L<Mojolicious::Plugin> and implements the following new ones.
$plugin->register;
-Register plugin hooks in L<Mojolicious> application.
+Register plugin in L<Mojolicious> application.
=head1 SEE ALSO
@@ -18,10 +18,9 @@ sub register {
my $name = $args->{name} || 'Mojolicious (Perl)';
# Add header
- $app->plugins->add_hook(
+ $app->hook(
after_build_tx => sub {
- my ($self, $tx) = @_;
- $tx->res->headers->header('X-Powered-By' => $name);
+ shift->res->headers->header('X-Powered-By' => $name);
}
);
}
@@ -13,26 +13,25 @@ sub register {
my ($self, $app) = @_;
# Start timer
- $app->plugins->add_hook(
+ $app->hook(
before_dispatch => sub {
- my ($self, $c) = @_;
- $c->stash('mojo.started' => [Time::HiRes::gettimeofday()]);
+ shift->stash('mojo.started' => [Time::HiRes::gettimeofday()]);
}
);
# End timer
- $app->plugins->add_hook(
+ $app->hook(
after_dispatch => sub {
- my ($self, $c) = @_;
- return unless my $started = $c->stash('mojo.started');
+ my $self = shift;
+ return unless my $started = $self->stash('mojo.started');
my $elapsed = sprintf '%f',
Time::HiRes::tv_interval($started,
[Time::HiRes::gettimeofday()]);
my $rps = $elapsed == 0 ? '??' : sprintf '%.3f', 1 / $elapsed;
- my $res = $c->res;
+ my $res = $self->res;
my $code = $res->code || 200;
my $message = $res->message || $res->default_message($code);
- $c->app->log->debug("$code $message (${elapsed}s, $rps/s).");
+ $self->app->log->debug("$code $message (${elapsed}s, $rps/s).");
}
);
}
View
@@ -75,7 +75,7 @@ sub run_hook {
return $self unless $name;
return unless $self->hooks->{$name};
- # Run
+ # DEPRECATED in Hot Beverage! (passing $self)
for my $hook (@{$self->hooks->{$name}}) { $self->$hook(@_) }
return $self;
@@ -90,7 +90,7 @@ sub run_hook_reverse {
return $self unless $name;
return unless $self->hooks->{$name};
- # Run
+ # DEPRECATED in Hot Beverage! (passing $self)
for my $hook (reverse @{$self->hooks->{$name}}) { $self->$hook(@_) }
return $self;
@@ -157,54 +157,8 @@ implements the following new ones.
$plugins = $plugins->add_hook(event => sub {...});
Hook into an event.
-The following events are available and run in the listed order.
-
-=over 4
-
-=item after_build_tx
-
-Runs right after the transaction is built and before the HTTP request gets
-parsed.
-One usage case would be upload progress bars.
-(Passed the transaction instance)
-
- $plugins->add_hook(before_request => sub {
- my ($self, $tx) = @_;
- });
-
-=item before_dispatch
-
-Runs before the dispatchers determines what action to run.
-(Passed the default controller instance)
-
- $plugins->add_hook(before_dispatch => sub {
- my ($self, $c) = @_;
- });
-
-=item after_static_dispatch
-
-Runs after the static dispatcher determines if a static file should be
-served. (Passed the default controller instance)
-Note that the callbacks of this hook run in reverse order.
-
- $plugins->add_hook(after_static_dispatch => sub {
- my ($self, $c) = @_;
- });
-
-=item after_dispatch
-
-Runs after the dispatchers determines what action to run.
-(Passed the default controller instance)
-Note that the callbacks of this hook run in reverse order.
-
- $plugins->add_hook(after_dispatch => sub {
- my ($self, $c) = @_;
- });
-
-=back
-
-You could also add custom events by using C<run_hook> and C<run_hook_reverse>
-in your application.
+You can also add custom events by calling C<run_hook> and C<run_hook_reverse>
+from your application.
=head2 C<load_plugin>
@@ -14,20 +14,20 @@ use Mojolicious::Lite;
use Test::Mojo;
# Custom dispatchers /custom
-app->plugins->add_hook(
+app->hook(
before_dispatch => sub {
- my ($self, $c) = @_;
- $c->render_text($c->param('a'), status => 205)
- if $c->req->url->path eq '/custom';
+ my $self = shift;
+ $self->render_text($self->param('a'), status => 205)
+ if $self->req->url->path eq '/custom';
}
);
# Custom dispatcher /custom_too
-app->plugins->add_hook(
+app->hook(
after_static_dispatch => sub {
- my ($self, $c) = @_;
- $c->render_text('this works too')
- if $c->req->url->path eq '/custom_too';
+ my $self = shift;
+ $self->render_text('this works too')
+ if $self->req->url->path eq '/custom_too';
}
);
View
@@ -405,12 +405,7 @@ get '/subrequest_sync' => sub {
};
# Make sure hook runs async
-app->plugins->add_hook(
- after_dispatch => sub {
- my ($self, $c) = @_;
- $c->stash->{async} = 'broken!';
- }
-);
+app->hook(after_dispatch => sub { shift->stash->{async} = 'broken!' });
# GET /subrequest_async
my $async;
@@ -20,9 +20,9 @@ use Test::Mojo;
# Upload progress
my $cache = {};
-app->plugins->add_hook(
+app->hook(
after_build_tx => sub {
- my ($self, $tx) = @_;
+ my $tx = shift;
$tx->req->on_progress(
sub {
my $req = shift;

0 comments on commit 3f59cda

Please sign in to comment.