Commit 54539e97 authored by Xavier Guimard's avatar Xavier Guimard

Add aroundSub entrypoint

parent 06431d71
Pipeline #3884 passed with stages
in 9 minutes and 42 seconds
......@@ -69,7 +69,8 @@ BEGIN {
}
# Endpoints inserted after any main sub
has 'afterSub' => ( is => 'rw', default => sub { {} } );
has 'afterSub' => ( is => 'rw', default => sub { {} } );
has 'aroundSub' => ( is => 'rw', default => sub { {} } );
has spRules => (
is => 'rw',
......@@ -366,7 +367,7 @@ sub findEP {
eval {
$obj->logger->debug("Launching ${plugin}::$callback");
};
$obj->$callback( $_[0] );
$obj->$callback(@_);
};
$self->logger->debug(" -> $callback");
}
......@@ -387,7 +388,30 @@ sub findEP {
$obj->logger->debug(
"Launching ${plugin}::$callback afterSub $ep");
};
$obj->$callback( $_[0] );
$obj->$callback(@_);
};
}
}
}
if ( $obj->can('aroundSub') ) {
$self->logger->debug("Found aroundSub in $plugin");
my $h = $obj->aroundSub;
unless ( ref $h and ref($h) eq 'HASH' ) {
$self->logger->error(
'"aroundSub" endpoint must be a hashref, skipped');
}
else {
foreach my $ep ( keys %$h ) {
my $callback = $h->{$ep};
my $previousSub = $self->aroundSub->{$ep} ||= sub {
$self->logger->debug(
"$ep launched inside ${plugin}::$callback");
$self->$ep(@_);
};
$self->aroundSub->{$ep} = sub {
$self->logger->debug(
"Launching ${plugin}::$callback instead of $ep");
$obj->$callback( $previousSub, @_ );
};
}
}
......
......@@ -170,6 +170,17 @@ process
=item C<beforeLogout>: method called before logout
=back
B<Note>: methods inserted so must return a PE_* constant. See
Lemonldap::NG::Portal::Main::Constants.
=head4 Advanced entry points
These entry points are not stored in C<$req-E<gt>step> but launched on the fly:
=over
=item C<afterSub>: hash ref that give methods to call after given main method
is called. Example:
......@@ -182,10 +193,24 @@ is called. Example:
return PE_OK;
}
=back
=item C<aroundSub>: hash ref that give methods to call instead of given main
method. Example:
B<Note>: methods inserted so must return a PE_* constant. See
Lemonldap::NG::Portal::Main::Constants.
use constant aroundSub => {
getUser => 'mysub',
};
sub mysub {
my ( $self, $sub, $req ) = @_;
# Do something before
my $ret = $sub->($req);
# Do something after
return $ret;
}
Do not launch "getUser" but use the given C<$sub>. This permits multiple
plugins to use "aroundSub" in the same time.
=back
=head1 SEE ALSO
......
......@@ -27,7 +27,12 @@ sub process {
}
else {
$self->logger->debug("Processing $sub");
last if ( $err = $self->$sub( $req, %args ) );
if ( my $as = $self->aroundSub->{$sub} ) {
last if ( $err = $as->( $req, %args ) );
}
else {
last if ( $err = $self->$sub( $req, %args ) );
}
if ( $self->afterSub->{$sub} ) {
unshift @{ $req->steps }, @{ $self->afterSub->{$sub} };
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment