/usr/share/perl5/Class/C3/Componentised/ApplyHooks.pm is in libclass-c3-componentised-perl 1.001000-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | package Class::C3::Componentised::ApplyHooks;
use strict;
use warnings;
our %Before;
our %After;
sub BEFORE_APPLY (&) {
push @{$Before{scalar caller}}, $_[0];
$Class::C3::Componentised::APPLICATOR_FOR{scalar caller} = __PACKAGE__;
}
sub AFTER_APPLY (&) {
push @{$After {scalar caller}}, $_[0];
$Class::C3::Componentised::APPLICATOR_FOR{scalar caller} = __PACKAGE__;
}
sub _apply_component_to_class {
my ($me, $comp, $target, $apply) = @_;
my @heritage = @{mro::get_linear_isa($comp)};
my @before = map {
my $to_run = $Before{$_};
($to_run?[$_,$to_run]:())
} @heritage;
for my $todo (@before) {
my ($parent, $fn) = @$todo;
for my $f (reverse @$fn) {
$target->$f($parent)
}
}
$apply->();
my @after = map {
my $to_run = $After{$_};
($to_run?[$_,$to_run]:())
} @heritage;
for my $todo (reverse @after) {
my ($parent, $fn) = @$todo;
for my $f (@$fn) {
$target->$f($parent)
}
}
}
{
no strict 'refs';
sub import {
my ($from, @args) = @_;
my $to = caller;
my $default = 1;
my $i = 0;
my $skip = 0;
my @import;
for my $arg (@args) {
if ($skip) {
$skip--;
$i++;
next
}
if ($arg eq '-before_apply') {
$default = 0;
$skip = 1;
push @{$Before{$to}}, $args[$i + 1];
$Class::C3::Componentised::APPLICATOR_FOR{$to} = $from;
} elsif ($arg eq '-after_apply') {
$default = 0;
$skip = 1;
push @{$After{$to}}, $args[$i + 1];
$Class::C3::Componentised::APPLICATOR_FOR{$to} = $from;
} elsif ($arg =~ /^BEFORE_APPLY|AFTER_APPLY$/) {
$default = 0;
push @import, $arg
}
$i++;
}
@import = qw(BEFORE_APPLY AFTER_APPLY)
if $default;
*{"$to\::$_"} = \&{"$from\::$_"} for @import
}
}
1;
=head1 NAME
Class::C3::Componentised::ApplyHooks - allow component to run methods on the class that is being injected into
=head1 SYNOPSIS
package MyComponent;
our %statistics;
use Class::C3::Componentised::ApplyHooks
-before_apply => sub {
my ($class, $component) = @_;
push @{$statistics{$class}}, '-before_apply';
},
-after_apply => sub {
my ($class, $component) = @_;
push @{$statistics{$class}}, '-after_apply';
}, qw(BEFORE_APPLY AFTER_APPLY);
BEFORE_APPLY { push @{$statistics{$class}}, 'BEFORE_APPLY' };
AFTER_APPLY { push @{$statistics{$class}}, 'AFTER_APPLY' };
AFTER_APPLY { use Devel::Dwarn; Dwarn %statistics };
1;
=head1 DESCRIPTION
This package allows a given component to run methods on the class that is being
injected into before or after the component is injected. Note from the
L</SYNOPSIS> that all C<Load Actions> may be run more than once.
=head1 IMPORT ACTION
Both import actions simply run a list of coderefs that will be passed the class
that is being acted upon and the component that is being added to the class.
=head1 IMPORT OPTIONS
=head2 -before_apply
Adds a before apply action for the current component without importing
any subroutines into your namespace.
=head2 -after_apply
Adds an after apply action for the current component without importing
any subroutines into your namespace.
=head1 EXPORTED SUBROUTINES
=head2 BEFORE_APPLY
BEFORE_APPLY { warn "about to apply $_[1] to class $_[0]" };
Adds a before apply action for the current component.
=head2 AFTER_APPLY
AFTER_APPLY { warn "just applied $_[1] to class $_[0]" };
Adds an after apply action for the current component.
=cut
|