1 package Class::C3::Componentised::ApplyHooks;
10 push @{$Before{scalar caller}}, $_[0];
11 $Class::C3::Componentised::APPLICATOR_FOR{scalar caller} = __PACKAGE__;
14 push @{$After {scalar caller}}, $_[0];
15 $Class::C3::Componentised::APPLICATOR_FOR{scalar caller} = __PACKAGE__;
18 sub _apply_component_to_class {
19 my ($me, $comp, $target, $apply) = @_;
20 my @heritage = @{mro::get_linear_isa($comp)};
23 my $to_run = $Before{$_};
24 ($to_run?[$_,$to_run]:())
27 for my $todo (@before) {
28 my ($parent, $fn) = @$todo;
29 for my $f (reverse @$fn) {
37 my $to_run = $After{$_};
38 ($to_run?[$_,$to_run]:())
41 for my $todo (reverse @after) {
42 my ($parent, $fn) = @$todo;
52 my ($from, @args) = @_;
66 if ($arg eq '-before_apply') {
69 push @{$Before{$to}}, $args[$i + 1];
70 $Class::C3::Componentised::APPLICATOR_FOR{$to} = $from;
71 } elsif ($arg eq '-after_apply') {
74 push @{$After{$to}}, $args[$i + 1];
75 $Class::C3::Componentised::APPLICATOR_FOR{$to} = $from;
76 } elsif ($arg =~ /^BEFORE_APPLY|AFTER_APPLY$/) {
82 @import = qw(BEFORE_APPLY AFTER_APPLY)
85 *{"$to\::$_"} = \&{"$from\::$_"} for @import
93 Class::C3::Componentised::ApplyHooks - Run methods before or after components are injected
101 use Class::C3::Componentised::ApplyHooks
102 -before_apply => sub {
103 my ($class, $component) = @_;
105 push @{$statistics{$class}}, '-before_apply';
107 -after_apply => sub {
108 my ($class, $component) = @_;
110 push @{$statistics{$class}}, '-after_apply';
111 }, qw(BEFORE_APPLY AFTER_APPLY);
113 BEFORE_APPLY { push @{$statistics{$class}}, 'BEFORE_APPLY' };
114 AFTER_APPLY { push @{$statistics{$class}}, 'AFTER_APPLY' };
115 AFTER_APPLY { use Devel::Dwarn; Dwarn %statistics };
121 This package allows a given component to run methods on the class that is being
122 injected into before or after the component is injected. Note from the
123 L</SYNOPSIS> that all C<Load Actions> may be run more than once.
127 Both import actions simply run a list of coderefs that will be passed the class
128 that is being acted upon and the component that is being added to the class.
130 =head1 IMPORT OPTIONS
134 Adds a before apply action for the current component without importing
135 any subroutines into your namespace.
139 Adds an after apply action for the current component without importing
140 any subroutines into your namespace.
142 =head1 EXPORTED SUBROUTINES
146 BEFORE_APPLY { warn "about to apply $_[1] to class $_[0]" };
148 Adds a before apply action for the current component.
152 AFTER_APPLY { warn "just applied $_[1] to class $_[0]" };
154 Adds an after apply action for the current component.