Convert Moose->throw_error to Moose::Util::throw
[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             Moose::Util::throw(sprintf "Conflict detected: Role%s %s exclude%s role '%s'", (@excluding == 1 ? '' : 's'), join(', ', @excluding), (@excluding == 1 ? 's' : ''), $excluded);
82         }
83     }
84
85     $c->add_excluded_roles(keys %excluded_roles);
86 }
87
88 sub check_required_methods {
89     my ($self, $c) = @_;
90
91     my %all_required_methods =
92         map { $_->name => $_ }
93         map { $_->get_required_method_list }
94         @{$c->get_roles};
95
96     foreach my $role (@{$c->get_roles}) {
97         foreach my $required (keys %all_required_methods) {
98
99             delete $all_required_methods{$required}
100                 if $role->has_method($required)
101                 || $self->is_aliased_method($role, $required);
102         }
103     }
104
105     $c->add_required_methods(values %all_required_methods);
106 }
107
108 sub check_required_attributes {
109
110 }
111
112 sub apply_attributes {
113     my ($self, $c) = @_;
114
115     my @all_attributes;
116
117     for my $role ( @{ $c->get_roles } ) {
118         push @all_attributes,
119             map { $role->get_attribute($_) } $role->get_attribute_list;
120     }
121
122     my %seen;
123     foreach my $attr (@all_attributes) {
124         my $name = $attr->name;
125
126         if ( exists $seen{$name} ) {
127             next if $seen{$name}->is_same_as($attr);
128
129             my $role1 = $seen{$name}->associated_role->name;
130             my $role2 = $attr->associated_role->name;
131
132             Moose::Util::throw(
133                 "We have encountered an attribute conflict with '$name' "
134                     . "during role composition. "
135                     . " This attribute is defined in both $role1 and $role2."
136                     . " This is a fatal error and cannot be disambiguated." );
137         }
138
139         $seen{$name} = $attr;
140     }
141
142     foreach my $attr (@all_attributes) {
143         $c->add_attribute( $attr->clone );
144     }
145 }
146
147 sub apply_methods {
148     my ($self, $c) = @_;
149
150     my @all_methods = map {
151         my $role     = $_;
152         my $aliases  = $self->get_method_aliases_for_role($role);
153         my %excludes = map { $_ => undef } @{ $self->get_exclusions_for_role($role) };
154         (
155             (map {
156                 exists $excludes{$_} ? () :
157                 +{
158                     role   => $role,
159                     name   => $_,
160                     method => $role->get_method($_),
161                 }
162             } map { $_->name }
163               grep { !$_->isa('Class::MOP::Method::Meta') }
164                    $role->_get_local_methods),
165             (map {
166                 +{
167                     role   => $role,
168                     name   => $aliases->{$_},
169                     method => $role->get_method($_),
170                 }
171             } keys %$aliases)
172         );
173     } @{$c->get_roles};
174
175     my (%seen, %conflicts, %method_map);
176     foreach my $method (@all_methods) {
177         next if $conflicts{$method->{name}};
178         my $seen = $seen{$method->{name}};
179
180         if ($seen) {
181             if ($seen->{method}->body != $method->{method}->body) {
182                 $c->add_conflicting_method(
183                     name  => $method->{name},
184                     roles => [$method->{role}->name, $seen->{role}->name],
185                 );
186
187                 delete $method_map{$method->{name}};
188                 $conflicts{$method->{name}} = 1;
189                 next;
190             }
191         }
192
193         $seen{$method->{name}}       = $method;
194         $method_map{$method->{name}} = $method->{method};
195     }
196
197     $c->add_method($_ => $method_map{$_}) for keys %method_map;
198 }
199
200 sub apply_override_method_modifiers {
201     my ($self, $c) = @_;
202
203     my @all_overrides = map {
204         my $role = $_;
205         map {
206             +{
207                 name   => $_,
208                 method => $role->get_override_method_modifier($_),
209             }
210         } $role->get_method_modifier_list('override');
211     } @{$c->get_roles};
212
213     my %seen;
214     foreach my $override (@all_overrides) {
215         if ( $c->has_method($override->{name}) ){
216             Moose::Util::throw( "Role '" . $c->name . "' has encountered an 'override' method conflict " .
217                                 "during composition (A local method of the same name as been found). This " .
218                                 "is fatal error." )
219         }
220         if (exists $seen{$override->{name}}) {
221             if ( $seen{$override->{name}} != $override->{method} ) {
222                 Moose::Util::throw( "We have encountered an 'override' method conflict during " .
223                                     "composition (Two 'override' methods of the same name encountered). " .
224                                     "This is fatal error.")
225             }
226         }
227         $seen{$override->{name}} = $override->{method};
228     }
229
230     $c->add_override_method_modifier(
231         $_->{name}, $_->{method}
232     ) for @all_overrides;
233
234 }
235
236 sub apply_method_modifiers {
237     my ($self, $modifier_type, $c) = @_;
238     my $add = "add_${modifier_type}_method_modifier";
239     my $get = "get_${modifier_type}_method_modifiers";
240     foreach my $role (@{$c->get_roles}) {
241         foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
242             $c->$add(
243                 $method_name,
244                 $_
245             ) foreach $role->$get($method_name);
246         }
247     }
248 }
249
250 1;
251
252 # ABSTRACT: Combine two or more roles
253
254 __END__
255
256 =pod
257
258 =head1 DESCRIPTION
259
260 Summation composes two traits, forming the union of non-conflicting
261 bindings and 'disabling' the conflicting bindings
262
263 =head2 METHODS
264
265 =over 4
266
267 =item B<new>
268
269 =item B<meta>
270
271 =item B<role_params>
272
273 =item B<get_exclusions_for_role>
274
275 =item B<get_method_aliases_for_role>
276
277 =item B<is_aliased_method>
278
279 =item B<is_method_aliased>
280
281 =item B<is_method_excluded>
282
283 =item B<apply>
284
285 =item B<check_role_exclusions>
286
287 =item B<check_required_methods>
288
289 =item B<check_required_attributes>
290
291 =item B<apply_attributes>
292
293 =item B<apply_methods>
294
295 =item B<apply_method_modifiers>
296
297 =item B<apply_override_method_modifiers>
298
299 =back
300
301 =head1 BUGS
302
303 See L<Moose/BUGS> for details on reporting bugs.
304
305 =cut
306