| 1 | package Class::Component; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | our $VERSION = '0.17'; | ||
| 6 | |||
| 7 | for my $method (qw/ load_components load_plugins new register_method register_hook remove_method remove_hook call run_hook NEXT /) { | ||
| 8 | no strict 'refs'; | ||
| 9 | *{__PACKAGE__."::$method"} = sub { Class::Component::Implement->$method(@_) }; | ||
| 10 | } | ||
| 11 | |||
| 12 | for my $name (qw/ config components plugins methods hooks /) { | ||
| 13 | my $method = "class_component_$name"; | ||
| 14 | no strict 'refs'; | ||
| 15 | *{__PACKAGE__."::$method"} = sub { | ||
| 16 | $_[0]->{"_$method"} = $_[1] if $_[1]; | ||
| 17 | $_[0]->{"_$method"} | ||
| 18 | }; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub import { | ||
| 22 | my($class, %args) = @_; | ||
| 23 | return unless $class eq 'Class::Component'; | ||
| 24 | my $pkg = caller(0); | ||
| 25 | |||
| 26 | unless ($pkg->isa('Class::Component')) { | ||
| 27 | no strict 'refs'; | ||
| 28 | unshift @{"$pkg\::ISA"}, $class; | ||
| 29 | } | ||
| 30 | |||
| 31 | Class::Component::Implement->init($pkg, %args); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub class_component_load_component_resolver {} | ||
| 35 | sub class_component_load_plugin_resolver {} | ||
| 36 | |||
| 37 | sub class_component_reinitialize { | ||
| 38 | my($class, %args) = @_; | ||
| 39 | Class::Component::Implement->init($class, %args); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub class_component_clear_isa_list { | ||
| 43 | my $class = shift; | ||
| 44 | |||
| 45 | my $klass = $_[0] || ref($class) || $class; | ||
| 46 | my $isa_list = Class::Component::Implement->component_isa_list; | ||
| 47 | for my $key (keys %{ $isa_list }) { | ||
| 48 | delete $isa_list->{$key} if $key =~ /^$klass-/ || $key eq $klass; | ||
| 49 | } | ||
| 50 | |||
| 51 | my $pkg_require_cache = Class::Component::Implement->pkg_require_cache; | ||
| 52 | for my $key (keys %{ $pkg_require_cache }) { | ||
| 53 | delete $pkg_require_cache->{$key} if $key =~ /^$klass\::/ || $key eq $klass; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | package # hide from PAUSE | ||
| 58 | Class::Component::Implement; | ||
| 59 | |||
| 60 | use strict; | ||
| 61 | use warnings; | ||
| 62 | use base qw( Class::Data::Inheritable ); | ||
| 63 | |||
| 64 | my $component_isa_list = {}; | ||
| 65 | my $default_components = {}; | ||
| 66 | my $default_plugins = {}; | ||
| 67 | my $default_configs = {}; | ||
| 68 | my $reload_plugin_maps = {}; | ||
| 69 | |||
| 70 | use UNIVERSAL::require; | ||
| 71 | |||
| 72 | use Carp::Clan qw/Class::Component/; | ||
| 73 | use Class::Inspector; | ||
| 74 | |||
| 75 | sub component_isa_list { $component_isa_list } | ||
| 76 | sub default_components { $default_components } | ||
| 77 | sub default_plugins { $default_plugins } | ||
| 78 | sub default_configs { $default_configs } | ||
| 79 | |||
| 80 | sub init { | ||
| 81 | my($class, $c, %args) = @_; | ||
| 82 | $c = $class->_class($c); | ||
| 83 | |||
| 84 | $default_components->{$c} ||= []; | ||
| 85 | $default_plugins->{$c} ||= []; | ||
| 86 | $default_configs->{$c} = delete $args{config} if defined $args{config}; | ||
| 87 | |||
| 88 | delete $reload_plugin_maps->{$c}; | ||
| 89 | $reload_plugin_maps->{$c} = \&_reload_plugin if $args{reload_plugin}; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub shared_configs { | ||
| 93 | my($class, $from, $to) = @_; | ||
| 94 | |||
| 95 | $default_components->{$to} = $default_components->{$from}; | ||
| 96 | $default_plugins->{$to} = $default_plugins->{$from}; | ||
| 97 | $reload_plugin_maps->{$to} = $reload_plugin_maps->{$from}; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub load_components { | ||
| 101 | my($class, $c, @components) = @_; | ||
| 102 | |||
| 103 | for my $component (@components) { | ||
| 104 | $class->_load_component($c, $component); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | sub _load_component { | ||
| 109 | my($class, $c, $component, $reload) = @_; | ||
| 110 | $c = $class->_class($c); | ||
| 111 | |||
| 112 | my $pkg; | ||
| 113 | if (($pkg = $component) =~ s/^\+// || ($pkg = $c->class_component_load_component_resolver($component))) { | ||
| 114 | $pkg->require or croak $@; | ||
| 115 | } else { | ||
| 116 | unless ($pkg = $class->pkg_require($c => "Component::$component")) { | ||
| 117 | $@ and croak $@; | ||
| 118 | croak "$component is not installed"; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | unless ($reload) { | ||
| 123 | for my $default (@{ $default_components->{$c} }) { | ||
| 124 | return if $pkg eq $default; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | no strict 'refs'; | ||
| 129 | unshift @{"$c\::ISA"}, $pkg; | ||
| 130 | for my $isa_pkg (@{ $class->isa_list($c) }) { | ||
| 131 | my $key = $c; | ||
| 132 | my $from; | ||
| 133 | unless ($c eq $isa_pkg) { | ||
| 134 | $key .= "-$isa_pkg"; | ||
| 135 | $from = $isa_pkg; | ||
| 136 | } | ||
| 137 | $class->component_isa_list->{$key} = $class->isa_list($c, $from); | ||
| 138 | } | ||
| 139 | push @{ $default_components->{$c} }, $pkg unless $reload; | ||
| 140 | $pkg->class_component_load_component_init($c) if $pkg->can('class_component_load_component_init'); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub load_plugins { | ||
| 144 | my($class, $c, @plugins) = @_; | ||
| 145 | |||
| 146 | return $class->load_plugins_default($c, @plugins) unless ref $c; | ||
| 147 | |||
| 148 | for my $plugin (@plugins) { | ||
| 149 | $class->_load_plugin($c, $plugin); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | sub load_plugins_default { | ||
| 154 | my($class, $c, @plugins) = @_; | ||
| 155 | |||
| 156 | LOOP: | ||
| 157 | for my $plugin (@plugins) { | ||
| 158 | for my $default (@{ $default_plugins->{$c} }) { | ||
| 159 | next LOOP if $plugin eq $default; | ||
| 160 | } | ||
| 161 | push @{ $default_plugins->{$c} }, $plugin; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | sub _load_plugin { | ||
| 166 | my($class, $c, $plugin) = @_; | ||
| 167 | |||
| 168 | # config option support | ||
| 169 | my $config; | ||
| 170 | if (ref($plugin) eq 'HASH') { | ||
| 171 | $config = $plugin->{config} || {}; | ||
| 172 | $plugin = $plugin->{module}; | ||
| 173 | } | ||
| 174 | return unless $plugin; | ||
| 175 | |||
| 176 | my $pkg; | ||
| 177 | if (($pkg = $plugin) =~ s/^\+// || ($pkg = $c->class_component_load_plugin_resolver($plugin))) { | ||
| 178 | $pkg->require or croak $@; | ||
| 179 | } else { | ||
| 180 | unless ($pkg = $class->pkg_require($c => "Plugin::$plugin")) { | ||
| 181 | $@ and croak $@; | ||
| 182 | croak "$plugin is not installed"; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | my $class_component_plugins = $c->class_component_plugins; | ||
| 187 | unless ($config) { | ||
| 188 | for my $default (@{ $class_component_plugins }) { | ||
| 189 | return if $pkg eq ref($default); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | my $obj = $pkg->new($config || $c->class_component_config->{$plugin} || {}, $c); | ||
| 194 | push @{ $class_component_plugins }, $obj; | ||
| 195 | $obj->register($c); | ||
| 196 | } | ||
| 197 | |||
| 198 | sub new { | ||
| 199 | my($class, $c, $args) = @_; | ||
| 200 | $args ||= {}; | ||
| 201 | |||
| 202 | my $self = bless { | ||
| 203 | %{ $args }, | ||
| 204 | _class_component_plugins => [], | ||
| 205 | _class_component_components => $default_components->{$c}, | ||
| 206 | _class_component_methods => {}, | ||
| 207 | _class_component_hooks => {}, | ||
| 208 | _class_component_config => $args->{config} || $default_configs->{$c} || {}, | ||
| 209 | _class_component_default_plugins => $default_plugins->{$c}, | ||
| 210 | }, $c; | ||
| 211 | |||
| 212 | $self->load_plugins(@{ $default_plugins->{$c} }, @{ $args->{load_plugins} || [] }); | ||
| 213 | |||
| 214 | $self; | ||
| 215 | } | ||
| 216 | |||
| 217 | sub register_method { | ||
| 218 | my($class, $c, @methods) = @_; | ||
| 219 | while (my($method, $plugin) = splice @methods, 0, 2) { | ||
| 220 | $c->class_component_methods->{$method} = $plugin | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | sub register_hook { | ||
| 225 | my($class, $c, @hooks) = @_; | ||
| 226 | while (my($hook, $obj) = splice @hooks, 0, 3) { | ||
| 227 | $c->class_component_hooks->{$hook} = [] unless $c->class_component_hooks->{$hook}; | ||
| 228 | push @{ $c->class_component_hooks->{$hook} }, $obj; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | sub remove_method { | ||
| 233 | my($class, $c, @methods) = @_; | ||
| 234 | while (my($method, $plugin) = splice @methods, 0, 2) { | ||
| 235 | next unless ref($c->class_component_methods->{$method}) eq $plugin; | ||
| 236 | delete $c->class_component_methods->{$method}; | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | sub remove_hook { | ||
| 241 | my($class, $c, @hooks) = @_; | ||
| 242 | while (my($hook, $remove_obj) = splice @hooks, 0, 3) { | ||
| 243 | my $i = -1; | ||
| 244 | for my $obj (@{ $c->class_component_hooks->{$hook} }) { | ||
| 245 | $i++; | ||
| 246 | next unless ref($obj->{plugin}) eq $remove_obj->{plugin} && $obj->{method} eq $remove_obj->{method}; | ||
| 247 | splice @{ $c->class_component_hooks->{$hook} }, $i, 1; | ||
| 248 | } | ||
| 249 | delete $c->class_component_hooks->{$hook} unless @{ $c->class_component_hooks->{$hook} }; | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | sub call { | ||
| 254 | my($class, $c, $method, @args) = @_; | ||
| 255 | return unless my $plugin = $c->class_component_methods->{$method}; | ||
| 256 | if (ref $plugin eq 'HASH') { | ||
| 257 | # extend method | ||
| 258 | my $obj = $plugin; | ||
| 259 | $plugin = $obj->{plugin}; | ||
| 260 | my $real_method = $obj->{method}; | ||
| 261 | return unless $plugin && $real_method; | ||
| 262 | $class->reload_plugin($c, $plugin); | ||
| 263 | if (ref $real_method eq 'CODE') { | ||
| 264 | $real_method->($plugin, $c, @args); | ||
| 265 | } elsif (!ref($real_method)) { | ||
| 266 | $plugin->$real_method($c, @args); | ||
| 267 | } | ||
| 268 | } else { | ||
| 269 | $class->reload_plugin($c, $plugin); | ||
| 270 | $plugin->$method($c, @args); | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | sub run_hook { | ||
| 275 | my($class, $c, $hook, $args) = @_; | ||
| 276 | return unless my $hooks = $c->class_component_hooks->{$hook}; | ||
| 277 | $class->reload_plugin($c, $hooks->[0]->{plugin}); | ||
| 278 | |||
| 279 | my @ret; | ||
| 280 | for my $obj (@{ $hooks }) { | ||
| 281 | my($plugin, $method) = ($obj->{plugin}, $obj->{method}); | ||
| 282 | my $ret = $plugin->$method($c, $args); | ||
| 283 | push @ret, $ret; | ||
| 284 | } | ||
| 285 | \@ret; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub _reload_plugin { | ||
| 289 | my($class, $c, $pkg) = @_; | ||
| 290 | return if Class::Inspector->loaded($class->_class($pkg)); | ||
| 291 | |||
| 292 | $default_components->{$class->_class($c)} = $c->class_component_components; | ||
| 293 | $default_plugins->{$class->_class($c)} = $c->class_component_plugins; | ||
| 294 | |||
| 295 | for my $component (@{ $default_components->{$class->_class($c)} }) { | ||
| 296 | $class->_load_component($c, '+' . $class->_class($component), 1); | ||
| 297 | } | ||
| 298 | |||
| 299 | for my $plugin (@{ $c->class_component_plugins }) { | ||
| 300 | $class->_load_plugin($c, '+' . $class->_class($plugin)); | ||
| 301 | } | ||
| 302 | |||
| 303 | } | ||
| 304 | |||
| 305 | sub reload_plugin { | ||
| 306 | my($class, $c) = @_; | ||
| 307 | return unless my $code = $reload_plugin_maps->{$class->_class($c)}; | ||
| 308 | goto $code; | ||
| 309 | } | ||
| 310 | |||
| 311 | sub NEXT { | ||
| 312 | my($class, $c, $method, @args) = @_; | ||
| 313 | my $klass = ref $c || $c; | ||
| 314 | my $caller = caller(1); | ||
| 315 | |||
| 316 | my $isa_list_cache = $component_isa_list->{"$klass-$caller"} || $class->isa_list_cache($c, $caller); | ||
| 317 | my @isa = @{ $isa_list_cache }; | ||
| 318 | |||
| 319 | for my $pkg (@isa) { | ||
| 320 | next unless $pkg->can($method);; | ||
| 321 | my $next = "$pkg\::$method"; | ||
| 322 | return $c->$next(@args); | ||
| 323 | } | ||
| 324 | |||
| 325 | for my $pkg (@isa) { | ||
| 326 | next unless $pkg->can('AUTOLOAD'); | ||
| 327 | my $next = "$pkg\::$method"; | ||
| 328 | return $c->$next(@args); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | sub isa_list_cache { | ||
| 333 | my($class, $c, $from) = @_; | ||
| 334 | my $key = ref $c || $c; | ||
| 335 | $key .= "-$from" if $from; | ||
| 336 | $component_isa_list->{$key} = $class->isa_list($c, $from) unless $component_isa_list->{$key}; | ||
| 337 | $component_isa_list->{$key}; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub isa_list { | ||
| 341 | my($class, $c, $from) = @_; | ||
| 342 | $c = ref $c || $c; | ||
| 343 | |||
| 344 | my $isa_list = $class->_fetch_isa_list($c); | ||
| 345 | my $isa_mark = {}; | ||
| 346 | $class->_mark_isa_list($isa_list, $isa_mark, 0); | ||
| 347 | my @isa = $class->_sort_isa_list($isa_list, $isa_mark, 0); | ||
| 348 | |||
| 349 | my @next_classes; | ||
| 350 | my $f = 0; | ||
| 351 | $f = 1 unless $from; | ||
| 352 | for my $pkg (@isa) { | ||
| 353 | if ($f) { | ||
| 354 | push @next_classes, $pkg; | ||
| 355 | } else { | ||
| 356 | next unless $pkg eq $from; | ||
| 357 | $f = 1; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | \@next_classes; | ||
| 361 | } | ||
| 362 | |||
| 363 | sub _fetch_isa_list { | ||
| 364 | my($class, $base) = @_; | ||
| 365 | |||
| 366 | my $isa_list = { pkg => $base, isa => [] }; | ||
| 367 | no strict 'refs'; | ||
| 368 | for my $pkg (@{"$base\::ISA"}) { | ||
| 369 | push @{ $isa_list->{isa} }, $class->_fetch_isa_list($pkg); | ||
| 370 | } | ||
| 371 | $isa_list; | ||
| 372 | } | ||
| 373 | |||
| 374 | sub _mark_isa_list { | ||
| 375 | my($class, $isa_list, $isa_mark, $nest) = @_; | ||
| 376 | |||
| 377 | for my $list (@{ $isa_list->{isa} }) { | ||
| 378 | $class->_mark_isa_list($list, $isa_mark, $nest + 1); | ||
| 379 | } | ||
| 380 | my $pkg = $isa_list->{pkg}; | ||
| 381 | $isa_mark->{$pkg} = { nest => $nest, count => 0 } if !$isa_mark->{$pkg} || $isa_mark->{$pkg}->{nest} < $nest; | ||
| 382 | $isa_mark->{$pkg}->{count}++; | ||
| 383 | } | ||
| 384 | |||
| 385 | sub _sort_isa_list { | ||
| 386 | my($class, $isa_list, $isa_mark, $nest) = @_; | ||
| 387 | |||
| 388 | my @isa; | ||
| 389 | my $pkg = $isa_list->{pkg}; | ||
| 390 | unless (--$isa_mark->{$pkg}->{count}) { | ||
| 391 | push @isa, $pkg; | ||
| 392 | } | ||
| 393 | |||
| 394 | for my $list (@{ $isa_list->{isa} }) { | ||
| 395 | my @ret = $class->_sort_isa_list($list, $isa_mark, $nest + 1); | ||
| 396 | push @isa, @ret; | ||
| 397 | } | ||
| 398 | |||
| 399 | @isa; | ||
| 400 | } | ||
| 401 | |||
| 402 | sub _class { | ||
| 403 | my($class, $c) = @_; | ||
| 404 | ref($c) || $c; | ||
| 405 | } | ||
| 406 | |||
| 407 | my $pkg_require_cache = {}; | ||
| 408 | sub pkg_require_cache { $pkg_require_cache } | ||
| 409 | sub pkg_require_cache_clear { $pkg_require_cache = {} } | ||
| 410 | sub pkg_require { | ||
| 411 | my($class, $c, $pkg) = @_; | ||
| 412 | $c = ref $c || $c; | ||
| 413 | |||
| 414 | my $isa_list; | ||
| 415 | if ($isa_list = $component_isa_list->{$c}) { | ||
| 416 | if (my $cache = $pkg_require_cache->{$pkg}) { | ||
| 417 | if ($cache->{isa_list} eq join('-', @{ $isa_list })) { | ||
| 418 | return $cache->{pkg}; | ||
| 419 | } | ||
| 420 | } | ||
| 421 | } | ||
| 422 | $isa_list ||= []; | ||
| 423 | |||
| 424 | my $obj = { isa_list => join('-', @{ $isa_list }) }; | ||
| 425 | $pkg_require_cache->{$pkg} = $obj; | ||
| 426 | for my $isa_pkg (@{ $class->isa_list_cache($c) }) { | ||
| 427 | unless ($isa_list) { | ||
| 428 | $isa_list = $component_isa_list->{$c}; | ||
| 429 | $obj->{isa_list} = join('-', @{ $isa_list }); | ||
| 430 | } | ||
| 431 | |||
| 432 | my $new_pkg = "$isa_pkg\::$pkg"; | ||
| 433 | next unless Class::Inspector->installed($new_pkg); | ||
| 434 | $new_pkg->require or return; | ||
| 435 | $obj->{pkg} = $new_pkg; | ||
| 436 | return $new_pkg; | ||
| 437 | } | ||
| 438 | } | ||
| 439 | |||
| 440 | package Class::Component; | ||
| 441 | |||
| 442 | 1; | ||
| 443 | __END__ |