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