add an option to explicitly prohibit method shadowing
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / ToRole.pm
CommitLineData
fb1e11d5 1package Moose::Meta::Role::Application::ToRole;
2
3use strict;
4use warnings;
5use metaclass;
6
fb1e11d5 7use Scalar::Util 'blessed';
8
fb1e11d5 9use base 'Moose::Meta::Role::Application';
10
1c9db35c 11sub apply {
d03bd989 12 my ($self, $role1, $role2) = @_;
13 $self->SUPER::apply($role1, $role2);
14 $role2->add_role($role1);
1c9db35c 15}
16
17sub check_role_exclusions {
18 my ($self, $role1, $role2) = @_;
70ea9161 19 if ( $role2->excludes_role($role1->name) ) {
20 require Moose;
21 Moose->throw_error("Conflict detected: " . $role2->name . " excludes role '" . $role1->name . "'");
22 }
1c9db35c 23 foreach my $excluded_role_name ($role1->get_excluded_roles_list) {
70ea9161 24 if ( $role2->does_role($excluded_role_name) ) {
25 require Moose;
0a58c428 26 Moose->throw_error("The role " . $role2->name . " does the excluded role '$excluded_role_name'");
70ea9161 27 }
1c9db35c 28 $role2->add_excluded_roles($excluded_role_name);
29 }
30}
31
32sub check_required_methods {
33 my ($self, $role1, $role2) = @_;
4c93bc92 34 foreach my $required_method ($role1->get_required_method_list) {
35 my $required_method_name = $required_method->name;
d03bd989 36
3e19778d 37 next if $self->is_aliased_method($required_method_name);
d03bd989 38
4c93bc92 39 $role2->add_required_methods($required_method)
1c9db35c 40 unless $role2->find_method_by_name($required_method_name);
41 }
42}
43
709c321c 44sub check_required_attributes {
d03bd989 45
709c321c 46}
47
1c9db35c 48sub apply_attributes {
49 my ($self, $role1, $role2) = @_;
50 foreach my $attribute_name ($role1->get_attribute_list) {
51 # it if it has one already
52 if ($role2->has_attribute($attribute_name) &&
53 # make sure we haven't seen this one already too
54 $role2->get_attribute($attribute_name) != $role1->get_attribute($attribute_name)) {
70ea9161 55
51d4595e 56 my $role2_name = $role2->name;
57
70ea9161 58 require Moose;
51d4595e 59 Moose->throw_error( "Role '"
60 . $role1->name
61 . "' has encountered an attribute conflict"
62 . " while being composed into '$role2_name'."
63 . " This is a fatal error and cannot be disambiguated."
64 . " The conflicting attribute is named '$attribute_name'." );
1c9db35c 65 }
66 else {
67 $role2->add_attribute(
f785aad8 68 $role1->get_attribute($attribute_name)->clone
1c9db35c 69 );
70 }
71 }
72}
73
74sub apply_methods {
4a9e8ff6 75 my ( $self, $role1, $role2 ) = @_;
76 foreach my $method ( $role1->_get_local_methods ) {
77
78 my $method_name = $method->name;
79
ba7d613d 80 next if $method->isa('Class::MOP::Method::Meta');
db9476b1 81
bd38046e 82 unless ( $self->is_method_excluded($method_name) ) {
4a9e8ff6 83
84 my $role2_method = $role2->get_method($method_name);
85 if ( $role2_method
86 && $role2_method->body != $method->body ) {
bd38046e 87
2afe5289 88 # method conflicts between roles used to result in the method
89 # becoming a requirement but now are permitted just like
6470237f 90 # for classes, unless shadowing is explicitly prohibited
91
92 if ( $self->is_shadowing_prohibited ) {
93
94 $role2->add_conflicting_method(
95 name => $method_name,
96 roles => [ $role1->name, $role2->name ],
97 );
98 }
bd38046e 99 }
100 else {
101 $role2->add_method(
102 $method_name,
4a9e8ff6 103 $method,
bd38046e 104 );
105 }
106 }
107
4a9e8ff6 108 next unless $self->is_method_aliased($method_name);
094b50c0 109
4a9e8ff6 110 my $aliased_method_name = $self->get_method_aliases->{$method_name};
70ea9161 111
4a9e8ff6 112 my $role2_method = $role2->get_method($aliased_method_name);
db9476b1 113
4a9e8ff6 114 if ( $role2_method
115 && $role2_method->body != $method->body ) {
116
117 require Moose;
118 Moose->throw_error(
119 "Cannot create a method alias if a local method of the same name exists"
db9476b1 120 );
4a9e8ff6 121 }
db9476b1 122
4a9e8ff6 123 $role2->add_method(
124 $aliased_method_name,
125 $role1->get_method($method_name)
126 );
127
128 if ( !$role2->has_method($method_name) ) {
129 $role2->add_required_methods($method_name)
130 unless $self->is_method_excluded($method_name);
1c9db35c 131 }
132 }
133}
134
135sub apply_override_method_modifiers {
136 my ($self, $role1, $role2) = @_;
137 foreach my $method_name ($role1->get_method_modifier_list('override')) {
138 # it if it has one already then ...
139 if ($role2->has_method($method_name)) {
140 # if it is being composed into another role
141 # we have a conflict here, because you cannot
6549b0d1 142 # combine an overridden method with a locally
1c9db35c 143 # defined one
70ea9161 144 require Moose;
c245d69b 145 Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " .
1c9db35c 146 "during composition (A local method of the same name as been found). This " .
4c0b3599 147 "is fatal error.");
1c9db35c 148 }
149 else {
150 # if we are a role, we need to make sure
151 # we dont have a conflict with the role
152 # we are composing into
153 if ($role2->has_override_method_modifier($method_name) &&
154 $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) {
70ea9161 155
156 require Moose;
c245d69b 157 Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " .
1c9db35c 158 "during composition (Two 'override' methods of the same name encountered). " .
4c0b3599 159 "This is fatal error.");
1c9db35c 160 }
161 else {
162 # if there is no conflict,
163 # just add it to the role
164 $role2->add_override_method_modifier(
165 $method_name,
166 $role1->get_override_method_modifier($method_name)
167 );
168 }
169 }
170 }
171}
172
173sub apply_method_modifiers {
174 my ($self, $modifier_type, $role1, $role2) = @_;
175 my $add = "add_${modifier_type}_method_modifier";
176 my $get = "get_${modifier_type}_method_modifiers";
177 foreach my $method_name ($role1->get_method_modifier_list($modifier_type)) {
178 $role2->$add(
179 $method_name,
180 $_
181 ) foreach $role1->$get($method_name);
182 }
183}
184
1c9db35c 185
fb1e11d5 1861;
187
ad46f524 188# ABSTRACT: Compose a role into another role
189
fb1e11d5 190__END__
191
192=pod
193
fb1e11d5 194=head1 DESCRIPTION
195
196=head2 METHODS
197
198=over 4
199
200=item B<new>
201
202=item B<meta>
203
1c9db35c 204=item B<apply>
205
709c321c 206=item B<check_role_exclusions>
207
1c9db35c 208=item B<check_required_methods>
209
709c321c 210=item B<check_required_attributes>
1c9db35c 211
212=item B<apply_attributes>
213
214=item B<apply_methods>
215
216=item B<apply_method_modifiers>
217
1c9db35c 218=item B<apply_override_method_modifiers>
219
fb1e11d5 220=back
221
222=head1 BUGS
223
d4048ef3 224See L<Moose/BUGS> for details on reporting bugs.
fb1e11d5 225
fb1e11d5 226=cut
227