4 use Class::Tiny::_Utils;
13 # get symbol table reference
14 my $stash = do { no strict 'refs'; \%{"${target}::"} };
15 # install before/after/around subs
16 foreach my $type (qw(before after around)) {
17 *{_getglob "${target}::${type}"} = sub {
18 push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ];
21 *{_getglob "${target}::requires"} = sub {
22 push @{$INFO{$target}{requires}||=[]}, @_;
24 *{_getglob "${target}::with"} = sub {
25 die "Only one role supported at a time by with" if @_ > 1;
26 Role::Tiny->apply_role_to_package($_[0], $target);
28 *{_getglob "${target}::has"} = sub {
29 my ($name, %spec) = @_;
30 ($INFO{$target}{accessor_maker} ||= do {
31 require Method::Generate::Accessor;
32 Method::Generate::Accessor->new
33 })->generate_method($target, $name, \%spec);
34 $INFO{$target}{attributes}{$name} = \%spec;
36 # grab all *non-constant* (ref eq 'SCALAR') subs present
37 # in the symbol table and store their refaddrs (no need to forcibly
38 # inflate constant subs into real subs) - also add '' to here (this
40 @{$INFO{$target}{not_methods}={}}{
41 '', map { *$_{CODE}||() } grep !(ref eq 'SCALAR'), values %$stash
44 $APPLIED_TO{$target} = { $target => undef };
47 sub apply_role_to_package {
48 my ($me, $role, $to) = @_;
50 die "This is apply_role_to_package" if ref($to);
51 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
53 $me->_check_requires($to, $role, @{$info->{requires}||[]});
55 $me->_install_methods($to, $role);
57 $me->_install_modifiers($to, $info->{modifiers});
59 # only add does() method to classes and only if they don't have one
60 if (not $INFO{$to} and not $to->can('does')) {
61 *{_getglob "${to}::does"} = \&does_role;
64 $me->_handle_constructor($to, $info->{attributes});
66 # copy our role list into the target's
67 @{$APPLIED_TO{$to}||={}}{keys %{$APPLIED_TO{$role}}} = ();
70 sub apply_roles_to_object {
71 my ($me, $object, @roles) = @_;
72 die "No roles supplied!" unless @roles;
73 my $class = ref($object);
74 bless($object, $me->create_class_with_roles($class, @roles));
78 sub create_class_with_roles {
79 my ($me, $superclass, @roles) = @_;
81 my $new_name = join('+', $superclass, my $compose_name = join '+', @roles);
82 return $new_name if $COMPOSED{class}{$new_name};
84 foreach my $role (@roles) {
85 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
96 my @composable = map $me->_composable_package_for($_), reverse @roles;
98 *{_getglob("${new_name}::ISA")} = [ @composable, $superclass ];
100 my @info = map +($INFO{$_} ? $INFO{$_} : ()), @roles;
102 $me->_check_requires(
103 $new_name, $compose_name,
104 do { my %h; @h{map @{$_->{requires}||[]}, @info} = (); keys %h }
106 $me->_handle_constructor(
107 $new_name, { map %{$_->{attr_info}||{}}, @info }
110 *{_getglob "${new_name}::does"} = \&does_role unless $new_name->can('does');
112 @{$APPLIED_TO{$new_name}||={}}{
113 map keys %{$APPLIED_TO{$_}}, @roles
116 $COMPOSED{class}{$new_name} = 1;
120 sub _composable_package_for {
121 my ($me, $role) = @_;
122 my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
123 return $composed_name if $COMPOSED{role}{$composed_name};
124 $me->_install_methods($composed_name, $role);
125 my $base_name = $composed_name.'::_BASE';
126 *{_getglob("${composed_name}::ISA")} = [ $base_name ];
127 my $modifiers = $INFO{$role}{modifiers}||[];
128 foreach my $modified (
129 do { my %h; @h{map $_->[1], @$modifiers} = (); keys %h }
131 Sub::Quote::quote_sub(
132 "${base_name}::${modified}" => q{ shift->next::method(@_) }
135 $me->_install_modifiers($composed_name, $modifiers);
136 $COMPOSED{role}{$composed_name} = 1;
137 return $composed_name;
140 sub _check_requires {
141 my ($me, $to, $name, @requires) = @_;
142 if (my @requires_fail = grep !$to->can($_), @requires) {
143 # role -> role, add to requires, role -> class, error out
144 if (my $to_info = $INFO{$to}) {
145 push @{$to_info->{requires}||=[]}, @requires_fail;
147 die "Can't apply ${name} to ${to} - missing ".join(', ', @requires_fail);
152 sub _install_methods {
153 my ($me, $to, $role) = @_;
155 my $info = $INFO{$role};
157 my $methods = $info->{methods} ||= do {
158 # grab role symbol table
159 my $stash = do { no strict 'refs'; \%{"${role}::"}};
160 my $not_methods = $info->{not_methods};
162 # grab all code entries that aren't in the not_methods list
164 my $code = *{$stash->{$_}}{CODE};
165 # rely on the '' key we added in import for "no code here"
166 exists $not_methods->{$code||''} ? () : ($_ => $code)
167 } grep !(ref($stash->{$_}) eq 'SCALAR'), keys %$stash
171 # grab target symbol table
172 my $stash = do { no strict 'refs'; \%{"${to}::"}};
174 # determine already extant methods of target
177 +((ref($stash->{$_}) eq 'SCALAR') || (*{$stash->{$_}}{CODE})),
181 foreach my $i (grep !exists $has_methods{$_}, keys %$methods) {
183 *{_getglob "${to}::${i}"} = $methods->{$i};
187 sub _install_modifiers {
188 my ($me, $to, $modifiers) = @_;
189 foreach my $modifier (@{$modifiers||[]}) {
190 _install_modifier($to, @{$modifier});
194 sub _handle_constructor {
195 my ($me, $to, $attr_info) = @_;
196 return unless $attr_info && keys %$attr_info;
198 @{$INFO{$to}{attributes}||={}}{keys %$attr_info} = values %$attr_info;
200 # only fiddle with the constructor if the target is a Class::Tiny class
201 if ($INC{"Class/Tiny.pm"}
202 and my $con = Class::Tiny->_constructor_maker_for($to)) {
203 $con->register_attribute_specs(%$attr_info);
209 my ($package, $role) = @_;
210 return exists $APPLIED_TO{$package}{$role};