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