| 1 | #======================================================================== | ||
| 2 | # | ||
| 3 | # Badger::Hub | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # A hub provides a central configuration and management point for | ||
| 7 | # Badger components to access other Badger components. | ||
| 8 | # | ||
| 9 | # AUTHOR | ||
| 10 | # Andy Wardley <abw@wardley.org> | ||
| 11 | # | ||
| 12 | #======================================================================== | ||
| 13 | |||
| 14 | package Badger::Hub; | ||
| 15 | |||
| 16 | use Badger::Debug ':dump'; | ||
| 17 | use Badger::Config; | ||
| 18 | use Badger::Class | ||
| 19 | version => 0.01, | ||
| 20 | debug => 0, | ||
| 21 | base => 'Badger::Prototype', | ||
| 22 | import => 'class', | ||
| 23 | auto_can => 'auto_can', | ||
| 24 | constants => 'HASH ARRAY REFS PKG', | ||
| 25 | utils => 'blessed params is_object', | ||
| 26 | words => 'COMPONENTS DELEGATES COMP_CACHE DELG_CACHE', | ||
| 27 | constant => { | ||
| 28 | CONFIG_MODULE => 'Badger::Config', | ||
| 29 | }, | ||
| 30 | messages => { | ||
| 31 | no_module => 'No %s module defined.', | ||
| 32 | bad_method => "Invalid method '%s' called on %s at %s line %s", | ||
| 33 | }; | ||
| 34 | |||
| 35 | our $COMPONENTS = { }; | ||
| 36 | our $RESOURCES = { }; | ||
| 37 | our $DELEGATES = { }; | ||
| 38 | our $LOADED = { }; | ||
| 39 | #our $COLLECTIONS = [qw( components resources delegates )]; | ||
| 40 | |||
| 41 | |||
| 42 | sub init { | ||
| 43 | my ($self, $config) = @_; | ||
| 44 | $self->init_hub($config); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub init_hub { | ||
| 48 | my ($self, $config) = @_; | ||
| 49 | $self->init_config($config); | ||
| 50 | $self->init_items($config); | ||
| 51 | # $self->init_collections($config); # init on demand | ||
| 52 | } | ||
| 53 | |||
| 54 | sub init_config { | ||
| 55 | my ($self, $args) = @_; | ||
| 56 | my $class = $self->class; | ||
| 57 | |||
| 58 | # We're looking for a specific 'config' item which the user can provide to | ||
| 59 | # points to a master configuration object or class name. We default to the | ||
| 60 | # value in the $CONFIG package variable, which in this case is Badger::Config, | ||
| 61 | # but could be re-defined by a subclass to be something else. | ||
| 62 | |||
| 63 | my $config = delete($args->{ config }) | ||
| 64 | || $self->class->any_var('CONFIG') | ||
| 65 | || { }; | ||
| 66 | |||
| 67 | my $module = delete($args->{ config_module }) | ||
| 68 | || $class->any_var('CONFIG_MODULE') | ||
| 69 | || $self->CONFIG_MODULE; | ||
| 70 | |||
| 71 | if (is_object($module, $config)) { | ||
| 72 | # $config is already a config module object of the right heritage | ||
| 73 | $self->debug("config is already a $module object") if DEBUG; | ||
| 74 | } | ||
| 75 | else { | ||
| 76 | if (ref $config eq HASH) { | ||
| 77 | # cool | ||
| 78 | } | ||
| 79 | elsif (! ref $config) { | ||
| 80 | # $config is a module name | ||
| 81 | $module = $config; | ||
| 82 | $config = { }; | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | return $self->error_msg( invalid => config => $config ); | ||
| 86 | } | ||
| 87 | $self->debug("loading and instantiating a custom $module config object") if DEBUG; | ||
| 88 | class($module)->load; | ||
| 89 | $config = $module->new($config); | ||
| 90 | } | ||
| 91 | |||
| 92 | $self->{ config } = $config; | ||
| 93 | $self->debug("hub config: $self->{ config }\n") if DEBUG; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub init_items { | ||
| 97 | my ($self, $args) = @_; | ||
| 98 | |||
| 99 | # merge all values in $CONFIG_ITEMS in with $args->{ items }; | ||
| 100 | $args->{ items } = $self->class->list_vars( | ||
| 101 | CONFIG_ITEMS => delete($args->{ config_items }), $args->{ items } | ||
| 102 | ); | ||
| 103 | |||
| 104 | $self->debug( | ||
| 105 | "hub config items: ", | ||
| 106 | $self->dump_data($args->{ items }) | ||
| 107 | ) if DEBUG; | ||
| 108 | |||
| 109 | return $self; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub init_collections { | ||
| 113 | my ($self, $config) = @_; | ||
| 114 | $self->init_components($config); | ||
| 115 | $self->init_resource($config); | ||
| 116 | $self->init_delegates($config); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub init_components { | ||
| 120 | shift->init_collection( components => @_ ); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub init_resources { | ||
| 124 | shift->init_collection( resources => @_ ); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub init_delegates { | ||
| 128 | shift->init_collection( delegates => @_ ); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub init_collection { | ||
| 132 | my ($self, $type) = @_; | ||
| 133 | my $TYPE = uc $type; | ||
| 134 | my $cfg = $self->config($type); | ||
| 135 | my $bits = $self->{ $type } = $self->class->hash_vars( | ||
| 136 | $TYPE => $self->config($type) | ||
| 137 | ); | ||
| 138 | $self->debug("init_collection: $type: ", $self->dump_data($bits)) if DEBUG; | ||
| 139 | return $bits; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub configure { | ||
| 143 | my ($self, $config) = self_params(@_); | ||
| 144 | my $item; | ||
| 145 | |||
| 146 | if ($item = delete $config->{ components }) { | ||
| 147 | $self->components($item); | ||
| 148 | } | ||
| 149 | if ($item = delete $config->{ resources }) { | ||
| 150 | $self->resources($item); | ||
| 151 | } | ||
| 152 | if ($item = delete $config->{ delegates }) { | ||
| 153 | $self->delegates($item); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | #----------------------------------------------------------------------------- | ||
| 158 | # comment | ||
| 159 | #----------------------------------------------------------------------------- | ||
| 160 | |||
| 161 | sub components { | ||
| 162 | my $self = shift->prototype; | ||
| 163 | my $comps = $self->{ components } | ||
| 164 | || $self->init_components; | ||
| 165 | |||
| 166 | if (@_) { | ||
| 167 | my $args = ref $_[0] eq HASH ? shift : { @_ }; | ||
| 168 | @$comps{ keys %$args } = values %$args; | ||
| 169 | } | ||
| 170 | |||
| 171 | return $comps; | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | sub component { | ||
| 176 | my $self = shift; | ||
| 177 | my $comps = $self->components; | ||
| 178 | $self->debug("components: ", $self->dump_data($comps)) if DEBUG; | ||
| 179 | return @_ | ||
| 180 | ? $comps->{ $_[0] } | ||
| 181 | : $comps; | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | sub delegates { | ||
| 186 | my $self = shift->prototype; | ||
| 187 | my $class = $self->class; | ||
| 188 | my $delgs = $self->{ delegates } | ||
| 189 | || $self->init_delegates; | ||
| 190 | |||
| 191 | if (@_) { | ||
| 192 | my $args = ref $_[0] eq HASH ? shift : { @_ }; | ||
| 193 | @$delgs{ keys %$args } = values %$args; | ||
| 194 | } | ||
| 195 | |||
| 196 | return $delgs; | ||
| 197 | } | ||
| 198 | |||
| 199 | |||
| 200 | sub delegate { | ||
| 201 | my $self = shift; | ||
| 202 | my $delegs = $self->delegates; | ||
| 203 | $self->debug("delegates: ", $self->dump_data($delegs)) if DEBUG; | ||
| 204 | return @_ | ||
| 205 | ? $delegs->{ $_[0] } | ||
| 206 | : $delegs; | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | sub auto_can { | ||
| 211 | my ($self, $name) = @_; | ||
| 212 | my $target; | ||
| 213 | |||
| 214 | if ($target = $self->component($name)) { | ||
| 215 | $self->debug("creating component method in $self for $name") if DEBUG; | ||
| 216 | return $self->auto_component( $name => $target ); | ||
| 217 | } | ||
| 218 | elsif ($target = $self->delegate($name)) { | ||
| 219 | $self->debug("creating delegate method in $self for $name") if DEBUG; | ||
| 220 | return $self->auto_delegate( $name => $target ); | ||
| 221 | } | ||
| 222 | elsif (DEBUG) { | ||
| 223 | $self->debug("no component or delegate found in $self for $name"); | ||
| 224 | } | ||
| 225 | |||
| 226 | return undef; | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | sub auto_component { | ||
| 231 | my ($self, $name, $comp) = @_; | ||
| 232 | my $class = ref $self || $self; | ||
| 233 | |||
| 234 | $LOADED->{ $name } ||= class($comp)->load; | ||
| 235 | |||
| 236 | return sub { | ||
| 237 | my $self = shift; | ||
| 238 | my $args = @_ && ref $_[0] eq HASH ? shift : { @_ }; | ||
| 239 | $self = $self->prototype unless ref $self; | ||
| 240 | |||
| 241 | return $self->{ $name } | ||
| 242 | ||= $self->construct( | ||
| 243 | $name => { | ||
| 244 | # TODO: figure out what's going on here in terms of | ||
| 245 | # possible combinations of configuration options | ||
| 246 | %$args, | ||
| 247 | hub => $self, | ||
| 248 | module => $comp | ||
| 249 | } | ||
| 250 | ); | ||
| 251 | } | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | sub auto_delegate { | ||
| 256 | my ($self, $name, $deleg) = @_; | ||
| 257 | my $class = ref $self || $self; | ||
| 258 | |||
| 259 | # foo => bar is mapped to $self->bar->foo | ||
| 260 | # foo => [bar, baz] is mapped to $self->bar->baz | ||
| 261 | my ($m1, $m2) = ref $deleg eq ARRAY ? @$deleg : ($deleg, $name); | ||
| 262 | |||
| 263 | return $self->error("Cannot auto_delegate() a method to itself: $m1 -> $m2") | ||
| 264 | if $m1 eq $m2; | ||
| 265 | |||
| 266 | return sub { | ||
| 267 | shift->$m1->$m2(@_); | ||
| 268 | }; | ||
| 269 | } | ||
| 270 | |||
| 271 | |||
| 272 | |||
| 273 | # Configure and create a sub-component identified by $name, using | ||
| 274 | # any configuration items in $params and any values defined locally | ||
| 275 | # in a configuration hash, object or class. | ||
| 276 | # | ||
| 277 | # The $self->{ config } can contain a hash array of configuration | ||
| 278 | # items, or it can be a Badger::Config object or the class name of a | ||
| 279 | # Badger::Config object. We look in the hash, or call the object/class | ||
| 280 | # method to find $name (e.g. $hash->{ $name }, $object->$name, or | ||
| 281 | # $class->name()). This is merged with $params. | ||
| 282 | |||
| 283 | sub construct { | ||
| 284 | my $self = shift; | ||
| 285 | my $name = shift; | ||
| 286 | my $config = $self->config($name, @_); | ||
| 287 | |||
| 288 | $self->debug("$name module config: ", $self->dump_data($config)) if DEBUG; | ||
| 289 | |||
| 290 | # see if a module name is specified in $args, config hash or use $pkgmod | ||
| 291 | my $module = $config->{ module } | ||
| 292 | || return $self->error_msg( no_module => $name ); | ||
| 293 | |||
| 294 | # load the module | ||
| 295 | class($module)->load; | ||
| 296 | |||
| 297 | return $module->new($config); | ||
| 298 | } | ||
| 299 | |||
| 300 | sub config { | ||
| 301 | my $self = shift->prototype; | ||
| 302 | my $config = $self->{ config }; return $config unless @_; | ||
| 303 | my $name = shift; | ||
| 304 | my $params = params(@_); | ||
| 305 | my $defaults; | ||
| 306 | my $method; | ||
| 307 | |||
| 308 | |||
| 309 | if ($config && ref $config eq HASH) { | ||
| 310 | # $self->{ config } can be a hash ref with a $name item | ||
| 311 | $defaults = $config->{ $name }; | ||
| 312 | } | ||
| 313 | elsif (blessed $config && ($method = $config->can('get'))) { | ||
| 314 | # $self->{ config } can be an object with a get($name) method which we call | ||
| 315 | $self->debug("calling config->get($name)") if DEBUG; | ||
| 316 | $defaults = $method->($config, $name); | ||
| 317 | } | ||
| 318 | elsif (blessed $config && ($method = $config->can($name))) { | ||
| 319 | # $self->{ config } can be an object with a $name method which we call | ||
| 320 | $self->debug("calling config->$name") if DEBUG; | ||
| 321 | $defaults = $method->($config); | ||
| 322 | } | ||
| 323 | # no defaults | ||
| 324 | return $self->no_config($name, $params) | ||
| 325 | unless $defaults; | ||
| 326 | |||
| 327 | $self->debug( | ||
| 328 | "config for $name: DEFAULTS: ", | ||
| 329 | $self->dump_data($defaults), | ||
| 330 | "\nPARAMS: ", $self->dump_data($params) | ||
| 331 | ) if DEBUG; | ||
| 332 | |||
| 333 | return { | ||
| 334 | %$defaults, | ||
| 335 | %$params | ||
| 336 | }; | ||
| 337 | } | ||
| 338 | |||
| 339 | sub no_config { | ||
| 340 | my ($self, $name, $params) = @_; | ||
| 341 | |||
| 342 | $self->debug( | ||
| 343 | "no_config for $name: ", | ||
| 344 | "PARAMS: ", $self->dump_data($params) | ||
| 345 | ) if DEBUG; | ||
| 346 | |||
| 347 | # TODO: option to make this a failure | ||
| 348 | return $self->pkgvar_config($name, $params) | ||
| 349 | || ($params ? { %$params } : { }); | ||
| 350 | } | ||
| 351 | |||
| 352 | sub pkgvar_config { | ||
| 353 | my ($self, $name, $params) = @_; | ||
| 354 | my $pkgvar = $self->class->any_var(uc $name) || return $params; | ||
| 355 | my $config; | ||
| 356 | |||
| 357 | if (ref $pkgvar eq HASH) { | ||
| 358 | # package variable can be a hash ref of config options | ||
| 359 | $config = $pkgvar; | ||
| 360 | } | ||
| 361 | else { | ||
| 362 | # or the name of a module | ||
| 363 | $config = { | ||
| 364 | module => $pkgvar | ||
| 365 | }; | ||
| 366 | }; | ||
| 367 | |||
| 368 | return { | ||
| 369 | %$config, | ||
| 370 | %$params | ||
| 371 | }; | ||
| 372 | } | ||
| 373 | |||
| 374 | |||
| 375 | |||
| 376 | #------------------------------------------------------------------------ | ||
| 377 | # destroy() | ||
| 378 | # | ||
| 379 | # Destroy the hub and cleanup any cache items we may have stored. | ||
| 380 | #------------------------------------------------------------------------ | ||
| 381 | |||
| 382 | sub destroy { | ||
| 383 | my $self = shift; | ||
| 384 | |||
| 385 | # if called as a class method we cleanup any prototype object | ||
| 386 | # stored as a singleton in the $PROTOTYPE package variable | ||
| 387 | unless (ref $self) { | ||
| 388 | no strict 'refs'; | ||
| 389 | my $class = $self; | ||
| 390 | $self = ${"$class\::PROTOTYPE"} || return; | ||
| 391 | $self->debug("deleting hub prototype from \$$class\::PROTOTYPE\n") if DEBUG; | ||
| 392 | ${"$class\::PROTOTYPE"} = undef; | ||
| 393 | } | ||
| 394 | |||
| 395 | $self->debug("destroying hub: $self\n") if DEBUG; | ||
| 396 | |||
| 397 | # empty content of $self to break any circular references that | ||
| 398 | # we may have established with other items that point back to us | ||
| 399 | %$self = (); | ||
| 400 | } | ||
| 401 | |||
| 402 | |||
| 403 | sub DESTROY { | ||
| 404 | my $self = shift; | ||
| 405 | $self->destroy; | ||
| 406 | } | ||
| 407 | |||
| 408 | |||
| 409 | 1; | ||
| 410 | __END__ |