| 1 | |||
| 2 | package UR::ModuleLoader; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | require UR; | ||
| 7 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 8 | |||
| 9 | Class::Autouse->autouse(\&dynamically_load_class); | ||
| 10 | Class::Autouse->autouse(\&dynamically_load_role); | ||
| 11 | Class::Autouse->sugar(\&define_class); | ||
| 12 | |||
| 13 | our @CARP_NOT = qw(Class::Autouse UR::Namespace); | ||
| 14 | |||
| 15 | sub define_class { | ||
| 16 | my ($class,$func,@params) = @_; | ||
| 17 | return unless $UR::initialized; | ||
| 18 | return unless $Class::Autouse::ORIGINAL_CAN->("UR::Object::Type","get"); | ||
| 19 | |||
| 20 | # Handle the special case of defining a new class | ||
| 21 | # This lets us have the effect of a UNIVERSAL::class method, w/o mucking with UNIVERSAL | ||
| 22 | if (defined($func) and $func eq "class" and @params > 1 and $class ne "UR::Object::Type") { | ||
| 23 | my @class_params; | ||
| 24 | if (@params == 2 and ref($params[1]) eq 'HASH') { | ||
| 25 | @class_params = %{ $params[1] }; | ||
| 26 | } | ||
| 27 | elsif (@params == 2 and ref($params[1]) eq 'ARRAY') { | ||
| 28 | @class_params = @{ $params[1] }; | ||
| 29 | } | ||
| 30 | else { | ||
| 31 | @class_params = @params[1..$#params]; | ||
| 32 | } | ||
| 33 | my $class_meta = UR::Object::Type->define(class_name => $class, @class_params); | ||
| 34 | unless ($class_meta) { | ||
| 35 | Carp::croak "error defining class $class!"; | ||
| 36 | } | ||
| 37 | return sub { $class }; | ||
| 38 | } | ||
| 39 | |||
| 40 | else { | ||
| 41 | return; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _should_dynamically_load_package { | ||
| 46 | my $package = shift; | ||
| 47 | # Don't even try to load unless we're done boostrapping somewhat. | ||
| 48 | return unless $UR::initialized; | ||
| 49 | return unless $Class::Autouse::ORIGINAL_CAN->("UR::Object::Type","get"); | ||
| 50 | |||
| 51 | # Some modules (Class::DBI, recently) call UNIVERSAL::can directly with things which don't even resemble | ||
| 52 | # class names. Skip doing any work on anything which isn't at least a two-part class name. | ||
| 53 | # We refuse explicitly to handle top-level namespaces below anyway, and this will keep us from | ||
| 54 | # slowing down other modules just to fail late. | ||
| 55 | |||
| 56 | my ($namespace) = ($package =~ /^(.*?)::/); | ||
| 57 | return unless $namespace; | ||
| 58 | |||
| 59 | unless ($namespace->isa("UR::Namespace")) { | ||
| 60 | return; | ||
| 61 | } | ||
| 62 | |||
| 63 | unless ($namespace->should_dynamically_load_class($package)) { | ||
| 64 | return; | ||
| 65 | } | ||
| 66 | |||
| 67 | return $namespace; | ||
| 68 | } | ||
| 69 | |||
| 70 | our %loading; | ||
| 71 | |||
| 72 | sub dynamically_load_class { | ||
| 73 | my ($class,$func,@params) = @_; | ||
| 74 | |||
| 75 | return unless my $namespace = _should_dynamically_load_package($class); | ||
| 76 | |||
| 77 | if (defined($func) and $func eq "class" and @params > 1 and $class ne "UR::Object::Type") { | ||
| 78 | # a "class" statement caught by the above define_class call | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | return if $loading{$class}; | ||
| 83 | local %loading = ( %loading, $class => 1 ); | ||
| 84 | |||
| 85 | # Attempt to get a class object, loading it as necessary (probably). | ||
| 86 | # TODO: this is a non-standard accessor | ||
| 87 | my $meta = $namespace->get_member_class($class); | ||
| 88 | unless ($meta) { | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | |||
| 92 | # Handle the case in which the class is not "generated". | ||
| 93 | # These are generated by default when used, so this is a corner case. | ||
| 94 | unless ($meta->generated()) | ||
| 95 | { | ||
| 96 | # we have a new class | ||
| 97 | # attempt to auto-generate it | ||
| 98 | unless ($meta->generate) | ||
| 99 | { | ||
| 100 | Carp::confess("failed to auto-generate $class"); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | # Return a descriptive error message for the caller. | ||
| 105 | my $fref; | ||
| 106 | if (defined $func) { | ||
| 107 | $fref = $class->can($func); | ||
| 108 | unless ($fref) { | ||
| 109 | Carp::confess("$class was auto-generated successfully but cannot find method $func"); | ||
| 110 | } | ||
| 111 | return $fref; | ||
| 112 | } | ||
| 113 | |||
| 114 | return 1; | ||
| 115 | }; | ||
| 116 | |||
| 117 | sub dynamically_load_role { | ||
| 118 | my($role_name, $func, @params) = @_; | ||
| 119 | |||
| 120 | return unless _should_dynamically_load_package($role_name); | ||
| 121 | |||
| 122 | return if $loading{$role_name}; | ||
| 123 | local %loading = ( %loading, $role_name => 1 ); | ||
| 124 | |||
| 125 | # The module may have actually been loaded by dynamically_load_class(), | ||
| 126 | # but failed the check for class-ness | ||
| 127 | if (UR::Role::Prototype->is_loaded($role_name) | ||
| 128 | && | ||
| 129 | $role_name->can($func) | ||
| 130 | ) { | ||
| 131 | return 1; | ||
| 132 | } | ||
| 133 | |||
| 134 | if (UR::Util::use_package_optimistically($role_name)) { | ||
| 135 | if (UR::Role::Prototype->is_loaded($role_name) | ||
| 136 | && | ||
| 137 | $role_name->can($func) | ||
| 138 | ) { | ||
| 139 | return 1; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | |||
| 149 | =pod | ||
| 150 | |||
| 151 | =head1 NAME | ||
| 152 | |||
| 153 | UR::ModuleLoader - UR hooks into Class::Autouse | ||
| 154 | |||
| 155 | =head1 DESCRIPTION | ||
| 156 | |||
| 157 | UR uses Class::Autouse to handle automagic loading for modules. As long | ||
| 158 | as some part of an application "use"s a Namespace module, the autoloader | ||
| 159 | will handle loading modules under that namespace when they are needed. | ||
| 160 | |||
| 161 | =head1 SEE ALSO | ||
| 162 | |||
| 163 | UR, UR::Namespace | ||
| 164 | |||
| 165 | =cut |