92
|
1 package IMPL::Config::Resolve;
|
|
2 use base qw(IMPL::Object IMPL::Object::Autofill IMPL::Object::Serializable);
|
|
3
|
|
4 use IMPL::Class::Property;
|
|
5 use IMPL::Exception;
|
|
6
|
|
7 BEGIN {
|
|
8 public property target => prop_all;
|
|
9 public property path => prop_all;
|
|
10 public property default => prop_all
|
|
11 }
|
|
12
|
|
13 __PACKAGE__->PassThroughArgs;
|
|
14
|
|
15 sub CTOR {
|
|
16 my ($this) = @_;
|
|
17
|
|
18 die new IMPL::InvalidArgumentException("The argument is mandatory","target") unless $this->target;
|
|
19 die new IMPL::InvalidArgumentException("The argument is mandatory","path") unless $this->path;
|
|
20 }
|
|
21
|
|
22 sub Invoke {
|
|
23 my ($this) = @_;
|
|
24
|
|
25 my $path = $this->path;
|
|
26
|
|
27 if (ref $path eq 'ARRAY') {
|
|
28 my $result = $this->target;
|
|
29 $result = $this->_InvokeMember($result,$_) || return $this->default foreach @$path;
|
|
30 return $result;
|
|
31 } elsif (not ref $path) {
|
|
32 my $result = $this->target;
|
|
33 $result = $this->_InvokeMember($result,$_) || return $this->default foreach map { name => $_},split /\./,$this->path;
|
|
34 return $result;
|
|
35 } else {
|
|
36 die new IMPL::InvalidOperationException("Unsopported path type",ref $path);
|
|
37 }
|
|
38 }
|
|
39
|
|
40 sub _InvokeMember {
|
|
41 my ($self,$object,$member) = @_;
|
|
42
|
|
43 local $@;
|
|
44 return eval { $object->($member->{method})(exists $member->{parameters} ? _as_list($member->{parameters}) : ()) };
|
|
45 }
|
|
46
|
|
47 sub _as_list {
|
|
48 ref $_[0] ?
|
|
49 (ref $_[0] eq 'HASH' ?
|
|
50 %{$_[0]}
|
|
51 :
|
|
52 (ref $_[0] eq 'ARRAY'?
|
|
53 @{$_[0]}
|
|
54 :
|
|
55 $_[0]
|
|
56 )
|
|
57 )
|
|
58 :
|
|
59 ($_[0]);
|
|
60 }
|
|
61
|
|
62 1;
|