return $output;
}
+#
+# Before/after run hooks
+#
+
+sub run_before {
+ _add_hook('before', @_);
+}
+
+sub run_after {
+ _add_hook('after', @_);
+}
+
+my %hooks;
+
+sub _add_hook {
+ my ($when, $class, $sub, %params) = @_;
+
+ foreach my $key (qw(only except)) {
+ $params{$key} = { map { ( $_ => 1 ) } @{ $params{$key} } } if $params{$key};
+ }
+
+ my $idx = "${when}/${class}";
+ $hooks{$idx} ||= [ ];
+ push @{ $hooks{$idx} }, { %params, code => $sub };
+}
+
+sub _run_hooks {
+ my ($self, $when, $action) = @_;
+
+ my $idx = "${when}/" . ref($self);
+
+ foreach my $hook (@{ $hooks{$idx} || [] }) {
+ next if ($hook->{only } && !$hook->{only }->{$action})
+ || ($hook->{except} && $hook->{except}->{$action});
+
+ if (ref($hook->{code}) eq 'CODE') {
+ $hook->{code}->($self);
+ } else {
+ my $sub = $hook->{code};
+ $self->$sub;
+ }
+ }
+}
+
#
# private functions -- for use in Base only
#
sub _run_action {
my $self = shift;
- my $action = "action_" . shift;
+ my $action = shift;
+ my $sub = "action_${action}";
+
+ return $self->_dispatch(@_) if $action eq 'dispatch';
- return $self->_dispatch(@_) if $action eq 'action_dispatch';
+ $::form->error("Invalid action '${action}' for controller " . ref($self)) if !$self->can($sub);
- $::form->error("Invalid action ${action} for controller " . ref($self)) if !$self->can($action);
- $self->$action(@_);
+ $self->_run_hooks('before', $action);
+ $self->$sub(@_);
+ $self->_run_hooks('after', $action);
}
sub _controller_name {
my $self = shift;
no strict 'refs';
- my @actions = grep { m/^action_/ } keys %{ ref($self) . "::" };
- my $action = first { $::form->{$_} } @actions;
+ my @actions = map { s/^action_//; $_ } grep { m/^action_/ } keys %{ ref($self) . "::" };
+ my $action = first { $::form->{"action_${_}"} } @actions;
+ my $sub = "action_${action}";
- $self->$action(@_);
+ $self->_run_hooks('before', $action);
+ $self->$sub(@_);
+ $self->_run_hooks('after', $action);
}
sub _template_obj {
The dispatching is handled by the function L</_dispatch>.
+=head2 HOOKS
+
+Hooks are functions that are called before or after the controller's
+action is called. The controller package defines the hooks, and those
+hooks themselves are run as instance methods.
+
+Hooks are run in the order they're added.
+
+The return value of the hooks is discarded.
+
+Hooks can be defined to run for all actions, for only specific actions
+or for all actions except a list of actions. Each entry is the action
+name, not the sub's name. Therefore in order to run a hook before one
+of the subs C<action_edit> or C<action_save> is called the following
+code can be used:
+
+ __PACKAGE__->run_before('things_to_do_before_edit_and_save', only => [ 'edit', 'save' ]);
+
=head1 FUNCTIONS
=head2 PUBLIC HELPER FUNCTIONS
header. The URL is generated by calling L</url_for> with
C<%url_params>.
+=item C<run_before $sub, %params>
+
+=item C<run_after $sub, %params>
+
+Adds a hook to run before or after certain actions are run for the
+current package. The code to run is C<$sub> which is either the name
+of an instance method or a code reference. If it's the latter then the
+first parameter will be C<$self>.
+
+C<%params> can contain two possible values that restrict the code to
+be run only for certain actions:
+
+=over 2
+
+=item C<< only => \@list >>
+
+Only run the code for actions given in C<@list>. The entries are the
+action names, not the names of the sub (so it's C<list> instead of
+C<action_list>).
+
+=item C<< except => \@list >>
+
+Run the code for all actions but for those given in C<@list>. The
+entries are the action names, not the names of the sub (so it's
+C<list> instead of C<action_list>).
+
+=back
+
+If neither restriction is used then the code will be run for any
+action.
+
+The hook's return values are discarded.
+
=back
=head2 PRIVATE FUNCTIONS