Commit | Line | Data |
ab3370e7 |
1 | package Role::Tiny; |
2 | |
3 | use strictures 1; |
6c74d087 |
4 | use Class::Tiny::_Utils; |
ab3370e7 |
5 | |
6 | our %INFO; |
7 | our %APPLIED_TO; |
1947330a |
8 | our %COMPOSED; |
ab3370e7 |
9 | |
10 | sub import { |
11 | my $target = caller; |
de3d4906 |
12 | strictures->import; |
ab3370e7 |
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)) { |
5a247406 |
17 | *{_getglob "${target}::${type}"} = sub { |
ab3370e7 |
18 | push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ]; |
19 | }; |
20 | } |
5a247406 |
21 | *{_getglob "${target}::requires"} = sub { |
ab3370e7 |
22 | push @{$INFO{$target}{requires}||=[]}, @_; |
23 | }; |
5a247406 |
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); |
27 | }; |
96d3f07a |
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; |
35 | }; |
ab3370e7 |
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 |
39 | # is used later) |
40 | @{$INFO{$target}{not_methods}={}}{ |
41 | '', map { *$_{CODE}||() } grep !(ref eq 'SCALAR'), values %$stash |
42 | } = (); |
43 | # a role does itself |
44 | $APPLIED_TO{$target} = { $target => undef }; |
45 | } |
46 | |
47 | sub apply_role_to_package { |
1947330a |
48 | my ($me, $role, $to) = @_; |
49 | |
ab3370e7 |
50 | die "This is apply_role_to_package" if ref($to); |
1947330a |
51 | die "${role} is not a Role::Tiny" unless my $info = $INFO{$role}; |
52 | |
53 | $me->_check_requires($to, $role, @{$info->{requires}||[]}); |
54 | |
55 | $me->_install_methods($to, $role); |
56 | |
57 | $me->_install_modifiers($to, $info->{modifiers}); |
58 | |
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; |
62 | } |
63 | |
64 | $me->_handle_constructor($to, $info->{attributes}); |
65 | |
66 | # copy our role list into the target's |
67 | @{$APPLIED_TO{$to}||={}}{keys %{$APPLIED_TO{$role}}} = (); |
68 | } |
69 | |
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)); |
75 | $object; |
76 | } |
77 | |
78 | sub create_class_with_roles { |
79 | my ($me, $superclass, @roles) = @_; |
80 | |
81 | my $new_name = join('+', $superclass, my $compose_name = join '+', @roles); |
82 | return $new_name if $COMPOSED{class}{$new_name}; |
83 | |
84 | foreach my $role (@roles) { |
85 | die "${role} is not a Role::Tiny" unless my $info = $INFO{$role}; |
86 | } |
87 | |
88 | if ($] > 5.010) { |
89 | require mro; |
90 | } else { |
91 | require MRO::Compat; |
92 | } |
93 | |
94 | require Sub::Quote; |
95 | |
96 | my @composable = map $me->_composable_package_for($_), reverse @roles; |
97 | |
98 | *{_getglob("${new_name}::ISA")} = [ @composable, $superclass ]; |
99 | |
100 | my @info = map +($INFO{$_} ? $INFO{$_} : ()), @roles; |
101 | |
102 | $me->_check_requires( |
103 | $new_name, $compose_name, |
104 | do { my %h; @h{map @{$_->{requires}||[]}, @info} = (); keys %h } |
105 | ); |
106 | $me->_handle_constructor( |
107 | $new_name, { map %{$_->{attr_info}||{}}, @info } |
108 | ); |
109 | |
110 | *{_getglob "${new_name}::does"} = \&does_role unless $new_name->can('does'); |
111 | |
112 | @{$APPLIED_TO{$new_name}||={}}{ |
113 | map keys %{$APPLIED_TO{$_}}, @roles |
114 | } = (); |
115 | |
116 | $COMPOSED{class}{$new_name} = 1; |
117 | return $new_name; |
118 | } |
119 | |
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 } |
130 | ) { |
131 | Sub::Quote::quote_sub( |
132 | "${base_name}::${modified}" => q{ shift->next::method(@_) } |
133 | ); |
134 | } |
135 | $me->_install_modifiers($composed_name, $modifiers); |
136 | $COMPOSED{role}{$composed_name} = 1; |
137 | return $composed_name; |
138 | } |
139 | |
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; |
146 | } else { |
147 | die "Can't apply ${name} to ${to} - missing ".join(', ', @requires_fail); |
148 | } |
149 | } |
150 | } |
151 | |
152 | sub _install_methods { |
153 | my ($me, $to, $role) = @_; |
154 | |
155 | my $info = $INFO{$role}; |
156 | |
ab3370e7 |
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}; |
161 | +{ |
162 | # grab all code entries that aren't in the not_methods list |
163 | map { |
934ea2c1 |
164 | my $code = *{$stash->{$_}}{CODE}; |
165 | # rely on the '' key we added in import for "no code here" |
166 | exists $not_methods->{$code||''} ? () : ($_ => $code) |
ab3370e7 |
167 | } grep !(ref($stash->{$_}) eq 'SCALAR'), keys %$stash |
168 | }; |
169 | }; |
1947330a |
170 | |
ab3370e7 |
171 | # grab target symbol table |
172 | my $stash = do { no strict 'refs'; \%{"${to}::"}}; |
1947330a |
173 | |
ab3370e7 |
174 | # determine already extant methods of target |
175 | my %has_methods; |
176 | @has_methods{grep |
177 | +((ref($stash->{$_}) eq 'SCALAR') || (*{$stash->{$_}}{CODE})), |
178 | keys %$stash |
179 | } = (); |
ab3370e7 |
180 | |
1947330a |
181 | foreach my $i (grep !exists $has_methods{$_}, keys %$methods) { |
ab3370e7 |
182 | no warnings 'once'; |
5a247406 |
183 | *{_getglob "${to}::${i}"} = $methods->{$i}; |
ab3370e7 |
184 | } |
1947330a |
185 | } |
ab3370e7 |
186 | |
1947330a |
187 | sub _install_modifiers { |
188 | my ($me, $to, $modifiers) = @_; |
189 | foreach my $modifier (@{$modifiers||[]}) { |
6c74d087 |
190 | _install_modifier($to, @{$modifier}); |
ab3370e7 |
191 | } |
1947330a |
192 | } |
ab3370e7 |
193 | |
1947330a |
194 | sub _handle_constructor { |
195 | my ($me, $to, $attr_info) = @_; |
196 | return unless $attr_info && keys %$attr_info; |
197 | if ($INFO{$to}) { |
198 | @{$INFO{$to}{attributes}||={}}{keys %$attr_info} = values %$attr_info; |
199 | } else { |
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); |
96d3f07a |
204 | } |
205 | } |
ab3370e7 |
206 | } |
207 | |
208 | sub does_role { |
209 | my ($package, $role) = @_; |
210 | return exists $APPLIED_TO{$package}{$role}; |
211 | } |
212 | |
213 | 1; |