annotate lib/IMPL/Config/ServicesBag.pm @ 420:df591e3afd10 ref20150831

sync
author cin
date Sat, 25 Feb 2017 22:35:26 +0300
parents ee36115f6a34
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Config::ServicesBag;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 require v5.9.5;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use mro;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 use IMPL::declare {
411
cin
parents: 407
diff changeset
9 require => {
cin
parents: 407
diff changeset
10 Entry => '-IMPL::Config::ServicesBag::Entry'
cin
parents: 407
diff changeset
11 },
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 'IMPL::Object' => undef
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 _prototype => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 _nameMap => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 _typeMap => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 _props => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 my ( $this, $prototype ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 $this->_prototype($prototype) if $prototype;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 $this->_nameMap( {} );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 $this->_typeMap( {} );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 sub GetDescriptorByName {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 my ( $this, $name ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 my $d = $this->_nameMap->{$name};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 return $d if $d and $d->{valid};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 my $parent = $this->_prototype;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 if ( $parent and $d = $parent->GetDescriptorByName($name) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 return $this->_nameMap->{$name} = $d;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 return undef;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 sub GetDescriptorByType {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 my ( $this, $type ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 my $d = $this->_typeMap->{$type};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 return $d if $d and $d->{valid};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 my $parent = $this->_prototype;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 if ( $parent and $d = $parent->GetDescriptorByType($type) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 return $this->_typeMap->{$type} = $d;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 return undef;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 sub RegisterValue {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 my ( $this, $value, $name, $type ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62
411
cin
parents: 407
diff changeset
63 my $d = Entry->new( {owner => $this, value => $value} );
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 if ($type) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 my $map = $this->_typeMap;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 my $isa = mro::get_linear_isa($type);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 $d->{isa} = $isa;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 # the service record which is superseded by the current one
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 my $replaces = $this->GetDescriptorByType($type);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 foreach my $t (@$isa) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 if ( my $prev = $this->GetDescriptorByType($t) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 # keep previous registrations if they are valid
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 next if not $replaces or $prev != $replaces;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 $map->{$t} = $d;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 # invalidate cache
411
cin
parents: 407
diff changeset
84 $replaces->Invalidate() if $replaces;
cin
parents: 407
diff changeset
85
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 if ($name) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 my $prev = $this->_nameMap->{$name};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 $d->{name} = $name;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 $this->_nameMap->{$name} = $d;
411
cin
parents: 407
diff changeset
92 $prev->Invalidate() if $prev;
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 return $d;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97
411
cin
parents: 407
diff changeset
98 sub _UpdateDescriptor {
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 my ( $this, $d ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100
411
cin
parents: 407
diff changeset
101 my $d2 = Entry->new($d);
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 # update named entries
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 my $name = $d->{name};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 if ( $name and $this->_nameMap->{$name} == $d ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 $this->_nameMap->{$name} = $d2;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 # update type entries
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 if ( my $isa = $d->{isa} ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 my $map = $this->_typeMap;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 foreach my $t (@$isa) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 next unless $map->{$t} == $d;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 $map->{$t} = $d2;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 $d->{valid} = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120
411
cin
parents: 407
diff changeset
121 package IMPL::Config::ServicesBag::Entry;
cin
parents: 407
diff changeset
122 use IMPL::Exception();
cin
parents: 407
diff changeset
123 use IMPL::declare {
cin
parents: 407
diff changeset
124 base => [
cin
parents: 407
diff changeset
125 'IMPL::Object::Fields' => undef
cin
parents: 407
diff changeset
126 ]
cin
parents: 407
diff changeset
127 };
cin
parents: 407
diff changeset
128
cin
parents: 407
diff changeset
129 my @fields = qw(owner type isa valid value);
cin
parents: 407
diff changeset
130 use fields @fields;
cin
parents: 407
diff changeset
131
cin
parents: 407
diff changeset
132 sub CTOR {
cin
parents: 407
diff changeset
133 my SELF $this = shift;
cin
parents: 407
diff changeset
134 my $args = shift;
cin
parents: 407
diff changeset
135
cin
parents: 407
diff changeset
136 $this->{valid} = 1;
cin
parents: 407
diff changeset
137 $this->{owner} = $args{owner} or die IMPL::InvalidArgumentException->new("owner");
cin
parents: 407
diff changeset
138 $this->{value} = $args{value} if exists $args->{value};
cin
parents: 407
diff changeset
139 $this->{isa} = $args{isa} if $args->{isa};
cin
parents: 407
diff changeset
140 }
cin
parents: 407
diff changeset
141
cin
parents: 407
diff changeset
142 sub Invalidate {
cin
parents: 407
diff changeset
143 my SELF $this = shift;
cin
parents: 407
diff changeset
144
cin
parents: 407
diff changeset
145 $this->{owner}->_UpdateDescriptor($this);
cin
parents: 407
diff changeset
146 }
cin
parents: 407
diff changeset
147
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160 Коллекция сервисов построена на прототиптровании экземпляров, т.е. при создании
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161 новой коллекции может указваться базовая коллекция в которой будет происходить
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162 поиск сервисов в случае их отсутсвия в основной. Для оптимизации данного процесса
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163 сервисы кешируются, чтобы избежать можестрвенных операций поиска по иерархии
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164 коллекций, для этого каждый сервис описывается дескриптором:
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
168 =item * isa массив типов сервиса, если он регистрировался как сервис
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
169
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170 =item * value значение
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172 =item * valid признак того, что дескриптор действителен
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174 =item * owner коллекция, которая создала данный дескриптор
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178 Если запрашиваемый десриптор не найден это является ошибкой, поэтому негативные
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179 ответы не кешируются
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181 =cut