| 1 | #======================================================================== | ||
| 2 | # | ||
| 3 | # Badger::Modules | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Module for loading and instantiating other modules. | ||
| 7 | # | ||
| 8 | # NOTE | ||
| 9 | # Badger::Factory is being cleaved in twain. Badger::Modules will | ||
| 10 | # implement the lower level parts related to finding and loading | ||
| 11 | # modules. Badger::Factory will be a subclass specialised for creating | ||
| 12 | # object instances. | ||
| 13 | # | ||
| 14 | # AUTHOR | ||
| 15 | # Andy Wardley <abw@wardley.org> | ||
| 16 | # | ||
| 17 | #======================================================================== | ||
| 18 | |||
| 19 | package Badger::Modules; | ||
| 20 | |||
| 21 | use Carp; | ||
| 22 | use Badger::Debug ':dump'; | ||
| 23 | use Badger::Class | ||
| 24 | version => 0.01, | ||
| 25 | debug => 0, | ||
| 26 | base => 'Badger::Prototype Badger::Exporter', | ||
| 27 | import => 'class', | ||
| 28 | utils => 'plural blessed textlike dotid camel_case', | ||
| 29 | accessors => 'item items', | ||
| 30 | words => 'ITEM ITEMS ISA TOLERANT BADGER_LOADED', | ||
| 31 | constants => 'PKG ARRAY HASH REFS ONCE DEFAULT', | ||
| 32 | constant => { | ||
| 33 | OBJECT => 'object', | ||
| 34 | FOUND => 'found', | ||
| 35 | FOUND_REF => 'found_ref', | ||
| 36 | PATH_SUFFIX => '_PATH', | ||
| 37 | NAMES_SUFFIX => '_NAMES', | ||
| 38 | DEFAULT_SUFFIX => '_DEFAULT', | ||
| 39 | }, | ||
| 40 | methods => { | ||
| 41 | init => \&init_modules, | ||
| 42 | throws => \&item, | ||
| 43 | }, | ||
| 44 | messages => { | ||
| 45 | no_item => 'No item(s) specified for factory to manage', | ||
| 46 | no_default => 'No default defined for %s factory', | ||
| 47 | bad_ref => 'Invalid reference for %s factory item %s: %s', | ||
| 48 | bad_method => q{Can't locate object method "%s" via package "%s" at %s line %s}, | ||
| 49 | failed => q{Error loading %s module %s as %s: %s}, | ||
| 50 | }; | ||
| 51 | |||
| 52 | our $ITEM = 'module'; | ||
| 53 | |||
| 54 | |||
| 55 | sub init_modules { | ||
| 56 | my ($self, $config) = @_; | ||
| 57 | my $class = $self->class; | ||
| 58 | my ($item, $items); | ||
| 59 | |||
| 60 | $self->debug("initialising modules: ", $self->dump_data($config)) if DEBUG; | ||
| 61 | |||
| 62 | $config->{ tolerant } = $class->any_var(TOLERANT) | ||
| 63 | unless defined $config->{ tolerant }; | ||
| 64 | |||
| 65 | # 'item' and 'items' can be specified as config params or we look for | ||
| 66 | # $ITEM and $ITEMS variables in the current package or those of any | ||
| 67 | # base classes. NOTE: $ITEM and $ITEMS must be in the same package | ||
| 68 | unless ($item = $config->{ item }) { | ||
| 69 | foreach my $pkg ($class->heritage) { | ||
| 70 | no strict REFS; | ||
| 71 | no warnings ONCE; | ||
| 72 | |||
| 73 | if (defined ($item = ${ $pkg.PKG.ITEM })) { | ||
| 74 | $items = ${ $pkg.PKG.ITEMS }; | ||
| 75 | last; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | return $self->error_msg('no_item') | ||
| 80 | unless $item; | ||
| 81 | |||
| 82 | # use 'items' in config, or grokked from $ITEMS, or guess plural | ||
| 83 | $items = $config->{ items } || $items || plural($item); | ||
| 84 | |||
| 85 | my $ipath = $item.PATH_SUFFIX; | ||
| 86 | my $inames = $item.NAMES_SUFFIX; | ||
| 87 | my $idefault = $item.DEFAULT_SUFFIX; | ||
| 88 | |||
| 89 | # Merge all XXXX_PATH package vars with any 'xxxx_path' or 'path' config | ||
| 90 | # items. Ditto for XXXX_NAME / 'xxxx_name' / 'aka' and XXXXS/ 'xxxxs' | ||
| 91 | |||
| 92 | my @path = @$config{ path => lc $ipath }; | ||
| 93 | my @names = @$config{ names => lc $inames }; | ||
| 94 | $self->{ path } = $class->list_vars(uc $ipath, @path); | ||
| 95 | $self->{ names } = $class->hash_vars(uc $inames, @names); | ||
| 96 | $self->{ $items } = $class->hash_vars(uc $items, $config->{ $items }); # TODO: this could clash | ||
| 97 | $self->{ tolerant } = $config->{ tolerant }; | ||
| 98 | $self->{ items } = $items; | ||
| 99 | $self->{ item } = $item; | ||
| 100 | $self->{ loaded } = { }; # TODO: make this the same thing? | ||
| 101 | |||
| 102 | $self->debug( | ||
| 103 | " Item: $self->{ item }\n", | ||
| 104 | "Items: $self->{ items }\n", | ||
| 105 | " Path: ", $ipath, ": ", $self->dump_data($self->{ path }), "\n", | ||
| 106 | "Names: ", $inames, ": ", $self->dump_data($self->{ names }) | ||
| 107 | ) if DEBUG; | ||
| 108 | |||
| 109 | return $self; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | sub path { | ||
| 114 | my $self = shift->prototype; | ||
| 115 | return @_ | ||
| 116 | ? ($self->{ path } = ref $_[0] eq ARRAY ? shift : [ @_ ]) | ||
| 117 | : $self->{ path }; | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | sub names { | ||
| 122 | my $self = shift->prototype; | ||
| 123 | my $names = $self->{ names }; | ||
| 124 | if (@_) { | ||
| 125 | my $args = ref $_[0] eq HASH ? shift : { @_ }; | ||
| 126 | @$names{ keys %$args } = values %$args; | ||
| 127 | } | ||
| 128 | return $names; | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | sub module_names { | ||
| 133 | my $self = shift; | ||
| 134 | my @bits = | ||
| 135 | map { camel_case($_) } | ||
| 136 | map { split /[\.]+/ } @_; | ||
| 137 | my %seen; | ||
| 138 | |||
| 139 | return ( | ||
| 140 | grep { ! $seen{ $_ }++ } | ||
| 141 | join( PKG, map { ucfirst $_ } @bits ), | ||
| 142 | join( PKG, @bits ) | ||
| 143 | ); | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | sub modules { | ||
| 148 | my $self = shift->prototype; | ||
| 149 | my $items = $self->{ $self->{ items } }; | ||
| 150 | if (@_) { | ||
| 151 | # NOTE: this doesn't have any effect... it's a artefact from | ||
| 152 | # Badger::Factory... we need to change $self->{ loaded } to be | ||
| 153 | # $self->{ items } | ||
| 154 | my $args = ref $_[0] eq HASH ? shift : { @_ }; | ||
| 155 | @$items{ keys %$args } = values %$args; | ||
| 156 | } | ||
| 157 | return $items; | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | sub module { | ||
| 162 | my $self = shift->prototype; | ||
| 163 | my $name = shift; | ||
| 164 | my $path = $self->{ path }; | ||
| 165 | my $loaded = $self->{ loaded }; | ||
| 166 | my ($module, $base, $alias, $found, $file, $symtab, @names, $size); | ||
| 167 | |||
| 168 | # Run the name through the name map to handle any unusual capitalisation, | ||
| 169 | # spelling, aliases, etc. | ||
| 170 | $name = $self->{ names }->{ $name } || $name; | ||
| 171 | |||
| 172 | # Then expand the name using whatever rules are in effect (e.g. the | ||
| 173 | # default which maps foo_bar to FooBar) | ||
| 174 | # FIXME: probably shouldn't do this if we found an entry in the names lookup | ||
| 175 | @names = $self->module_names($name); | ||
| 176 | |||
| 177 | LOOKUP: | ||
| 178 | foreach $base (@$path) { | ||
| 179 | foreach $alias (@names) { | ||
| 180 | $module = join(PKG, $base, $alias); | ||
| 181 | |||
| 182 | # TODO: look in $self->{ $items } for pre-defined result... | ||
| 183 | |||
| 184 | # See if we've previously loaded a module with this name (true | ||
| 185 | # value) or failed to load a module (defined but false value) | ||
| 186 | if ($found = $loaded->{ $module }) { | ||
| 187 | $self->debug("$module has already been loaded") if DEBUG; | ||
| 188 | return $self->found( $name, $module ); | ||
| 189 | } | ||
| 190 | elsif (defined $found) { | ||
| 191 | $self->debug("$module has previously been requested but not found") if DEBUG; | ||
| 192 | next; | ||
| 193 | } | ||
| 194 | |||
| 195 | # Look to see if the module already has a symbol table defined | ||
| 196 | no strict REFS; | ||
| 197 | $symtab = \%{$module.PKG}; | ||
| 198 | |||
| 199 | # We have to be careful because symbols may be defined in a | ||
| 200 | # package's symbols table *before* the module is loaded (e.g. the | ||
| 201 | # $DEBUG package variable that Badger::Debug uses). So we only | ||
| 202 | # assume that the module is loaded if VERSION or BADGER_LOADED is | ||
| 203 | # defined | ||
| 204 | if ($symtab->{ VERSION } || $symtab->{ BADGER_LOADED }) { | ||
| 205 | $self->debug("found an existing VERSION/BADGER_LOADED in $module symbol table") if DEBUG; | ||
| 206 | return $self->found( $name, $module ); | ||
| 207 | } | ||
| 208 | |||
| 209 | $file = $module; | ||
| 210 | $file =~ s/::/\//g; # TODO: check Perl maps this to OS | ||
| 211 | $file .= '.pm'; | ||
| 212 | |||
| 213 | $self->debug("Attempting to load $module as $file") if DEBUG; | ||
| 214 | |||
| 215 | eval { | ||
| 216 | # We use eval so that we can "use" the module and force any | ||
| 217 | # import hooks to run. But it might be better to load the | ||
| 218 | # module with "require" and then manually call import() | ||
| 219 | require $file; | ||
| 220 | }; | ||
| 221 | |||
| 222 | if ($@) { | ||
| 223 | $self->debug("Failed to load $module: $@") if DEBUG; | ||
| 224 | # Don't confuse "Can't locate A/Module/Used/In/Your/Module.pm" | ||
| 225 | # messages with "Can't locate Your/Module.pm". The former is | ||
| 226 | # an error that should be reported, the latter isn't. We convert the | ||
| 227 | # class name to a regex that matches any non-word directory | ||
| 228 | # separators, e.g. Your::Module => Your\W+Module | ||
| 229 | my $qmfile = quotemeta($file); | ||
| 230 | $self->failed($name, $module, $@) if $@ !~ /^Can't locate $qmfile.*? in \@INC/; | ||
| 231 | next; | ||
| 232 | } | ||
| 233 | |||
| 234 | # Some filesystems are case-insensitive (like Apple's HFS), so an | ||
| 235 | # attempt to load Badger::Example::foo may succeed, when the | ||
| 236 | # correct package name is actually Badger::Example::Foo. We | ||
| 237 | # double-check by looking to see if anything extra has been added | ||
| 238 | # to the symbol table. | ||
| 239 | $self->debug("$module symbol table keys: ", join(', ', keys %$symtab)) if DEBUG; | ||
| 240 | next unless %$symtab; | ||
| 241 | |||
| 242 | $self->debug("calling $module->import") if DEBUG; | ||
| 243 | |||
| 244 | # now call the import() method to fire any import actions | ||
| 245 | $module->import; | ||
| 246 | |||
| 247 | # add the $BADGER_LOADED package variable to indicate that the | ||
| 248 | # module has been loaded and add an entry to the internal cache | ||
| 249 | ${ $module.PKG.BADGER_LOADED } ||= 1; | ||
| 250 | $loaded->{ $module } = $module; | ||
| 251 | |||
| 252 | return $self->found( $name, $module ); | ||
| 253 | } | ||
| 254 | } | ||
| 255 | |||
| 256 | # add entry to indicate module not found | ||
| 257 | $loaded->{ $name } = 0; | ||
| 258 | |||
| 259 | return $self->not_found($name); | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | sub found { | ||
| 264 | # my ($self, $name, $module) = @_; | ||
| 265 | return $_[2]; | ||
| 266 | } | ||
| 267 | |||
| 268 | |||
| 269 | sub not_found { | ||
| 270 | my $self = shift; | ||
| 271 | |||
| 272 | return $self->{ tolerant } | ||
| 273 | ? $self->decline_msg( not_found => $self->{ item } => @_ ) | ||
| 274 | : $self->error_msg( not_found => $self->{ item } => @_ ); | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | sub failed { | ||
| 279 | my $self = shift; | ||
| 280 | $self->error_msg( failed => $self->{ item }, @_ ); | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | |||
| 285 | |||
| 286 | 1; | ||
| 287 | |||
| 288 | __END__ |