1 package Moose::Meta::Role::Application::ToRole;
7 use Scalar::Util 'blessed';
9 use base 'Moose::Meta::Role::Application';
12 my ($self, $role1, $role2) = @_;
13 $self->SUPER::apply($role1, $role2);
14 $role2->add_role($role1);
17 sub check_role_exclusions {
18 my ($self, $role1, $role2) = @_;
19 if ( $role2->excludes_role($role1->name) ) {
21 Moose->throw_error("Conflict detected: " . $role2->name . " excludes role '" . $role1->name . "'");
23 foreach my $excluded_role_name ($role1->get_excluded_roles_list) {
24 if ( $role2->does_role($excluded_role_name) ) {
26 Moose->throw_error("The class " . $role2->name . " does the excluded role '$excluded_role_name'");
28 $role2->add_excluded_roles($excluded_role_name);
32 sub check_required_methods {
33 my ($self, $role1, $role2) = @_;
34 foreach my $required_method ($role1->get_required_method_list) {
35 my $required_method_name = $required_method->name;
37 next if $self->is_aliased_method($required_method_name);
39 $role2->add_required_methods($required_method)
40 unless $role2->find_method_by_name($required_method_name);
44 sub check_required_attributes {
48 sub 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)) {
56 my $role2_name = $role2->name;
59 Moose->throw_error( "Role '"
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'." );
67 $role2->add_attribute(
68 $role1->get_attribute($attribute_name)->clone
75 my ( $self, $role1, $role2 ) = @_;
76 foreach my $method ( $role1->_get_local_methods ) {
78 my $method_name = $method->name;
80 next if $method->isa('Class::MOP::Method::Meta');
82 unless ( $self->is_method_excluded($method_name) ) {
84 my $role2_method = $role2->get_method($method_name);
86 && $role2_method->body != $method->body ) {
88 # method conflicts between roles result in the method becoming
90 $role2->add_conflicting_method(
92 roles => [ $role1->name, $role2->name ],
103 next unless $self->is_method_aliased($method_name);
105 my $aliased_method_name = $self->get_method_aliases->{$method_name};
107 my $role2_method = $role2->get_method($aliased_method_name);
110 && $role2_method->body != $method->body ) {
114 "Cannot create a method alias if a local method of the same name exists"
119 $aliased_method_name,
120 $role1->get_method($method_name)
123 if ( !$role2->has_method($method_name) ) {
124 $role2->add_required_methods($method_name)
125 unless $self->is_method_excluded($method_name);
130 sub apply_override_method_modifiers {
131 my ($self, $role1, $role2) = @_;
132 foreach my $method_name ($role1->get_method_modifier_list('override')) {
133 # it if it has one already then ...
134 if ($role2->has_method($method_name)) {
135 # if it is being composed into another role
136 # we have a conflict here, because you cannot
137 # combine an overridden method with a locally
140 Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " .
141 "during composition (A local method of the same name as been found). This " .
145 # if we are a role, we need to make sure
146 # we dont have a conflict with the role
147 # we are composing into
148 if ($role2->has_override_method_modifier($method_name) &&
149 $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) {
152 Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " .
153 "during composition (Two 'override' methods of the same name encountered). " .
154 "This is fatal error.");
157 # if there is no conflict,
158 # just add it to the role
159 $role2->add_override_method_modifier(
161 $role1->get_override_method_modifier($method_name)
168 sub apply_method_modifiers {
169 my ($self, $modifier_type, $role1, $role2) = @_;
170 my $add = "add_${modifier_type}_method_modifier";
171 my $get = "get_${modifier_type}_method_modifiers";
172 foreach my $method_name ($role1->get_method_modifier_list($modifier_type)) {
176 ) foreach $role1->$get($method_name);
183 # ABSTRACT: Compose a role into another role
201 =item B<check_role_exclusions>
203 =item B<check_required_methods>
205 =item B<check_required_attributes>
207 =item B<apply_attributes>
209 =item B<apply_methods>
211 =item B<apply_method_modifiers>
213 =item B<apply_override_method_modifiers>
219 See L<Moose/BUGS> for details on reporting bugs.