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