Commit | Line | Data |
a2227e71 |
1 | package Mouse::Meta::Role; |
2 | use strict; |
3 | use warnings; |
74be9f76 |
4 | |
c6d8f5ca |
5 | use Mouse::Util qw(:meta not_supported english_list get_code_info); |
6d28c5cf |
6 | use Mouse::Meta::Module; |
f3bb863f |
7 | our @ISA = qw(Mouse::Meta::Module); |
a2227e71 |
8 | |
6cfa1e5e |
9 | sub method_metaclass(){ 'Mouse::Meta::Role::Method' } # required for get_method() |
10 | |
8e64d0fa |
11 | sub _construct_meta { |
acf0f643 |
12 | my $class = shift; |
7a50b450 |
13 | |
acf0f643 |
14 | my %args = @_; |
15 | |
3a63a2e7 |
16 | $args{methods} ||= {}; |
59089ec3 |
17 | $args{attributes} ||= {}; |
18 | $args{required_methods} ||= []; |
47f36c05 |
19 | $args{roles} ||= []; |
274b6cce |
20 | |
9009aca1 |
21 | my $self = bless \%args, ref($class) || $class; |
22 | if($class ne __PACKAGE__){ |
23 | $self->meta->_initialize_object($self, \%args); |
24 | } |
7a50b450 |
25 | |
9009aca1 |
26 | return $self; |
7a50b450 |
27 | } |
28 | |
29 | sub create_anon_role{ |
30 | my $self = shift; |
31 | return $self->create(undef, @_); |
32 | } |
33 | |
34 | sub is_anon_role{ |
35 | return exists $_[0]->{anon_serial_id}; |
acf0f643 |
36 | } |
a2227e71 |
37 | |
afc73948 |
38 | sub get_roles { $_[0]->{roles} } |
39 | |
6cfa1e5e |
40 | sub get_required_method_list{ |
41 | return @{ $_[0]->{required_methods} }; |
42 | } |
afc73948 |
43 | |
59089ec3 |
44 | sub add_required_methods { |
ea249879 |
45 | my($self, @methods) = @_; |
59089ec3 |
46 | push @{$self->{required_methods}}, @methods; |
47 | } |
48 | |
6cfa1e5e |
49 | sub requires_method { |
50 | my($self, $name) = @_; |
51 | return scalar( grep{ $_ eq $name } @{ $self->{required_methods} } ) != 0; |
52 | } |
53 | |
274b6cce |
54 | sub add_attribute { |
55 | my $self = shift; |
56 | my $name = shift; |
6cfa1e5e |
57 | |
58 | $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ }; |
da0c885d |
59 | } |
60 | |
7a50b450 |
61 | sub _canonicalize_apply_args{ |
62 | my($self, $applicant, %args) = @_; |
63 | |
64 | if($applicant->isa('Mouse::Meta::Class')){ |
65 | $args{_to} = 'class'; |
66 | } |
67 | elsif($applicant->isa('Mouse::Meta::Role')){ |
68 | $args{_to} = 'role'; |
69 | } |
70 | else{ |
71 | $args{_to} = 'instance'; |
72 | |
73 | not_supported 'Application::ToInstance'; |
74 | } |
75 | |
76 | if($args{alias} && !exists $args{-alias}){ |
77 | $args{-alias} = $args{alias}; |
78 | } |
79 | if($args{excludes} && !exists $args{-excludes}){ |
80 | $args{-excludes} = $args{excludes}; |
81 | } |
82 | |
83 | if(my $excludes = $args{-excludes}){ |
84 | $args{-excludes} = {}; # replace with a hash ref |
85 | if(ref $excludes){ |
86 | %{$args{-excludes}} = (map{ $_ => undef } @{$excludes}); |
87 | } |
88 | else{ |
89 | $args{-excludes}{$excludes} = undef; |
90 | } |
91 | } |
92 | |
93 | return \%args; |
94 | } |
95 | |
3a63a2e7 |
96 | sub _check_required_methods{ |
97 | my($role, $class, $args, @other_roles) = @_; |
98 | |
7a50b450 |
99 | if($args->{_to} eq 'class'){ |
3a63a2e7 |
100 | my $class_name = $class->name; |
7a50b450 |
101 | my $role_name = $role->name; |
102 | my @missing; |
3a63a2e7 |
103 | foreach my $method_name(@{$role->{required_methods}}){ |
7a50b450 |
104 | if(!$class_name->can($method_name)){ |
3a63a2e7 |
105 | my $has_method = 0; |
106 | |
107 | foreach my $another_role_spec(@other_roles){ |
108 | my $another_role_name = $another_role_spec->[0]; |
109 | if($role_name ne $another_role_name && $another_role_name->can($method_name)){ |
110 | $has_method = 1; |
111 | last; |
112 | } |
113 | } |
7a50b450 |
114 | |
115 | push @missing, $method_name if !$has_method; |
3a63a2e7 |
116 | } |
117 | } |
7a50b450 |
118 | if(@missing){ |
119 | $class->throw_error("'$role_name' requires the " |
120 | . (@missing == 1 ? 'method' : 'methods') |
121 | . " " |
122 | . english_list(map{ sprintf q{'%s'}, $_ } @missing) |
123 | . " to be implemented by '$class_name'"); |
124 | } |
125 | } |
126 | elsif($args->{_to} eq 'role'){ |
127 | # apply role($role) to role($class) |
128 | foreach my $method_name($role->get_required_method_list){ |
129 | next if $class->has_method($method_name); # already has it |
130 | $class->add_required_methods($method_name); |
131 | } |
3a63a2e7 |
132 | } |
2e92bb89 |
133 | |
3a63a2e7 |
134 | return; |
2e92bb89 |
135 | } |
136 | |
3a63a2e7 |
137 | sub _apply_methods{ |
138 | my($role, $class, $args) = @_; |
da0c885d |
139 | |
3a63a2e7 |
140 | my $role_name = $role->name; |
141 | my $class_name = $class->name; |
6cfa1e5e |
142 | |
7a50b450 |
143 | my $alias = $args->{-alias}; |
144 | my $excludes = $args->{-excludes}; |
e0b163e1 |
145 | |
3a63a2e7 |
146 | foreach my $method_name($role->get_method_list){ |
147 | next if $method_name eq 'meta'; |
148 | |
149 | my $code = $role_name->can($method_name); |
6cfa1e5e |
150 | |
7a50b450 |
151 | if(!exists $excludes->{$method_name}){ |
6cfa1e5e |
152 | if(!$class->has_method($method_name)){ |
153 | $class->add_method($method_name => $code); |
154 | } |
2e92bb89 |
155 | } |
2e92bb89 |
156 | |
3a63a2e7 |
157 | if($alias && $alias->{$method_name}){ |
158 | my $dstname = $alias->{$method_name}; |
6cfa1e5e |
159 | |
7a50b450 |
160 | my $dstcode = do{ no strict 'refs'; *{$class_name . '::' . $dstname}{CODE} }; |
161 | |
162 | if(defined($dstcode) && $dstcode != $code){ |
6cfa1e5e |
163 | $class->throw_error("Cannot create a method alias if a local method of the same name exists"); |
21498b08 |
164 | } |
3a63a2e7 |
165 | else{ |
166 | $class->add_method($dstname => $code); |
2e92bb89 |
167 | } |
59089ec3 |
168 | } |
169 | } |
170 | |
3a63a2e7 |
171 | return; |
172 | } |
173 | |
174 | sub _apply_attributes{ |
175 | my($role, $class, $args) = @_; |
176 | |
7a50b450 |
177 | if ($args->{_to} eq 'class') { |
b1b81553 |
178 | # apply role to class |
3a63a2e7 |
179 | for my $attr_name ($role->get_attribute_list) { |
180 | next if $class->has_attribute($attr_name); |
181 | |
182 | my $spec = $role->get_attribute($attr_name); |
05b9dc92 |
183 | |
1b9e472d |
184 | $class->add_attribute($attr_name => %{$spec}); |
b1b81553 |
185 | } |
7a50b450 |
186 | } |
187 | elsif($args->{_to} eq 'role'){ |
b1b81553 |
188 | # apply role to role |
3a63a2e7 |
189 | for my $attr_name ($role->get_attribute_list) { |
190 | next if $class->has_attribute($attr_name); |
191 | |
192 | my $spec = $role->get_attribute($attr_name); |
193 | $class->add_attribute($attr_name => $spec); |
b1b81553 |
194 | } |
da0c885d |
195 | } |
d99db7b6 |
196 | |
3a63a2e7 |
197 | return; |
198 | } |
199 | |
200 | sub _apply_modifiers{ |
201 | my($role, $class, $args) = @_; |
202 | |
7a50b450 |
203 | for my $modifier_type (qw/override before around after/) { |
3a63a2e7 |
204 | my $add_modifier = "add_${modifier_type}_method_modifier"; |
205 | my $modifiers = $role->{"${modifier_type}_method_modifiers"}; |
d99db7b6 |
206 | |
3a63a2e7 |
207 | while(my($method_name, $modifier_codes) = each %{$modifiers}){ |
6cfa1e5e |
208 | foreach my $code(ref($modifier_codes) eq 'ARRAY' ? @{$modifier_codes} : $modifier_codes){ |
3a63a2e7 |
209 | $class->$add_modifier($method_name => $code); |
d99db7b6 |
210 | } |
211 | } |
212 | } |
3a63a2e7 |
213 | return; |
da0c885d |
214 | } |
0fc8adbc |
215 | |
3a63a2e7 |
216 | sub _append_roles{ |
217 | my($role, $class, $args) = @_; |
21498b08 |
218 | |
7a50b450 |
219 | my $roles = ($args->{_to} eq 'class') ? $class->roles : $class->get_roles; |
3a63a2e7 |
220 | |
221 | foreach my $r($role, @{$role->get_roles}){ |
222 | if(!$class->does_role($r->name)){ |
223 | push @{$roles}, $r; |
21498b08 |
224 | } |
225 | } |
3a63a2e7 |
226 | return; |
227 | } |
21498b08 |
228 | |
3a63a2e7 |
229 | # Moose uses Application::ToInstance, Application::ToClass, Application::ToRole |
230 | sub apply { |
7a50b450 |
231 | my $self = shift; |
232 | my $applicant = shift; |
3a63a2e7 |
233 | |
7a50b450 |
234 | my $args = $self->_canonicalize_apply_args($applicant, @_); |
21498b08 |
235 | |
7a50b450 |
236 | $self->_check_required_methods($applicant, $args); |
237 | $self->_apply_methods($applicant, $args); |
238 | $self->_apply_attributes($applicant, $args); |
239 | $self->_apply_modifiers($applicant, $args); |
240 | $self->_append_roles($applicant, $args); |
3a63a2e7 |
241 | return; |
242 | } |
21498b08 |
243 | |
3a63a2e7 |
244 | sub combine_apply { |
245 | my(undef, $class, @roles) = @_; |
05b9dc92 |
246 | |
7a50b450 |
247 | if($class->isa('Mouse::Object')){ |
248 | not_supported 'Application::ToInstance'; |
249 | } |
250 | |
60b5c3be |
251 | # check conflicting |
252 | my %method_provided; |
253 | my @method_conflicts; |
254 | my %attr_provided; |
255 | my %override_provided; |
256 | |
257 | foreach my $role_spec (@roles) { |
258 | my $role = $role_spec->[0]->meta; |
259 | my $role_name = $role->name; |
260 | |
261 | # methods |
262 | foreach my $method_name($role->get_method_list){ |
263 | next if $class->has_method($method_name); # manually resolved |
264 | |
265 | my $code = do{ no strict 'refs'; \&{ $role_name . '::' . $method_name } }; |
266 | |
267 | my $c = $method_provided{$method_name}; |
268 | |
269 | if($c && $c->[0] != $code){ |
270 | push @{$c}, $role; |
271 | push @method_conflicts, $c; |
272 | } |
273 | else{ |
274 | $method_provided{$method_name} = [$code, $method_name, $role]; |
275 | } |
276 | } |
277 | |
278 | # attributes |
279 | foreach my $attr_name($role->get_attribute_list){ |
280 | my $attr = $role->get_attribute($attr_name); |
281 | my $c = $attr_provided{$attr_name}; |
282 | if($c && $c != $attr){ |
8e64d0fa |
283 | $class->throw_error("We have encountered an attribute conflict with '$attr_name' " |
60b5c3be |
284 | . "during composition. This is fatal error and cannot be disambiguated.") |
285 | } |
286 | else{ |
287 | $attr_provided{$attr_name} = $attr; |
288 | } |
289 | } |
290 | |
291 | # override modifiers |
292 | foreach my $method_name($role->get_method_modifier_list('override')){ |
293 | my $override = $role->get_override_method_modifier($method_name); |
294 | my $c = $override_provided{$method_name}; |
295 | if($c && $c != $override){ |
8e64d0fa |
296 | $class->throw_error( "We have encountered an 'override' method conflict with '$method_name' during " |
297 | . "composition (Two 'override' methods of the same name encountered). " |
60b5c3be |
298 | . "This is fatal error.") |
299 | } |
300 | else{ |
301 | $override_provided{$method_name} = $override; |
302 | } |
303 | } |
304 | } |
305 | if(@method_conflicts){ |
306 | my $error; |
307 | |
308 | if(@method_conflicts == 1){ |
309 | my($code, $method_name, @roles) = @{$method_conflicts[0]}; |
310 | $class->throw_error( |
311 | sprintf q{Due to a method name conflict in roles %s, the method '%s' must be implemented or excluded by '%s'}, |
312 | english_list(map{ sprintf q{'%s'}, $_->name } @roles), $method_name, $class->name |
313 | ); |
314 | } |
315 | else{ |
316 | @method_conflicts = sort { $a->[0] cmp $b->[0] } @method_conflicts; # to avoid hash-ordering bugs |
317 | my $methods = english_list(map{ sprintf q{'%s'}, $_->[1] } @method_conflicts); |
318 | my $roles = english_list( |
319 | map{ sprintf q{'%s'}, $_->name } |
320 | map{ my($code, $method_name, @roles) = @{$_}; @roles } @method_conflicts |
321 | ); |
322 | |
323 | $class->throw_error( |
324 | sprintf q{Due to method name conflicts in roles %s, the methods %s must be implemented or excluded by '%s'}, |
325 | $roles, $methods, $class->name |
326 | ); |
327 | } |
328 | } |
329 | |
3a63a2e7 |
330 | foreach my $role_spec (@roles) { |
331 | my($role_name, $args) = @{$role_spec}; |
21498b08 |
332 | |
3a63a2e7 |
333 | my $role = $role_name->meta; |
21498b08 |
334 | |
7a50b450 |
335 | $args = $role->_canonicalize_apply_args($class, %{$args}); |
336 | |
3a63a2e7 |
337 | $role->_check_required_methods($class, $args, @roles); |
338 | $role->_apply_methods($class, $args); |
339 | $role->_apply_attributes($class, $args); |
340 | $role->_apply_modifiers($class, $args); |
341 | $role->_append_roles($class, $args); |
21498b08 |
342 | } |
3a63a2e7 |
343 | return; |
21498b08 |
344 | } |
345 | |
6cfa1e5e |
346 | for my $modifier_type (qw/before after around/) { |
3a63a2e7 |
347 | |
348 | my $modifier = "${modifier_type}_method_modifiers"; |
349 | my $add_method_modifier = sub { |
fc0e0bbd |
350 | my ($self, $method_name, $method) = @_; |
351 | |
3a63a2e7 |
352 | push @{ $self->{$modifier}->{$method_name} ||= [] }, $method; |
353 | return; |
fc0e0bbd |
354 | }; |
3370794f |
355 | my $has_method_modifiers = sub{ |
356 | my($self, $method_name) = @_; |
357 | my $m = $self->{$modifier}->{$method_name}; |
358 | return $m && @{$m} != 0; |
359 | }; |
3a63a2e7 |
360 | my $get_method_modifiers = sub { |
361 | my ($self, $method_name) = @_; |
362 | return @{ $self->{$modifier}->{$method_name} ||= [] } |
c2f128e7 |
363 | }; |
c2f128e7 |
364 | |
3a63a2e7 |
365 | no strict 'refs'; |
366 | *{ 'add_' . $modifier_type . '_method_modifier' } = $add_method_modifier; |
3370794f |
367 | *{ 'has_' . $modifier_type . '_method_modifiers' } = $has_method_modifiers; |
3a63a2e7 |
368 | *{ 'get_' . $modifier_type . '_method_modifiers' } = $get_method_modifiers; |
369 | } |
47f36c05 |
370 | |
6cfa1e5e |
371 | sub add_override_method_modifier{ |
372 | my($self, $method_name, $method) = @_; |
373 | |
60b5c3be |
374 | if($self->has_method($method_name)){ |
375 | # This error happens in the override keyword or during role composition, |
376 | # so I added a message, "A local method of ...", only for compatibility (gfx) |
8e64d0fa |
377 | $self->throw_error("Cannot add an override of method '$method_name' " |
60b5c3be |
378 | . "because there is a local version of '$method_name'" |
379 | . "(A local method of the same name as been found)"); |
380 | } |
6cfa1e5e |
381 | |
382 | $self->{override_method_modifiers}->{$method_name} = $method; |
383 | } |
384 | |
8e64d0fa |
385 | sub has_override_method_modifier { |
386 | my ($self, $method_name) = @_; |
387 | return exists $self->{override_method_modifiers}->{$method_name}; |
388 | } |
389 | |
390 | sub get_override_method_modifier { |
391 | my ($self, $method_name) = @_; |
392 | return $self->{override_method_modifiers}->{$method_name}; |
6cfa1e5e |
393 | } |
394 | |
395 | sub get_method_modifier_list { |
396 | my($self, $modifier_type) = @_; |
397 | |
398 | return keys %{ $self->{$modifier_type . '_method_modifiers'} }; |
399 | } |
400 | |
67199842 |
401 | # This is currently not passing all the Moose tests. |
402 | sub does_role { |
403 | my ($self, $role_name) = @_; |
404 | |
405 | (defined $role_name) |
fce211ae |
406 | || $self->throw_error("You must supply a role name to look for"); |
67199842 |
407 | |
408 | # if we are it,.. then return true |
409 | return 1 if $role_name eq $self->name; |
3a63a2e7 |
410 | # otherwise.. check our children |
411 | for my $role (@{ $self->get_roles }) { |
67199842 |
412 | return 1 if $role->does_role($role_name); |
413 | } |
414 | return 0; |
415 | } |
416 | |
417 | |
a2227e71 |
418 | 1; |
419 | |
1820fffe |
420 | __END__ |
421 | |
422 | =head1 NAME |
423 | |
424 | Mouse::Meta::Role - The Mouse Role metaclass |
425 | |
426 | =head1 SEE ALSO |
427 | |
428 | L<Moose::Meta::Role> |
429 | |
430 | =cut |