| 1 | package Class::Component::Plugin; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use base qw( Class::Accessor::Fast Class::Data::Inheritable ); | ||
| 6 | |||
| 7 | __PACKAGE__->mk_accessors(qw/ config /); | ||
| 8 | __PACKAGE__->mk_classdata( '__attr_cache' => {} ); | ||
| 9 | __PACKAGE__->mk_classdata( '__methods_cache' ); | ||
| 10 | |||
| 11 | use Carp::Clan qw/Class::Component/; | ||
| 12 | use Class::Inspector; | ||
| 13 | use UNIVERSAL::require; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my($class, $config, $c) = @_; | ||
| 17 | my $self = bless {}, $class; | ||
| 18 | $self->config($config); | ||
| 19 | $self->init($c); | ||
| 20 | $self; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub init {} | ||
| 24 | |||
| 25 | my %attribute_detect_cache = (); | ||
| 26 | sub class_component_plugin_attribute_detect_cache_enable { 1 }; | ||
| 27 | sub class_component_plugin_attribute_detect { | ||
| 28 | my($self, $attr, $cache_key) = @_; | ||
| 29 | $attribute_detect_cache{$cache_key} = []; | ||
| 30 | |||
| 31 | return unless my($key, $value) = ($attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/); | ||
| 32 | unless (defined $value) { | ||
| 33 | $attribute_detect_cache{$cache_key} = [$key, $value]; | ||
| 34 | return ($key, $value); | ||
| 35 | } | ||
| 36 | |||
| 37 | my $pkg = ref $self; | ||
| 38 | # from Attribute::Handlers | ||
| 39 | my $evaled = eval "package $pkg; no warnings; local \$SIG{__WARN__} = sub{ die \@_ }; [$value]"; ## no critic | ||
| 40 | $@ and croak "$pkg: $value: $@"; | ||
| 41 | my $data = $evaled || [$value]; | ||
| 42 | $value = (@{ $data } > 1) ? $data : $data->[0]; | ||
| 43 | |||
| 44 | $attribute_detect_cache{$cache_key} = [$key, $value]; | ||
| 45 | return ($key, $value); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub class_component_load_attribute_resolver { } | ||
| 49 | |||
| 50 | sub register { | ||
| 51 | my($self, $c) = @_; | ||
| 52 | |||
| 53 | unless ($self->__methods_cache) { | ||
| 54 | my @methods; | ||
| 55 | for my $method (@{ Class::Inspector->methods(ref $self) || [] }) { | ||
| 56 | next unless my $code = $self->can($method); | ||
| 57 | next unless my $attrs = $self->__attr_cache->{$code}; | ||
| 58 | push @methods, { method => $method, code => $code, attrs => $attrs }; | ||
| 59 | } | ||
| 60 | $self->__methods_cache( \@methods ); | ||
| 61 | } | ||
| 62 | |||
| 63 | my $is_attribute_detect_cache = $self->class_component_plugin_attribute_detect_cache_enable; | ||
| 64 | my $class = ref $self; | ||
| 65 | for my $data (@{ $self->__methods_cache }) { | ||
| 66 | for my $attr (@{ $data->{attrs} }) { | ||
| 67 | my($key, $value); | ||
| 68 | my $cache_key = "$class\::$attr"; | ||
| 69 | my $attr_res = $attribute_detect_cache{$cache_key}; | ||
| 70 | if ($is_attribute_detect_cache && $attr_res) { | ||
| 71 | ($key, $value) = ( $attr_res->[0], $attr_res->[1] ); | ||
| 72 | } else { | ||
| 73 | next unless ($key, $value) = $self->class_component_plugin_attribute_detect($attr, $cache_key); | ||
| 74 | } | ||
| 75 | |||
| 76 | my $attr_class; | ||
| 77 | if (my $pkg = $self->class_component_load_attribute_resolver($key)) { | ||
| 78 | $pkg->require or croak $@; | ||
| 79 | $attr_class = $pkg; | ||
| 80 | } else { | ||
| 81 | $attr_class = Class::Component::Implement->pkg_require($c => "Attribute::$key"); | ||
| 82 | } | ||
| 83 | unless ($attr_class) { | ||
| 84 | next unless $@; | ||
| 85 | croak "'$key' is not supported attribute"; | ||
| 86 | } | ||
| 87 | $attr_class->register($self, $c, $data->{method}, $value, $data->{code}); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | sub MODIFY_CODE_ATTRIBUTES { | ||
| 93 | my($class, $code, @attrs) = @_; | ||
| 94 | $class->__attr_cache->{$code} = [@attrs]; | ||
| 95 | return (); | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; | ||
| 99 | __END__ |