| 1 | package Class::Component::Component::Plaggerize; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp (); | ||
| 5 | use UNIVERSAL::require; | ||
| 6 | |||
| 7 | sub import { | ||
| 8 | my($class, %args) = @_; | ||
| 9 | |||
| 10 | my $config = delete $args{plaggerize} || {}; | ||
| 11 | $config->{plugins} ||= [qw/ PluginLoader Log ConfigLoader /]; | ||
| 12 | |||
| 13 | my @plugins; | ||
| 14 | for my $plugin (@{ $config->{plugins} }) { | ||
| 15 | my $module = $plugin =~ s/^\+// ? $plugin : "Class::Component::Component::Plaggerize::$plugin"; | ||
| 16 | $module->require or Carp::croak("Loading $module: $@"); | ||
| 17 | push @plugins, $module; | ||
| 18 | } | ||
| 19 | if (@plugins) { | ||
| 20 | no strict 'refs'; | ||
| 21 | my @isa ; | ||
| 22 | for my $pkg (@{"$class\::ISA"}) { | ||
| 23 | if ($pkg->isa(__PACKAGE__)) { | ||
| 24 | push @isa, $pkg, @plugins; | ||
| 25 | } else { | ||
| 26 | push @isa, $pkg; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | @{"$class\::ISA"} = @isa; | ||
| 30 | |||
| 31 | $class->class_component_clear_isa_list; | ||
| 32 | } | ||
| 33 | $class->NEXT( import => %args ); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub new { | ||
| 37 | my($class, $args) = @_; | ||
| 38 | |||
| 39 | my $config = delete $args->{config} || {}; | ||
| 40 | $args->{config} = {}; | ||
| 41 | my $self = $class->NEXT( new => $args ); | ||
| 42 | |||
| 43 | $self->conf( $class->setup_config($config) ); | ||
| 44 | $self->setup_plugins; | ||
| 45 | |||
| 46 | $self; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub conf { | ||
| 50 | my $self = shift; | ||
| 51 | $_[0] ? ( $self->{conf} = $_[0]) : $self->{conf}; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub setup_config { shift->NEXT( setup_config => @_ ) } | ||
| 55 | sub setup_plugins { shift->NEXT( setup_plugins => @_ ) } | ||
| 56 | |||
| 57 | sub log { shift->NEXT( log => @_ ) } | ||
| 58 | sub should_log { shift->NEXT( should_log => @_ ) } | ||
| 59 | |||
| 60 | 1; | ||
| 61 | |||
| 62 | __END__ |