Skip to content

Commit

Permalink
Item14152: Fixed a problem with applying roles
Browse files Browse the repository at this point in the history
Attempt to apply roles to an unfinished class (i.e. before the 'extends'
clause is done it's job) were causing problems with missing methods if
a role using before/around/after on a method which is declared by an
ancestor class only.
  • Loading branch information
vrurg committed Jun 17, 2017
1 parent 4f82634 commit e9ecff3
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions core/lib/Foswiki/Class.pm
Expand Up @@ -128,7 +128,7 @@ sub _fw_has {
my $target = shift;
my ($attr) = @_;

#say STDERR "Registering attr $attr on $target";
#say STDERR "Registering attr `$attr' on $target";

push @{ $_classData{$target}{registeredAttrs}{list} },
{ attr => $attr, options => [ @_[ 1 .. $#_ ] ] };
Expand Down Expand Up @@ -158,14 +158,16 @@ sub _fw_extends {

# Install BUILD method if a feature requiring it requested.
# Otherwise feature implementation role will fail to apply cleanly.
unless ( defined $trg_ns->{BUILD}
&& defined *{ $trg_ns->{BUILD} }{CODE} )
{
#say STDERR "Installing BUILD for $target";
install_modifier( $target, fresh => BUILD => sub { } );
}
#unless ( defined $trg_ns->{BUILD}
# && defined *{ $trg_ns->{BUILD} }{CODE} )
#{
# #say STDERR "Installing BUILD for $target";
# install_modifier( $target, fresh => BUILD => sub { } );
#}
}
__PACKAGE__->_apply_roles;

#say STDERR "Applying roles to $target";
__PACKAGE__->_apply_roles($target);
}

if ( $ENV{FOSWIKI_ASSERTS} ) {
Expand Down Expand Up @@ -199,8 +201,12 @@ foreach my $module (qw(Moo Moo::Role)) {
#say STDERR "Installing wrapper $codeName on $target";
my $origCode = $_[2];
$_[2] = sub {

#say STDERR "Orig ${target}::$codeName code first.";
&$origCode(@_);

#say STDERR "Extension ${target}::$codeName code next.";
$ovCode->( $target, @_ );
goto &$origCode;
};
}
goto &$_install_tracked;
Expand All @@ -214,7 +220,7 @@ sub import {
my ($class) = shift;
my $target = caller;

#say STDERR "Foswiki::Class($class, $target)";
#say STDERR "--- Foswiki::Class($class, $target)";

$SIG{__DIE__} = sub { Carp::confess(@_) };

Expand Down Expand Up @@ -320,8 +326,8 @@ sub getClassAttributes {
return _getAllAttrs($class);
}

# Actually we're duplicating Moo::_install_coderef here in a way. But we better
# avoid using a module's internalls.
# Actually _inject_code is duplicating Moo::_install_coderef in a way. But usage
# of Moo internals is better be avoided.
sub _inject_code {
my ( $target, $name, $code ) = @_;

Expand All @@ -330,11 +336,12 @@ sub _inject_code {

sub _apply_roles {
my $class = shift;
foreach my $target (
grep { defined $_classData{$_}{assignedRoles} }
keys %_classData
)
{

my @targets =
grep { defined $_classData{$_}{assignedRoles} }
( scalar(@_) ? @_ : keys %_classData );

foreach my $target (@targets) {

#say STDERR "Applying roles ",
# join( ", ", @{ $_classData{$target}{assignedRoles} } ), " to $target";
Expand Down

0 comments on commit e9ecff3

Please sign in to comment.