During role summation, make sure that any method name that conflicts does not get...
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / RoleSummation.pm
1 package Moose::Meta::Role::Application::RoleSummation;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use Scalar::Util 'blessed';
8
9 use Moose::Meta::Role::Composite;
10
11 use base 'Moose::Meta::Role::Application';
12
13 __PACKAGE__->meta->add_attribute('role_params' => (
14     reader  => 'role_params',
15     default => sub { {} },
16     Class::MOP::_definition_context(),
17 ));
18
19 sub get_exclusions_for_role {
20     my ($self, $role) = @_;
21     $role = $role->name if blessed $role;
22     my $excludes_key = exists $self->role_params->{$role}->{'-excludes'} ?
23                            '-excludes' : 'excludes';
24     if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$excludes_key}) {
25         if (ref $self->role_params->{$role}->{$excludes_key} eq 'ARRAY') {
26             return $self->role_params->{$role}->{$excludes_key};
27         }
28         return [ $self->role_params->{$role}->{$excludes_key} ];
29     }
30     return [];
31 }
32
33 sub get_method_aliases_for_role {
34     my ($self, $role) = @_;
35     $role = $role->name if blessed $role;
36     my $alias_key = exists $self->role_params->{$role}->{'-alias'} ?
37                         '-alias' : 'alias';
38     if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$alias_key}) {
39         return $self->role_params->{$role}->{$alias_key};
40     }
41     return {};
42 }
43
44 sub is_method_excluded {
45     my ($self, $role, $method_name) = @_;
46     foreach ($self->get_exclusions_for_role($role->name)) {
47         return 1 if $_ eq $method_name;
48     }
49     return 0;
50 }
51
52 sub is_method_aliased {
53     my ($self, $role, $method_name) = @_;
54     exists $self->get_method_aliases_for_role($role->name)->{$method_name} ? 1 : 0
55 }
56
57 sub is_aliased_method {
58     my ($self, $role, $method_name) = @_;
59     my %aliased_names = reverse %{$self->get_method_aliases_for_role($role->name)};
60     exists $aliased_names{$method_name} ? 1 : 0;
61 }
62
63 sub check_role_exclusions {
64     my ($self, $c) = @_;
65
66     my %excluded_roles;
67     for my $role (@{ $c->get_roles }) {
68         my $name = $role->name;
69
70         for my $excluded ($role->get_excluded_roles_list) {
71             push @{ $excluded_roles{$excluded} }, $name;
72         }
73     }
74
75     foreach my $role (@{$c->get_roles}) {
76         foreach my $excluded (keys %excluded_roles) {
77             next unless $role->does_role($excluded);
78
79             my @excluding = @{ $excluded_roles{$excluded} };
80
81             require Moose;
82             Moose->throw_error(sprintf "Conflict detected: Role%s %s exclude%s role '%s'", (@excluding == 1 ? '' : 's'), join(', ', @excluding), (@excluding == 1 ? 's' : ''), $excluded);
83         }
84     }
85
86     $c->add_excluded_roles(keys %excluded_roles);
87 }
88
89 sub check_required_methods {
90     my ($self, $c) = @_;
91
92     my %all_required_methods =
93         map { $_->name => $_ }
94         map { $_->get_required_method_list }
95         @{$c->get_roles};
96
97     foreach my $role (@{$c->get_roles}) {
98         foreach my $required (keys %all_required_methods) {
99
100             delete $all_required_methods{$required}
101                 if $role->has_method($required)
102                 || $self->is_aliased_method($role, $required);
103         }
104     }
105
106     $c->add_required_methods(values %all_required_methods);
107 }
108
109 sub check_required_attributes {
110
111 }
112
113 sub apply_attributes {
114     my ($self, $c) = @_;
115
116     my @all_attributes;
117
118     for my $role ( @{ $c->get_roles } ) {
119         push @all_attributes,
120             map { $role->get_attribute($_) } $role->get_attribute_list;
121     }
122
123     my %seen;
124     foreach my $attr (@all_attributes) {
125         my $name = $attr->name;
126
127         if ( exists $seen{$name} ) {
128             next if $seen{$name}->is_same_as($attr);
129
130             my $role1 = $seen{$name}->associated_role->name;
131             my $role2 = $attr->associated_role->name;
132
133             require Moose;
134             Moose->throw_error(
135                 "We have encountered an attribute conflict with '$name' "
136                     . "during role composition. "
137                     . " This attribute is defined in both $role1 and $role2."
138                     . " This is a fatal error and cannot be disambiguated." );
139         }
140
141         $seen{$name} = $attr;
142     }
143
144     foreach my $attr (@all_attributes) {
145         $c->add_attribute( $attr->clone );
146     }
147 }
148
149 sub apply_methods {
150     my ($self, $c) = @_;
151
152     my @all_methods = map {
153         my $role     = $_;
154         my $aliases  = $self->get_method_aliases_for_role($role);
155         my %excludes = map { $_ => undef } @{ $self->get_exclusions_for_role($role) };
156         (
157             (map {
158                 exists $excludes{$_} ? () :
159                 +{
160                     role   => $role,
161                     name   => $_,
162                     method => $role->get_method($_),
163                 }
164             } map { $_->name }
165               grep { !$_->isa('Class::MOP::Method::Meta') }
166                    $role->_get_local_methods),
167             (map {
168                 +{
169                     role   => $role,
170                     name   => $aliases->{$_},
171                     method => $role->get_method($_),
172                 }
173             } keys %$aliases)
174         );
175     } @{$c->get_roles};
176
177     my (%seen, %conflicts, %method_map);
178     foreach my $method (@all_methods) {
179         next if $conflicts{$method->{name}};
180         my $seen = $seen{$method->{name}};
181
182         if ($seen) {
183             if ($seen->{method}->body != $method->{method}->body) {
184                 $c->add_conflicting_method(
185                     name  => $method->{name},
186                     roles => [$method->{role}->name, $seen->{role}->name],
187                 );
188
189                 delete $method_map{$method->{name}};
190                 $conflicts{$method->{name}} = 1;
191                 next;
192             }
193         }
194
195         $seen{$method->{name}}       = $method;
196         $method_map{$method->{name}} = $method->{method};
197     }
198
199     $c->add_method($_ => $method_map{$_}) for keys %method_map;
200 }
201
202 sub apply_override_method_modifiers {
203     my ($self, $c) = @_;
204
205     my @all_overrides = map {
206         my $role = $_;
207         map {
208             +{
209                 name   => $_,
210                 method => $role->get_override_method_modifier($_),
211             }
212         } $role->get_method_modifier_list('override');
213     } @{$c->get_roles};
214
215     my %seen;
216     foreach my $override (@all_overrides) {
217         if ( $c->has_method($override->{name}) ){
218             require Moose;
219             Moose->throw_error( "Role '" . $c->name . "' has encountered an 'override' method conflict " .
220                                 "during composition (A local method of the same name as been found). This " .
221                                 "is fatal error." )
222         }
223         if (exists $seen{$override->{name}}) {
224             if ( $seen{$override->{name}} != $override->{method} ) {
225                 require Moose;
226                 Moose->throw_error( "We have encountered an 'override' method conflict during " .
227                                     "composition (Two 'override' methods of the same name encountered). " .
228                                     "This is fatal error.")
229             }
230         }
231         $seen{$override->{name}} = $override->{method};
232     }
233
234     $c->add_override_method_modifier(
235         $_->{name}, $_->{method}
236     ) for @all_overrides;
237
238 }
239
240 sub apply_method_modifiers {
241     my ($self, $modifier_type, $c) = @_;
242     my $add = "add_${modifier_type}_method_modifier";
243     my $get = "get_${modifier_type}_method_modifiers";
244     foreach my $role (@{$c->get_roles}) {
245         foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
246             $c->$add(
247                 $method_name,
248                 $_
249             ) foreach $role->$get($method_name);
250         }
251     }
252 }
253
254 1;
255
256 # ABSTRACT: Combine two or more roles
257
258 __END__
259
260 =pod
261
262 =head1 DESCRIPTION
263
264 Summation composes two traits, forming the union of non-conflicting
265 bindings and 'disabling' the conflicting bindings
266
267 =head2 METHODS
268
269 =over 4
270
271 =item B<new>
272
273 =item B<meta>
274
275 =item B<role_params>
276
277 =item B<get_exclusions_for_role>
278
279 =item B<get_method_aliases_for_role>
280
281 =item B<is_aliased_method>
282
283 =item B<is_method_aliased>
284
285 =item B<is_method_excluded>
286
287 =item B<apply>
288
289 =item B<check_role_exclusions>
290
291 =item B<check_required_methods>
292
293 =item B<check_required_attributes>
294
295 =item B<apply_attributes>
296
297 =item B<apply_methods>
298
299 =item B<apply_method_modifiers>
300
301 =item B<apply_override_method_modifiers>
302
303 =back
304
305 =head1 BUGS
306
307 See L<Moose/BUGS> for details on reporting bugs.
308
309 =cut
310