| 1 | package UR::Role::Prototype; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use UR; | ||
| 7 | use UR::Object::Type::InternalAPI; | ||
| 8 | use UR::Util; | ||
| 9 | use UR::AttributeHandlers; | ||
| 10 | |||
| 11 | use Sub::Name qw(); | ||
| 12 | use Sub::Install qw(); | ||
| 13 | use List::MoreUtils qw(any); | ||
| 14 | use Carp; | ||
| 15 | our @CARP_NOT = qw(UR::Object::Type); | ||
| 16 | |||
| 17 | our $VERSION = "0.46"; # UR $VERSION;; | ||
| 18 | |||
| 19 | UR::Object::Type->define( | ||
| 20 | class_name => 'UR::Role::Prototype', | ||
| 21 | doc => 'Object representing a role', | ||
| 22 | id_by => 'role_name', | ||
| 23 | has => [ | ||
| 24 | id_by => { is => 'ARRAY', doc => 'List of ID properties and their definitions' }, | ||
| 25 | role_name => { is => 'Text', doc => 'Package name identifying the role' }, | ||
| 26 | class_names => { is => 'Text', is_many => 1, doc => 'Class names composing this role' }, | ||
| 27 | methods => { is => 'HASH', doc => 'Map of method names and coderefs', default => {} }, | ||
| 28 | overloads => { is => 'HASH', doc => 'Map of overload keys and coderefs', default => {} }, | ||
| 29 | has => { is => 'ARRAY', doc => 'List of properties and their definitions' }, | ||
| 30 | roles => { is => 'ARRAY', doc => 'List of other role names composed into this role', default => [] }, | ||
| 31 | requires => { is => 'ARRAY', doc => 'List of properties required of consuming classes', default => [] }, | ||
| 32 | attributes_have => { is => 'HASH', doc => 'Meta-attributes for properites' }, | ||
| 33 | excludes => { is => 'ARRAY', doc => 'List of Role names that cannot compose with this role', default => [] }, | ||
| 34 | method_modifiers => { is => 'UR::Role::MethodModifier', | ||
| 35 | is_many => 1, | ||
| 36 | doc => q(List of 'before', 'after' and 'around' method modifiers), | ||
| 37 | reverse_as => 'role' | ||
| 38 | }, | ||
| 39 | map { $_ => _get_property_desc_from_ur_object_type($_) } | ||
| 40 | meta_properties_to_compose_into_classes(), | ||
| 41 | ], | ||
| 42 | is_transactional => 0, | ||
| 43 | ); | ||
| 44 | |||
| 45 | sub property_data { | ||
| 46 | my($self, $property_name) = @_; | ||
| 47 | return $self->has->{$property_name}; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub has_property_names { | ||
| 51 | my $self = shift; | ||
| 52 | return keys %{ $self->has }; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub id_by_property_names { | ||
| 56 | my $self = shift; | ||
| 57 | return @{ $self->id_by }; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub method_names { | ||
| 61 | my $self = shift; | ||
| 62 | return keys %{ $self->methods }; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub meta_properties_to_compose_into_classes { | ||
| 66 | return qw( is_abstract is_final is_singleton doc | ||
| 67 | composite_id_separator id_generator valid_signals | ||
| 68 | subclassify_by subclass_description_preprocessor sub_classification_method_name | ||
| 69 | sub_classification_meta_class_name | ||
| 70 | schema_name data_source_id table_name select_hint join_hint ); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub define { | ||
| 74 | my $class = shift; | ||
| 75 | my $desc = $class->_normalize_role_description(@_); | ||
| 76 | |||
| 77 | unless ($desc->{role_name}) { | ||
| 78 | Carp::croak(q('role_name' is a required parameter for defining a role)); | ||
| 79 | } | ||
| 80 | |||
| 81 | my $methods = _introspect_methods($desc->{role_name}); | ||
| 82 | my $overloads = _introspect_overloads($desc->{role_name}); | ||
| 83 | |||
| 84 | my $extra = delete $desc->{extra}; | ||
| 85 | my $role = UR::Role::Prototype->__define__(%$desc, methods => $methods, overloads => $overloads); | ||
| 86 | |||
| 87 | if ($extra and %$extra) { | ||
| 88 | $role->UR::Object::Type::_apply_extra_attrs_to_class_or_role($extra); | ||
| 89 | } | ||
| 90 | |||
| 91 | $role->_inject_instance_constructor_into_namespace(); | ||
| 92 | |||
| 93 | return $role; | ||
| 94 | } | ||
| 95 | |||
| 96 | our @ROLE_DESCRIPTION_KEY_MAPPINGS = ( | ||
| 97 | @UR::Object::Type::CLASS_DESCRIPTION_KEY_MAPPINGS_COMMON_TO_CLASSES_AND_ROLES, | ||
| 98 | [ role_name => qw// ], | ||
| 99 | [ methods => qw// ], | ||
| 100 | [ requires => qw// ], | ||
| 101 | [ excludes => qw// ], | ||
| 102 | ); | ||
| 103 | |||
| 104 | sub _normalize_role_description { | ||
| 105 | my $class = shift; | ||
| 106 | my $old_role = { @_ }; | ||
| 107 | |||
| 108 | my $role_name = delete $old_role->{role_name}; | ||
| 109 | |||
| 110 | my $new_role = { | ||
| 111 | role_name => $role_name, | ||
| 112 | has => {}, | ||
| 113 | attributes_have => {}, | ||
| 114 | UR::Object::Type::_canonicalize_class_params($old_role, \@ROLE_DESCRIPTION_KEY_MAPPINGS), | ||
| 115 | }; | ||
| 116 | |||
| 117 | # The above call to _canonicalize_class_params removed recognized keys. Anything | ||
| 118 | # left over wasn't recognized | ||
| 119 | $new_role->{extra} = $old_role; | ||
| 120 | |||
| 121 | foreach my $key (qw( requires excludes ) ) { | ||
| 122 | unless (UR::Util::ensure_arrayref($new_role, $key)) { | ||
| 123 | Carp::croak("The '$key' metadata for role $role_name must be an arrayref"); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | # UR::Object::Type::_normalize_class_description_impl() copies these over before | ||
| 128 | # processing the properties. We need to, too | ||
| 129 | @$old_role{'has', 'attributes_have'} = @$new_role{'has','attributes_have'}; | ||
| 130 | @$new_role{'has','attributes_have'} = ( {}, {} ); | ||
| 131 | UR::Object::Type::_massage_field_into_arrayref($new_role, 'id_by'); | ||
| 132 | UR::Object::Type::_normalize_id_property_data($old_role, $new_role); | ||
| 133 | UR::Object::Type::_process_class_definition_property_keys($old_role, $new_role); | ||
| 134 | _complete_property_descriptions($new_role); | ||
| 135 | |||
| 136 | return $new_role; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub _complete_property_descriptions { | ||
| 140 | my $role_desc = shift; | ||
| 141 | |||
| 142 | # stole from UR::Object::Type::_normalize_class_description_impl() | ||
| 143 | my $properties = $role_desc->{has}; | ||
| 144 | foreach my $property_name ( keys %$properties ) { | ||
| 145 | my $old_property = $properties->{$property_name}; | ||
| 146 | my %new_property = UR::Object::Type->_normalize_property_description1($property_name, $old_property, $role_desc); | ||
| 147 | delete $new_property{class_name}; # above normalizer fills this in as undef | ||
| 148 | $properties->{$property_name} = \%new_property; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | my %property_definition_key_to_method_name = ( | ||
| 153 | is => 'data_type', | ||
| 154 | len => 'data_length', | ||
| 155 | ); | ||
| 156 | |||
| 157 | sub _get_property_desc_from_ur_object_type { | ||
| 158 | my $property_name = shift; | ||
| 159 | |||
| 160 | my $prop_meta = UR::Object::Property->get(class_name => 'UR::Object::Type', property_name => $property_name); | ||
| 161 | Carp::croak("Couldn't get UR::Object::Type property meta for $property_name") unless $prop_meta; | ||
| 162 | |||
| 163 | # These properties' definition key is the same as the method name | ||
| 164 | my %definition = map { $_ => $prop_meta->$_ } | ||
| 165 | grep { defined $prop_meta->$_ } | ||
| 166 | qw( is_many is_optional is_transient is_mutable default_value doc ); | ||
| 167 | |||
| 168 | # These have a translation | ||
| 169 | while(my($key, $method) = each(%property_definition_key_to_method_name)) { | ||
| 170 | $definition{$key} = $prop_meta->$method; | ||
| 171 | } | ||
| 172 | |||
| 173 | # For any UR::Object::Type properties that are required or have a default value, | ||
| 174 | # those don't apply to Roles | ||
| 175 | $definition{is_optional} = 1; | ||
| 176 | delete $definition{default_value}; | ||
| 177 | |||
| 178 | return \%definition; | ||
| 179 | } | ||
| 180 | |||
| 181 | { | ||
| 182 | my @overload_ops; | ||
| 183 | sub _all_overload_ops { | ||
| 184 | @overload_ops = map { split /\s+/ } values(%overload::ops) unless @overload_ops; | ||
| 185 | @overload_ops; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | my @DONT_EXPORT_THESE_SUBS_TO_CLASSES = qw(__import__ FETCH_CODE_ATTRIBUTES MODIFY_CODE_ATTRIBUTES MODIFY_SCALAR_ATTRIBUTES before after around); | ||
| 190 | sub _introspect_methods { | ||
| 191 | my $role_name = shift; | ||
| 192 | |||
| 193 | my $subs = UR::Util::coderefs_for_package($role_name); | ||
| 194 | delete @$subs{@DONT_EXPORT_THESE_SUBS_TO_CLASSES}; # don't allow __import__ to be exported to a class's namespace | ||
| 195 | delete @$subs{ map { "($_" } ( _all_overload_ops, ')', '(' ) }; | ||
| 196 | return $subs; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub _introspect_overloads { | ||
| 200 | my $role_name = shift; | ||
| 201 | |||
| 202 | return {} unless overload::Overloaded($role_name); | ||
| 203 | |||
| 204 | my %overloads; | ||
| 205 | my $stash = do { | ||
| 206 | no strict 'refs'; | ||
| 207 | \%{$role_name . '::'}; | ||
| 208 | }; | ||
| 209 | foreach my $op ( _all_overload_ops ) { | ||
| 210 | my $op_key = $op eq 'fallback' ? ')' : $op; | ||
| 211 | my $overloaded = $stash->{'(' . $op_key}; | ||
| 212 | |||
| 213 | if ($overloaded) { | ||
| 214 | my $subref = *{$overloaded}{CODE}; | ||
| 215 | $overloads{$op} = $subref eq \&overload::nil | ||
| 216 | ? ${*{$overloaded}{SCALAR}} # overridden with string method name | ||
| 217 | : $subref; # overridden with a subref | ||
| 218 | } | ||
| 219 | } | ||
| 220 | return \%overloads; | ||
| 221 | } | ||
| 222 | |||
| 223 | # Called by UR::Object::Type::Initializer::compose_roles to apply a role name | ||
| 224 | # to a partially constructed class description | ||
| 225 | sub _apply_roles_to_class_desc { | ||
| 226 | my($class, $desc) = @_; | ||
| 227 | if (ref($class) or ref($desc) ne 'HASH') { | ||
| 228 | Carp::croak('_apply_roles_to_class_desc() must be called as a class method on a basic class description'); | ||
| 229 | } | ||
| 230 | |||
| 231 | _validate_class_method_overrides_consumed_roles($desc); | ||
| 232 | return unless ($desc->{roles} and @{ $desc->{roles} }); | ||
| 233 | my @role_objs = _role_prototypes_with_params_for_class_desc($desc); | ||
| 234 | |||
| 235 | _validate_role_exclusions($desc, @role_objs); | ||
| 236 | _validate_role_requirements($desc, @role_objs); | ||
| 237 | _validate_class_desc_overrides($desc, @role_objs); | ||
| 238 | |||
| 239 | my $id_property_names_to_add = _collect_id_property_names_from_roles($desc, @role_objs); | ||
| 240 | my $properties_to_add = _collect_properties_from_roles($desc, @role_objs); | ||
| 241 | my $meta_properties_to_add = _collect_meta_properties_from_roles($desc, @role_objs); | ||
| 242 | my $overloads_to_add = _collect_overloads_from_roles($desc, @role_objs); | ||
| 243 | my $method_modifiers_to_add = _collect_method_modifiers_from_roles($desc, @role_objs); | ||
| 244 | |||
| 245 | _save_role_instances_to_class_desc($desc, @role_objs); | ||
| 246 | _assert_all_role_params_are_bound_to_values($desc, @role_objs); | ||
| 247 | do { $_->prototype->add_class_name($desc->{class_name}) } foreach @role_objs; | ||
| 248 | |||
| 249 | UR::Role::Param->replace_unbound_params_in_struct_with_values( | ||
| 250 | [ $id_property_names_to_add, $properties_to_add, $meta_properties_to_add, $overloads_to_add ], | ||
| 251 | @role_objs); | ||
| 252 | |||
| 253 | _import_methods_from_roles_into_namespace($desc->{class_name}, \@role_objs); | ||
| 254 | _apply_overloads_to_namespace($desc->{class_name}, $overloads_to_add); | ||
| 255 | _apply_method_modifiers_to_namespace($desc, $method_modifiers_to_add); | ||
| 256 | |||
| 257 | _merge_role_meta_properties_into_class_desc($desc, $meta_properties_to_add); | ||
| 258 | _merge_role_id_property_names_into_class_desc($desc, $id_property_names_to_add); | ||
| 259 | _merge_role_properties_into_class_desc($desc, $properties_to_add); | ||
| 260 | } | ||
| 261 | |||
| 262 | sub _save_role_instances_to_class_desc { | ||
| 263 | my($desc, @role_prototypes) = @_; | ||
| 264 | |||
| 265 | my $class_name = $desc->{class_name}; | ||
| 266 | my @instances = map { $_->instantiate_role_instance($class_name) } | ||
| 267 | @role_prototypes; | ||
| 268 | $desc->{roles} = \@instances; | ||
| 269 | } | ||
| 270 | |||
| 271 | sub _assert_all_role_params_are_bound_to_values { | ||
| 272 | my($desc, @role_instances) = @_; | ||
| 273 | |||
| 274 | foreach my $instance ( @role_instances ) { | ||
| 275 | my $role_name = $instance->role_name; | ||
| 276 | my %expected_params = map { $_ => 1 } | ||
| 277 | UR::Role::Param->param_names_for_role($role_name); | ||
| 278 | my $got_params = $instance->role_params; | ||
| 279 | if (my @missing = grep { ! exists($got_params->{$_}) } keys %expected_params) { | ||
| 280 | Carp::croak("Role $role_name expects values for these params: ",join(', ', @missing)); | ||
| 281 | } | ||
| 282 | if (my @extra = grep { ! exists($expected_params{$_}) } keys %$got_params) { | ||
| 283 | Carp::croak("Role $role_name does not recognize these params: ",join(', ', @extra)); | ||
| 284 | } | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | |||
| 289 | sub _merge_role_meta_properties_into_class_desc { | ||
| 290 | my($desc, $meta_properties_to_add) = @_; | ||
| 291 | |||
| 292 | my $valid_signals = delete $meta_properties_to_add->{valid_signals}; | ||
| 293 | my @meta_prop_names = keys %$meta_properties_to_add; | ||
| 294 | @$desc{@meta_prop_names} = @$meta_properties_to_add{@meta_prop_names}; | ||
| 295 | if ($valid_signals) { | ||
| 296 | push @{$desc->{valid_signals}}, @$valid_signals; | ||
| 297 | }; | ||
| 298 | } | ||
| 299 | |||
| 300 | sub _merge_role_properties_into_class_desc { | ||
| 301 | my($desc, $properties_to_add) = @_; | ||
| 302 | |||
| 303 | my @property_names = keys %$properties_to_add; | ||
| 304 | @{$desc->{has}}{@property_names} = @$properties_to_add{@property_names}; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub _merge_role_id_property_names_into_class_desc { | ||
| 308 | my($desc, $id_properties_to_add) = @_; | ||
| 309 | |||
| 310 | push @{$desc->{id_by}}, @$id_properties_to_add; | ||
| 311 | } | ||
| 312 | |||
| 313 | sub _role_prototypes_with_params_for_class_desc { | ||
| 314 | my $desc = shift; | ||
| 315 | |||
| 316 | my @role_prototypes; | ||
| 317 | foreach my $role_name ( @{ $desc->{roles} } ) { | ||
| 318 | my $role; | ||
| 319 | my $exception = do { | ||
| 320 | local $@; | ||
| 321 | $role = eval { $role_name->__role__ }; | ||
| 322 | $@; | ||
| 323 | }; | ||
| 324 | unless ($role) { | ||
| 325 | my $class_name = $desc->{class_name}; | ||
| 326 | Carp::croak("Cannot apply role $role_name to class $class_name: $exception"); | ||
| 327 | } | ||
| 328 | push @role_prototypes, $role; | ||
| 329 | } | ||
| 330 | return @role_prototypes; | ||
| 331 | } | ||
| 332 | |||
| 333 | sub _collect_id_property_names_from_roles { | ||
| 334 | my($desc, @role_objs) = @_; | ||
| 335 | |||
| 336 | my %class_id_by_properties = map { $_ => 1 } @{ $desc->{id_by} }; | ||
| 337 | my %class_property_is_id_by = map { $_ => $class_id_by_properties{$_} } | ||
| 338 | keys %{ $desc->{has} }; | ||
| 339 | |||
| 340 | my @property_names_to_add; | ||
| 341 | foreach my $role ( @role_objs ) { | ||
| 342 | my @role_id_property_names = $role->id_by_property_names; | ||
| 343 | |||
| 344 | my @conflict = grep { exists($class_property_is_id_by{$_}) and ! $class_property_is_id_by{$_} } | ||
| 345 | @role_id_property_names; | ||
| 346 | if (@conflict) { | ||
| 347 | Carp::croak(sprintf(q(Cannot compose role %s: Property '%s' was declared as a normal property in class %s, but as an ID property in the role), | ||
| 348 | $role->role_name, | ||
| 349 | join(q(', '), @conflict), | ||
| 350 | $desc->{class_name}, | ||
| 351 | )); | ||
| 352 | } | ||
| 353 | push @property_names_to_add, @role_id_property_names; | ||
| 354 | } | ||
| 355 | return \@property_names_to_add; | ||
| 356 | } | ||
| 357 | |||
| 358 | sub _collect_properties_from_roles { | ||
| 359 | my($desc, @role_objs) = @_; | ||
| 360 | |||
| 361 | my $properties_from_class = $desc->{has}; | ||
| 362 | |||
| 363 | my(%properties_to_add, %source_for_properties_to_add); | ||
| 364 | foreach my $role ( @role_objs ) { | ||
| 365 | my @role_property_names = $role->has_property_names; | ||
| 366 | foreach my $property_name ( @role_property_names ) { | ||
| 367 | my $prop_definition = $role->property_data($property_name); | ||
| 368 | if (my $conflict = $source_for_properties_to_add{$property_name}) { | ||
| 369 | Carp::croak(sprintf(q(Cannot compose role %s: Property '%s' conflicts with property in role %s), | ||
| 370 | $role->role_name, $property_name, $conflict)); | ||
| 371 | } | ||
| 372 | |||
| 373 | $source_for_properties_to_add{$property_name} = $role->role_name; | ||
| 374 | |||
| 375 | next if exists $properties_from_class->{$property_name}; | ||
| 376 | |||
| 377 | $properties_to_add{$property_name} = $prop_definition; | ||
| 378 | } | ||
| 379 | } | ||
| 380 | return UR::Util::deep_copy(\%properties_to_add); | ||
| 381 | } | ||
| 382 | |||
| 383 | sub _collect_overloads_from_roles { | ||
| 384 | my($desc, @role_objs) = @_; | ||
| 385 | |||
| 386 | my $overloads_from_class = _introspect_overloads($desc->{class_name}); | ||
| 387 | |||
| 388 | my(%overloads_to_add, %source_for_overloads_to_add); | ||
| 389 | my $fallback_validator = _create_fallback_validator(); | ||
| 390 | |||
| 391 | foreach my $role ( @role_objs ) { | ||
| 392 | my $role_name = $role->role_name; | ||
| 393 | my $overloads_this_role = $role->overloads; | ||
| 394 | |||
| 395 | $fallback_validator->($role_name, $overloads_this_role->{fallback}); | ||
| 396 | while( my($op, $impl) = each(%$overloads_this_role)) { | ||
| 397 | next if ($op eq 'fallback'); | ||
| 398 | if (my $conflict = $source_for_overloads_to_add{$op}) { | ||
| 399 | Carp::croak("Cannot compose role $role_name: Overload '$op' conflicts with overload in role $conflict"); | ||
| 400 | } | ||
| 401 | $source_for_overloads_to_add{$op} = $role_name; | ||
| 402 | |||
| 403 | next if exists $overloads_from_class->{$op}; | ||
| 404 | |||
| 405 | $overloads_to_add{$op} = $impl; | ||
| 406 | } | ||
| 407 | } | ||
| 408 | |||
| 409 | my $fallback = $fallback_validator->(); | ||
| 410 | $overloads_to_add{fallback} = $fallback if defined $fallback; | ||
| 411 | return \%overloads_to_add; | ||
| 412 | } | ||
| 413 | |||
| 414 | sub _collect_method_modifiers_from_roles { | ||
| 415 | my($desc, @role_objs) = @_; | ||
| 416 | |||
| 417 | my $class_name = $desc->{class_name}; | ||
| 418 | my @all_modifiers = map { $_->method_modifiers } @role_objs; | ||
| 419 | |||
| 420 | my $isa = join('::', $class_name, 'ISA'); | ||
| 421 | no strict 'refs'; | ||
| 422 | local @$isa = (@$isa, @{$desc->{is}}); | ||
| 423 | use strict 'refs'; | ||
| 424 | |||
| 425 | foreach my $mod ( @all_modifiers ) { | ||
| 426 | unless ($class_name->can($mod->name)) { | ||
| 427 | my $role_name = $mod->role->role_name; | ||
| 428 | my $type = $mod->type; | ||
| 429 | my $subname = $mod->name; | ||
| 430 | Carp::croak(qq(Cannot compose role $role_name: Cannot apply '$type' method modifier: Method "$subname" not found via class $class_name)); | ||
| 431 | } | ||
| 432 | } | ||
| 433 | return \@all_modifiers; | ||
| 434 | } | ||
| 435 | |||
| 436 | sub _create_fallback_validator { | ||
| 437 | my($fallback, $fallback_set_in); | ||
| 438 | |||
| 439 | return sub { | ||
| 440 | unless (@_) { | ||
| 441 | # no args, return current value | ||
| 442 | return $fallback; | ||
| 443 | } | ||
| 444 | |||
| 445 | my($role_name, $value) = @_; | ||
| 446 | if (defined($value) and !defined($fallback)) { | ||
| 447 | $fallback = $value; | ||
| 448 | $fallback_set_in = $role_name; | ||
| 449 | return 1; | ||
| 450 | } | ||
| 451 | return 1 unless (defined($fallback) and defined ($value)); | ||
| 452 | return 1 unless ($fallback xor $value); | ||
| 453 | |||
| 454 | Carp::croak(sprintf(q(Cannot compose role %s: fallback value '%s' conflicts with fallback value '%s' in role %s), | ||
| 455 | $role_name, | ||
| 456 | $value ? $value : defined($value) ? 'FALSE' : 'UNDEF', | ||
| 457 | $fallback ? $fallback : defined($fallback) ? 'FALSE' : 'UNDEF', | ||
| 458 | $fallback_set_in)); | ||
| 459 | }; | ||
| 460 | } | ||
| 461 | |||
| 462 | |||
| 463 | sub _collect_meta_properties_from_roles { | ||
| 464 | my($desc, @role_objs) = @_; | ||
| 465 | |||
| 466 | my(%meta_properties_to_add, %source_for_meta_properties_to_add); | ||
| 467 | foreach my $role ( @role_objs ) { | ||
| 468 | foreach my $meta_prop_name ( $role->meta_properties_to_compose_into_classes ) { | ||
| 469 | next if (defined $desc->{$meta_prop_name} and $meta_prop_name ne 'valid_signals'); | ||
| 470 | next unless defined $role->$meta_prop_name; | ||
| 471 | |||
| 472 | if ($meta_prop_name ne 'valid_signals') { | ||
| 473 | if (exists $meta_properties_to_add{$meta_prop_name}) { | ||
| 474 | Carp::croak(sprintf(q(Cannot compose role %s: Meta property '%s' conflicts with meta property from role %s), | ||
| 475 | $role->role_name, | ||
| 476 | $meta_prop_name, | ||
| 477 | $source_for_meta_properties_to_add{$meta_prop_name})); | ||
| 478 | } | ||
| 479 | $meta_properties_to_add{$meta_prop_name} = $role->$meta_prop_name; | ||
| 480 | $source_for_meta_properties_to_add{$meta_prop_name} = $role->role_name; | ||
| 481 | } else { | ||
| 482 | $meta_properties_to_add{valid_signals} ||= []; | ||
| 483 | push @{ $meta_properties_to_add{valid_signals} }, @{ $role->valid_signals }; | ||
| 484 | } | ||
| 485 | } | ||
| 486 | } | ||
| 487 | return UR::Util::deep_copy(\%meta_properties_to_add); | ||
| 488 | } | ||
| 489 | |||
| 490 | sub _validate_role_requirements { | ||
| 491 | my($desc, @role_objs) = @_; | ||
| 492 | |||
| 493 | my $class_name = $desc->{class_name}; | ||
| 494 | my %found_properties_and_methods = map { $_ => 1 } keys %{ $desc->{has} }; | ||
| 495 | |||
| 496 | foreach my $role ( @role_objs ) { | ||
| 497 | foreach my $requirement ( @{ $role->requires } ) { | ||
| 498 | unless ($found_properties_and_methods{ $requirement } | ||
| 499 | ||= _class_desc_lineage_has_method_or_property($desc, $requirement)) | ||
| 500 | { | ||
| 501 | my $role_name = $role->role_name; | ||
| 502 | Carp::croak("Cannot compose role $role_name: missing required property or method '$requirement'"); | ||
| 503 | } | ||
| 504 | } | ||
| 505 | |||
| 506 | # Properties and methods from this role can satisfy requirements for later roles | ||
| 507 | foreach my $name ( $role->has_property_names, $role->method_names ) { | ||
| 508 | $found_properties_and_methods{$name} = 1; | ||
| 509 | } | ||
| 510 | } | ||
| 511 | |||
| 512 | return 1; | ||
| 513 | } | ||
| 514 | |||
| 515 | sub _validate_role_exclusions { | ||
| 516 | my($desc, @role_objs) = @_; | ||
| 517 | |||
| 518 | my %role_names = map { $_ => $_ } @{ $desc->{roles} }; | ||
| 519 | foreach my $role ( @role_objs ) { | ||
| 520 | |||
| 521 | my @conflicts = grep { defined } | ||
| 522 | @role_names{ @{ $role->excludes } }; | ||
| 523 | if (@conflicts) { | ||
| 524 | my $class_name = $desc->{class_name}; | ||
| 525 | my $plural = @conflicts > 1 ? 's' : ''; | ||
| 526 | Carp::croak(sprintf('Cannot compose role%s %s into class %s: Role %s excludes %s', | ||
| 527 | $plural, | ||
| 528 | join(', ', @conflicts), | ||
| 529 | $desc->{class_name}, | ||
| 530 | $role->role_name, | ||
| 531 | $plural ? 'them' : 'it')); | ||
| 532 | } | ||
| 533 | } | ||
| 534 | return 1; | ||
| 535 | } | ||
| 536 | |||
| 537 | sub _validate_class_method_overrides_consumed_roles { | ||
| 538 | my $desc = shift; | ||
| 539 | |||
| 540 | my $class_name = $desc->{class_name}; | ||
| 541 | my %this_class_role_names = $desc->{roles} | ||
| 542 | ? map { ref($_) ? ($_->role_name => 1) : ($_ => 1) } | ||
| 543 | @{$desc->{roles}} | ||
| 544 | : (); | ||
| 545 | my $this_class_methods = UR::Util::coderefs_for_package($class_name); | ||
| 546 | while (my($method_name, $subref) = each %$this_class_methods) { | ||
| 547 | my @overrides = UR::AttributeHandlers::get_overrides_for_coderef($subref); | ||
| 548 | next unless (@overrides); | ||
| 549 | |||
| 550 | my @missing_role_names = grep { ! exists $this_class_role_names{$_} } | ||
| 551 | @overrides; | ||
| 552 | if (@missing_role_names) { | ||
| 553 | Carp::croak("Class method '$method_name' declares Overrides for roles the class does not consume: " | ||
| 554 | . join(', ', @missing_role_names)); | ||
| 555 | } | ||
| 556 | } | ||
| 557 | return 1; | ||
| 558 | } | ||
| 559 | |||
| 560 | sub _validate_class_desc_overrides { | ||
| 561 | my($desc, @roles) = @_; | ||
| 562 | |||
| 563 | my $class_name = $desc->{class_name}; | ||
| 564 | my %this_class_methods = map { %{ UR::Util::coderefs_for_package($_) } } | ||
| 565 | (@{$desc->{is}}, $class_name); | ||
| 566 | |||
| 567 | my %overridden_methods_by_role; | ||
| 568 | foreach my $method_name ( keys %this_class_methods ) { | ||
| 569 | if (my @role_names = UR::AttributeHandlers::get_overrides_for_coderef($this_class_methods{$method_name})) { | ||
| 570 | foreach my $role_name ( @role_names ) { | ||
| 571 | $overridden_methods_by_role{$role_name} ||= []; | ||
| 572 | push @{$overridden_methods_by_role{$role_name}}, $method_name; | ||
| 573 | } | ||
| 574 | } | ||
| 575 | } | ||
| 576 | |||
| 577 | foreach my $role ( @roles ) { | ||
| 578 | my $role_name = $role->role_name; | ||
| 579 | my $this_role_methods = $role->methods; | ||
| 580 | my @this_role_method_names = keys( %$this_role_methods ); | ||
| 581 | |||
| 582 | my %method_is_overridden; | ||
| 583 | my @conflict_methods = grep { ! ($method_is_overridden{$_} ||= _coderef_overrides_package($this_class_methods{$_}, $role_name)) } | ||
| 584 | grep { exists $this_class_methods{$_} } | ||
| 585 | @this_role_method_names; | ||
| 586 | if (@conflict_methods) { | ||
| 587 | my $plural = scalar(@conflict_methods) > 1 ? 's' : ''; | ||
| 588 | my $conflicts = scalar(@conflict_methods) > 1 ? 'conflict' : 'conflicts'; | ||
| 589 | |||
| 590 | my %conflicting_sources; | ||
| 591 | CONFLICTING_METHOD_NAME: | ||
| 592 | foreach my $conflicting_method_name ( @conflict_methods ) { | ||
| 593 | foreach my $source_class_name ( $class_name, @{$desc->{is}} ) { | ||
| 594 | if ($source_class_name->can($conflicting_method_name)) { | ||
| 595 | $conflicting_sources{$conflicting_method_name} = $source_class_name; | ||
| 596 | next CONFLICTING_METHOD_NAME; | ||
| 597 | } | ||
| 598 | } | ||
| 599 | $conflicting_sources{$conflicting_method_name} = '<unknown>'; | ||
| 600 | } | ||
| 601 | Carp::croak("Cannot compose role $role_name: " | ||
| 602 | . "Method name${plural} $conflicts with class $class_name:\n" | ||
| 603 | . join("\n", map { sprintf("\t%s (from %s)\n", $_, $conflicting_sources{$_}) } | ||
| 604 | keys %conflicting_sources) | ||
| 605 | . "Did you forget to add the 'Overrides' attribute?"); | ||
| 606 | } | ||
| 607 | |||
| 608 | my @missing_methods = grep { ! exists $this_role_methods->{$_} and ! exists $role->has->{$_} } | ||
| 609 | @{$overridden_methods_by_role{$role_name}}; | ||
| 610 | if (@missing_methods) { | ||
| 611 | my $plural = scalar(@missing_methods) > 1 ? 's' : ''; | ||
| 612 | my $method_list = join(q(', '), @missing_methods); | ||
| 613 | Carp::croak("Cannot compose role $role_name: " | ||
| 614 | . "Class method${plural} '$method_list' declares it Overrides non-existant method in the role."); | ||
| 615 | } | ||
| 616 | } | ||
| 617 | |||
| 618 | return 1; | ||
| 619 | } | ||
| 620 | |||
| 621 | sub _class_desc_lineage_has_method_or_property { | ||
| 622 | my($desc, $requirement) = @_; | ||
| 623 | |||
| 624 | my $class_name = $desc->{class_name}; | ||
| 625 | if (my $can = $class_name->can($requirement)) { | ||
| 626 | return $can; | ||
| 627 | } | ||
| 628 | |||
| 629 | my @is = @{ $desc->{is} }; | ||
| 630 | my %seen; | ||
| 631 | while(my $parent = shift @is) { | ||
| 632 | next if $seen{$parent}++; | ||
| 633 | |||
| 634 | if (my $can = $parent->can($requirement)) { | ||
| 635 | return $can; | ||
| 636 | } | ||
| 637 | |||
| 638 | my $parent_meta = $parent->__meta__; | ||
| 639 | if (my $prop_meta = $parent_meta->property($requirement)) { | ||
| 640 | return $prop_meta; | ||
| 641 | } | ||
| 642 | } | ||
| 643 | return; | ||
| 644 | } | ||
| 645 | |||
| 646 | sub _import_methods_from_roles_into_namespace { | ||
| 647 | my($class_name, $roles) = @_; | ||
| 648 | |||
| 649 | my $this_class_methods = UR::Util::coderefs_for_package($class_name); | ||
| 650 | |||
| 651 | my(%all_imported_methods, %method_sources); | ||
| 652 | foreach my $role ( @$roles ) { | ||
| 653 | my $this_role_methods = $role->methods; | ||
| 654 | my @this_role_method_names = keys( %$this_role_methods ); | ||
| 655 | |||
| 656 | my @conflicting = grep { ! exists($this_class_methods->{$_}) } # not a conflict if the class overrides | ||
| 657 | grep { exists $all_imported_methods{$_} } | ||
| 658 | @this_role_method_names; | ||
| 659 | |||
| 660 | if (@conflicting) { | ||
| 661 | my $plural = scalar(@conflicting) > 1 ? 's' : ''; | ||
| 662 | my $conflicts = scalar(@conflicting) > 1 ? 'conflict' : 'conflicts'; | ||
| 663 | Carp::croak('Cannot compose role ' . $role->role_name | ||
| 664 | . ": method${plural} $conflicts with those defined in other roles\n\t" | ||
| 665 | . join("\n\t", join('::', map { ( $method_sources{$_}, $_ ) } @conflicting))); | ||
| 666 | } | ||
| 667 | |||
| 668 | @method_sources{ @this_role_method_names } = ($role->role_name) x @this_role_method_names; | ||
| 669 | @all_imported_methods{ @this_role_method_names } = @$this_role_methods{ @this_role_method_names }; | ||
| 670 | } | ||
| 671 | |||
| 672 | delete @all_imported_methods{ keys %$this_class_methods }; # Don't import roles' methods already defined on the class | ||
| 673 | foreach my $name ( keys %all_imported_methods ) { | ||
| 674 | Sub::Install::install_sub({ | ||
| 675 | code => $all_imported_methods{$name}, | ||
| 676 | as => $name, | ||
| 677 | into => $class_name, | ||
| 678 | }); | ||
| 679 | } | ||
| 680 | } | ||
| 681 | |||
| 682 | sub _coderef_overrides_package { | ||
| 683 | my($coderef, $package) = @_; | ||
| 684 | |||
| 685 | my @overrides = UR::AttributeHandlers::get_overrides_for_coderef($coderef); | ||
| 686 | return any { $_ eq $package } @overrides; | ||
| 687 | } | ||
| 688 | |||
| 689 | sub _apply_overloads_to_namespace { | ||
| 690 | my($class_name, $overloads) = @_; | ||
| 691 | |||
| 692 | my(%cooked_overloads); | ||
| 693 | while( my($op, $impl) = each %$overloads) { | ||
| 694 | $cooked_overloads{$op} = ref $impl | ||
| 695 | ? sprintf(q($overloads->{'%s'}), $op) | ||
| 696 | : qq('$impl'); | ||
| 697 | } | ||
| 698 | |||
| 699 | my $string = "package $class_name;\n" | ||
| 700 | . 'use overload ' | ||
| 701 | . join(",\n\t", map { sprintf(q('%s' => %s), $_, $cooked_overloads{$_}) } keys %cooked_overloads) | ||
| 702 | . ';'; | ||
| 703 | |||
| 704 | my $exception; | ||
| 705 | do { | ||
| 706 | local $@; | ||
| 707 | eval $string; | ||
| 708 | $exception = $@; | ||
| 709 | }; | ||
| 710 | |||
| 711 | if ($exception) { | ||
| 712 | Carp::croak("Failed to apply overloads to package $class_name: $exception"); | ||
| 713 | } | ||
| 714 | return 1; | ||
| 715 | } | ||
| 716 | |||
| 717 | sub _apply_method_modifiers_to_namespace { | ||
| 718 | my($desc, $modifiers_list) = @_; | ||
| 719 | |||
| 720 | my $class_name = $desc->{class_name}; | ||
| 721 | |||
| 722 | my $isa = join('::', $class_name, 'ISA'); | ||
| 723 | no strict 'refs'; | ||
| 724 | local @$isa = (@$isa, @{$desc->{is}}); | ||
| 725 | use strict 'refs'; | ||
| 726 | |||
| 727 | foreach my $mod ( @$modifiers_list ) { | ||
| 728 | $mod->apply_to_package($class_name); | ||
| 729 | } | ||
| 730 | 1; | ||
| 731 | } | ||
| 732 | |||
| 733 | sub _define_role { | ||
| 734 | my($role_name, $func, @params) = @_; | ||
| 735 | |||
| 736 | if (defined($func) and $func eq "role" and @params > 1 and $role_name ne "UR::Role") { | ||
| 737 | my @role_params; | ||
| 738 | if (@params == 2 and ref($params[1]) eq 'HASH') { | ||
| 739 | @role_params = %{ $params[1] }; | ||
| 740 | } | ||
| 741 | elsif (@params == 2 and ref($params[1]) eq 'ARRAY') { | ||
| 742 | @role_params = @{ $params[1] }; | ||
| 743 | } | ||
| 744 | else { | ||
| 745 | @role_params = @params[1..$#params]; | ||
| 746 | } | ||
| 747 | my $role = UR::Role->define(role_name => $role_name, @role_params); | ||
| 748 | unless ($role) { | ||
| 749 | Carp::croak "error defining role $role_name!"; | ||
| 750 | } | ||
| 751 | return sub { $role_name }; | ||
| 752 | } else { | ||
| 753 | return; | ||
| 754 | } | ||
| 755 | } | ||
| 756 | |||
| 757 | sub _inject_instance_constructor_into_namespace { | ||
| 758 | my $self = shift; | ||
| 759 | |||
| 760 | my $package = $self->role_name; | ||
| 761 | my $full_name = join('::', $package, 'create'); | ||
| 762 | my $sub = Sub::Name::subname $full_name => sub { | ||
| 763 | my($class, %params) = @_; | ||
| 764 | return UR::Role::PrototypeWithParams->create(prototype => $self, role_params => \%params); | ||
| 765 | }; | ||
| 766 | Sub::Install::reinstall_sub({ | ||
| 767 | into => $package, | ||
| 768 | as => 'create', | ||
| 769 | code => $sub, | ||
| 770 | }); | ||
| 771 | |||
| 772 | Sub::Install::reinstall_sub({ | ||
| 773 | into => $package, | ||
| 774 | as => '__role__', | ||
| 775 | code => sub { $package->create() }, | ||
| 776 | }); | ||
| 777 | } | ||
| 778 | |||
| 779 | 1; | ||
| 780 | |||
| 781 | __END__ |