annotate lib/IMPL/Config/Bag.pm @ 413:af8d359ee4cc ref20150831

working on di container
author cin
date Thu, 24 Sep 2015 12:19:30 +0300
parents 30e8c6a74937
children ec6f2d389d1e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
412
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
1 package IMPL::Config::Bag;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
2 use strict;
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
3
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
4 use IMPL::declare {
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
5 base => [
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
6 'IMPL::Object' => undef
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
7 ],
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
8 props => [
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
9 parent => '*r',
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
10 _cache => '*rw',
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
11 _entries => '*rw'
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
12 ]
412
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
13 };
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
14
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
15 sub CTOR {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
16 my ($this, $p) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
17
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
18 $this->{$parent} = $p;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
19 $this->{$_cache} = {};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
20 $this->{$_entries} = [];
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
21 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
22
412
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
23 sub Resolve {
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
24 my ($this,$role) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
25
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
26 my $d = $this->GetDescriptor();
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
27 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
28
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
29 sub GetDescriptor {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
30 my ($this, $role) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
31
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
32 my $d = $this->{$_cache}{$role};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
33 return $d if $d and $d->{valid};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
34
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
35 my @entries = @{$this->{$_entries}};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
36
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
37 if(my $t = $this->{$parent} && $this->{$parent}->GetDescriptor($role)) {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
38 push @entries, $t;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
39 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
40
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
41 my $level;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
42 foreach my $entry (@entries) {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
43 my $t = $entry->{isa}{$role};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
44 next unless defined $t;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
45 if (defined($level) && $level > $t) {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
46 $d = $entry;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
47 $level = $t;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
48 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
49 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
50
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
51 if ($d and $d->{valid}) {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
52 $this->{$_cache}{$role} = $d;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
53 return $d;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
54 } else {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
55 return;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
56 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
57 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
58
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
59 sub ResolveAll {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
60 my ($this, $role) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
61
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
62 my $result = $this->{$parent} ? $this->{$parent}->ResolveAll() : [];
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
63
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
64 push @$result, map $_->{value}, grep $_->{isa}{$role}, @{$this->{$_entries}};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
65
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
66 return $result;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
67 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
68
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
69 sub Register {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
70 my ($this, $role, $isa, $value) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
71 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
72
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
73 sub _UpdateDescriptor {
412
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
74
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
75 }
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
76
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
77 package IMPL::Config::Bag::Entry;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
78 use IMPL::Exception();
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
79 use IMPL::declare {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
80 base => [
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
81 'IMPL::Object::Fields' => undef
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
82 ]
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
83 };
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
84
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
85 my @fields = qw(owner type isa valid value index);
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
86 use fields @fields;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
87
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
88 sub CTOR {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
89 my SELF $this = shift;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
90 my $args = shift;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
91
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
92 $this->{valid} = 1;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
93 $this->{owner} = $args->{owner} or die IMPL::InvalidArgumentException->new("owner");
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
94 $this->{value} = $args->{value} if exists $args->{value};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
95 $this->{isa} = $args->{isa} if $args->{isa};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
96 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
97
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
98 sub Invalidate {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
99 my SELF $this = shift;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
100
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
101 $this->{owner}->_UpdateDescriptor($this);
412
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
102 }
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
103
30e8c6a74937 working on di container (role based registrations)
cin
parents:
diff changeset
104 1;