Commit | Line | Data |
d245e471 |
1 | package Moo::Role; |
2 | |
3 | use strictures 1; |
4 | use Moo::_Utils; |
5 | use base qw(Role::Tiny); |
6 | |
a047096b |
7 | require Moo::sification; |
8 | |
d245e471 |
9 | BEGIN { *INFO = \%Role::Tiny::INFO } |
10 | |
11 | our %INFO; |
12 | |
13 | sub import { |
14 | my $target = caller; |
6c49212f |
15 | my ($me) = @_; |
d245e471 |
16 | strictures->import; |
1ba11455 |
17 | return if $INFO{$target}; # already exported into this package |
d245e471 |
18 | # get symbol table reference |
19 | my $stash = do { no strict 'refs'; \%{"${target}::"} }; |
167455a0 |
20 | _install_coderef "${target}::has" => "Moo::Role::has" => sub { |
d245e471 |
21 | my ($name, %spec) = @_; |
22 | ($INFO{$target}{accessor_maker} ||= do { |
faa9ce11 |
23 | require Method::Generate::Accessor; |
d245e471 |
24 | Method::Generate::Accessor->new |
25 | })->generate_method($target, $name, \%spec); |
57d402ef |
26 | push @{$INFO{$target}{attributes}||=[]}, $name, \%spec; |
6c49212f |
27 | $me->_maybe_reset_handlemoose($target); |
d245e471 |
28 | }; |
6c49212f |
29 | # install before/after/around subs |
30 | foreach my $type (qw(before after around)) { |
31 | *{_getglob "${target}::${type}"} = sub { |
32 | require Class::Method::Modifiers; |
33 | push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ]; |
34 | $me->_maybe_reset_handlemoose($target); |
35 | }; |
36 | } |
37 | *{_getglob "${target}::requires"} = sub { |
38 | push @{$INFO{$target}{requires}||=[]}, @_; |
39 | $me->_maybe_reset_handlemoose($target); |
40 | }; |
41 | *{_getglob "${target}::with"} = sub { |
42 | $me->apply_roles_to_package($target, @_); |
43 | $me->_maybe_reset_handlemoose($target); |
44 | }; |
45 | # grab all *non-constant* (stash slot is not a scalarref) subs present |
46 | # in the symbol table and store their refaddrs (no need to forcibly |
47 | # inflate constant subs into real subs) - also add '' to here (this |
48 | # is used later) with a map to the coderefs in case of copying or re-use |
49 | my @not_methods = ('', map { *$_{CODE}||() } grep !ref($_), values %$stash); |
50 | @{$INFO{$target}{not_methods}={}}{@not_methods} = @not_methods; |
51 | # a role does itself |
52 | $Role::Tiny::APPLIED_TO{$target} = { $target => undef }; |
53 | |
7f9775b1 |
54 | if ($INC{'Moo/HandleMoose.pm'}) { |
55 | Moo::HandleMoose::inject_fake_metaclass_for($target); |
56 | } |
6c49212f |
57 | } |
58 | |
59 | sub _maybe_reset_handlemoose { |
60 | my ($class, $target) = @_; |
61 | if ($INC{"Moo/HandleMoose.pm"}) { |
62 | Moo::HandleMoose::maybe_reinject_fake_metaclass_for($target); |
63 | } |
d245e471 |
64 | } |
65 | |
a84066c7 |
66 | sub _inhale_if_moose { |
67 | my ($self, $role) = @_; |
68 | _load_module($role); |
c100c04c |
69 | my $meta; |
70 | if (!$INFO{$role} |
71 | and ( |
72 | $INC{"Moose.pm"} |
73 | and $meta = Class::MOP::class_of($role) |
74 | ) |
75 | or ( |
76 | $INC{"Mouse.pm"} |
77 | and $meta = Mouse::Util::find_meta($role) |
78 | ) |
79 | ) { |
80 | $INFO{$role}{methods} = { |
81 | map +($_ => $role->can($_)), |
82 | grep !$meta->get_method($_)->isa('Class::MOP::Method::Meta'), |
83 | $meta->get_method_list |
84 | }; |
85 | $Role::Tiny::APPLIED_TO{$role} = { |
86 | map +($_->name => 1), $meta->calculate_all_roles |
87 | }; |
88 | $INFO{$role}{requires} = [ $meta->get_required_method_list ]; |
89 | $INFO{$role}{attributes} = [ |
90 | map +($_ => $meta->get_attribute($_)), $meta->get_attribute_list |
91 | ]; |
92 | my $mods = $INFO{$role}{modifiers} = []; |
93 | foreach my $type (qw(before after around)) { |
94 | # Mouse pokes its own internals so we have to fall back to doing |
95 | # the same thing in the absence of the Moose API method |
96 | my $map = $meta->${\( |
97 | $meta->can("get_${type}_method_modifiers_map") |
98 | or sub { shift->{"${type}_method_modifiers"} } |
99 | )}; |
100 | foreach my $method (keys %$map) { |
101 | foreach my $mod (@{$map->{$method}}) { |
102 | push @$mods, [ $type => $method => $mod ]; |
a84066c7 |
103 | } |
104 | } |
a84066c7 |
105 | } |
c100c04c |
106 | require Class::Method::Modifiers if @$mods; |
107 | $INFO{$role}{inhaled_from_moose} = 1; |
a84066c7 |
108 | } |
109 | } |
110 | |
a41e15c3 |
111 | sub _maybe_make_accessors { |
112 | my ($self, $role, $target) = @_; |
113 | my $m; |
114 | if ($INFO{$role}{inhaled_from_moose} |
e9290d4a |
115 | or $INC{"Moo.pm"} |
116 | and $m = Moo->_accessor_maker_for($target) |
a41e15c3 |
117 | and ref($m) ne 'Method::Generate::Accessor') { |
118 | $self->_make_accessors($role, $target); |
119 | } |
120 | } |
121 | |
a84066c7 |
122 | sub _make_accessors_if_moose { |
123 | my ($self, $role, $target) = @_; |
124 | if ($INFO{$role}{inhaled_from_moose}) { |
a41e15c3 |
125 | $self->_make_accessors($role, $target); |
126 | } |
127 | } |
128 | |
129 | sub _make_accessors { |
130 | my ($self, $role, $target) = @_; |
131 | my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do { |
132 | require Method::Generate::Accessor; |
133 | Method::Generate::Accessor->new |
134 | }); |
db10ae2d |
135 | my $con_gen = $Moo::MAKERS{$target}{constructor}; |
a41e15c3 |
136 | my @attrs = @{$INFO{$role}{attributes}||[]}; |
137 | while (my ($name, $spec) = splice @attrs, 0, 2) { |
db10ae2d |
138 | # needed to ensure we got an index for an arrayref based generator |
139 | if ($con_gen) { |
140 | $spec = $con_gen->all_attribute_specs->{$name}; |
141 | } |
a41e15c3 |
142 | $acc_gen->generate_method($target, $name, $spec); |
a84066c7 |
143 | } |
144 | } |
145 | |
4a79464d |
146 | sub apply_roles_to_package { |
147 | my ($me, $to, @roles) = @_; |
148 | $me->_inhale_if_moose($_) for @roles; |
149 | $me->SUPER::apply_roles_to_package($to, @roles); |
150 | } |
151 | |
6893ea30 |
152 | sub apply_single_role_to_package { |
369a4c50 |
153 | my ($me, $to, $role) = @_; |
a84066c7 |
154 | $me->_inhale_if_moose($role); |
d245e471 |
155 | $me->_handle_constructor($to, $INFO{$role}{attributes}); |
a41e15c3 |
156 | $me->_maybe_make_accessors($role, $to); |
157 | $me->SUPER::apply_single_role_to_package($to, $role); |
d245e471 |
158 | } |
159 | |
160 | sub create_class_with_roles { |
161 | my ($me, $superclass, @roles) = @_; |
162 | |
c69190f1 |
163 | my $new_name = join( |
164 | '__WITH__', $superclass, my $compose_name = join '__AND__', @roles |
165 | ); |
166 | |
d245e471 |
167 | return $new_name if $Role::Tiny::COMPOSED{class}{$new_name}; |
168 | |
a84066c7 |
169 | $me->_inhale_if_moose($_) for @roles; |
170 | |
64284a1b |
171 | my $m; |
e9290d4a |
172 | if ($INC{"Moo.pm"} |
173 | and $m = Moo->_accessor_maker_for($superclass) |
64284a1b |
174 | and ref($m) ne 'Method::Generate::Accessor') { |
175 | # old fashioned way time. |
176 | *{_getglob("${new_name}::ISA")} = [ $superclass ]; |
177 | $me->apply_roles_to_package($new_name, @roles); |
178 | return $new_name; |
179 | } |
180 | |
faa9ce11 |
181 | require Sub::Quote; |
d245e471 |
182 | |
183 | $me->SUPER::create_class_with_roles($superclass, @roles); |
184 | |
185 | foreach my $role (@roles) { |
186 | die "${role} is not a Role::Tiny" unless my $info = $INFO{$role}; |
187 | } |
188 | |
c69190f1 |
189 | $Moo::MAKERS{$new_name} = {}; |
190 | |
d245e471 |
191 | $me->_handle_constructor( |
873df570 |
192 | $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ], $superclass |
d245e471 |
193 | ); |
194 | |
195 | return $new_name; |
196 | } |
197 | |
a84066c7 |
198 | sub _composable_package_for { |
199 | my ($self, $role) = @_; |
200 | my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role; |
201 | return $composed_name if $Role::Tiny::COMPOSED{role}{$composed_name}; |
202 | $self->_make_accessors_if_moose($role, $composed_name); |
203 | $self->SUPER::_composable_package_for($role); |
204 | } |
205 | |
dccea57d |
206 | sub _install_single_modifier { |
207 | my ($me, @args) = @_; |
208 | _install_modifier(@args); |
d245e471 |
209 | } |
210 | |
211 | sub _handle_constructor { |
c4570291 |
212 | my ($me, $to, $attr_info, $superclass) = @_; |
57d402ef |
213 | return unless $attr_info && @$attr_info; |
d245e471 |
214 | if ($INFO{$to}) { |
57d402ef |
215 | push @{$INFO{$to}{attributes}||=[]}, @$attr_info; |
d245e471 |
216 | } else { |
217 | # only fiddle with the constructor if the target is a Moo class |
218 | if ($INC{"Moo.pm"} |
c4570291 |
219 | and my $con = Moo->_constructor_maker_for($to, $superclass)) { |
a41e15c3 |
220 | # shallow copy of the specs since the constructor will assign an index |
57d402ef |
221 | $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info); |
d245e471 |
222 | } |
223 | } |
224 | } |
225 | |
226 | 1; |
bce933ec |
227 | |
0b6e5fff |
228 | =head1 NAME |
229 | |
230 | Moo::Role - Minimal Object Orientation support for Roles |
bce933ec |
231 | |
232 | =head1 SYNOPSIS |
233 | |
234 | package My::Role; |
235 | |
236 | use Moo::Role; |
237 | |
238 | sub foo { ... } |
239 | |
240 | sub bar { ... } |
241 | |
242 | has baz => ( |
243 | is => 'ro', |
244 | ); |
245 | |
246 | 1; |
247 | |
248 | else where |
249 | |
250 | package Some::Class; |
251 | |
252 | use Moo; |
253 | |
254 | # bar gets imported, but not foo |
255 | with('My::Role'); |
256 | |
257 | sub foo { ... } |
258 | |
259 | 1; |
260 | |
261 | =head1 DESCRIPTION |
262 | |
263 | C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the |
264 | documentation on how this works. The main addition here is extra bits to make |
265 | the roles more "Moosey;" which is to say, it adds L</has>. |
266 | |
267 | =head1 IMPORTED SUBROUTINES |
268 | |
269 | See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are |
270 | imported by this module. |
271 | |
272 | =head2 has |
273 | |
274 | has attr => ( |
275 | is => 'ro', |
276 | ); |
277 | |
278 | Declares an attribute for the class to be composed into. See |
279 | L<Moo/has> for all options. |
40f3e3aa |
280 | |
281 | =head1 AUTHORS |
282 | |
283 | See L<Moo> for authors. |
284 | |
285 | =head1 COPYRIGHT AND LICENSE |
286 | |
287 | See L<Moo> for the copyright and license. |
288 | |
289 | =cut |