Mercurial > pub > Impl
annotate lib/IMPL/Config/ActivationContext.pm @ 427:09e0086a82a7 ref20150831 tip
Merge
| author | cin |
|---|---|
| date | Tue, 15 May 2018 00:51:33 +0300 |
| parents | b0481c071bea |
| children |
| rev | line source |
|---|---|
| 407 | 1 package IMPL::Config::ActivationContext; |
| 2 | |
| 416 | 3 use IMPL::lang qw(:base); |
| 407 | 4 use IMPL::Exception(); |
| 5 use IMPL::declare { | |
| 421 | 6 require => { |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
7 Bag => 'IMPL::Config::Bag', |
| 421 | 8 ServiceNotFoundException => 'IMPL::Config::ServiceNotFoundException', |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
9 Descriptor => '-IMPL::Config::Descriptor' |
| 421 | 10 }, |
| 11 base => { | |
| 12 'IMPL::Object' => '@_' | |
| 13 }, | |
| 14 props => [ | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
15 container => 'rw', |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
16 owner => 'rw', |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
17 instances => 'rw', |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
18 name => 'rw', |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
19 _services => 'rw', |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
20 _stack => 'rw' |
| 421 | 21 ] |
| 407 | 22 }; |
| 23 | |
| 415 | 24 sub CTOR { |
| 421 | 25 my ( $this, $container ) = @_; |
| 415 | 26 |
| 421 | 27 $this->container($container) |
| 28 or die IMPL::InvalidArgumentException->new('container'); | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
29 $this->owner($container); |
| 421 | 30 $this->_services( $container->services ); |
| 31 $this->instances( {} ); | |
| 32 $this->_stack( [] ); | |
| 407 | 33 } |
| 34 | |
| 35 sub EnterScope { | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
36 my ( $this, $name, $services, $container ) = @_; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
37 |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
38 my $info = { name => $this->name }; |
| 415 | 39 |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
40 $this->name($name); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
41 |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
42 if ( $container && $this->container != $container ) { |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
43 $info->{container} = $this->container; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
44 $this->container($container); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
45 } |
| 415 | 46 |
| 421 | 47 if ($services) { |
| 48 die IMPL::InvalidArgumentException->new( | |
| 49 services => 'An array is required' ) | |
| 50 unless isarray($services); | |
| 416 | 51 |
| 421 | 52 my $bag = $this->container->serviceCache->{ ref($services) }; |
| 416 | 53 |
| 421 | 54 unless ($bag) { |
| 55 $bag = Bag->new( $this->_services ); | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
56 $bag->tag( $container || $this->container ); |
| 415 | 57 |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
58 $bag->Register( $container->GetLinearRoleHash( $_->{role} ), |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
59 $_->{descriptor} ) |
| 421 | 60 foreach @$services; |
| 416 | 61 |
| 421 | 62 $container->serviceCache->{ ref($services) } = $bag; |
| 63 } | |
| 416 | 64 |
| 421 | 65 $info->{services} = $this->_services; |
| 66 $this->_services($bag); | |
| 67 } | |
| 415 | 68 |
| 421 | 69 push @{ $this->_stack }, $info; |
| 407 | 70 } |
| 71 | |
| 72 sub LeaveScope { | |
| 421 | 73 my ($this) = @_; |
| 415 | 74 |
| 421 | 75 my $info = pop @{ $this->_stack } |
| 76 or die IMPL::InvalidOperationException->new(); | |
| 416 | 77 |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
78 $this->name( $info->{name} ); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
79 $this->_services( $info->{services} ) if $info->{services}; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
80 $this->conatiner( $info->{container} ) if $info->{container}; |
| 416 | 81 } |
| 415 | 82 |
| 83 sub Resolve { | |
| 421 | 84 my ( $this, $role, %opts ) = @_; |
| 417 | 85 |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
86 #change of the container may occur only due resolution of the dependency |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
87 my ( $d, $bag ) = $this->_services->Resolve($role); |
| 417 | 88 |
| 421 | 89 unless ($d) { |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
90 die ServiceNotFoundException->new(serviceName => $role) unless $opts{optional}; |
| 421 | 91 return $opts{default}; |
| 92 } | |
| 93 else { | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
94 $this->EnterScope( $d->GetName(), $d->services(), $bag->tag() ); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
95 my $instance = $d->Activate($this); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
96 $this->LeaveScope(); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
97 return $instance; |
| 421 | 98 } |
| 415 | 99 } |
| 100 | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
101 sub Activate { |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
102 my ( $this, $d ) = @_; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
103 $this->EnterScope( $d->GetName(), $d->services() ); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
104 my $instance = $d->Activate($this); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
105 $this->LeaveScope(); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
106 return $instance; |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
107 } |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
108 |
| 415 | 109 sub Clone { |
| 421 | 110 my ($this) = @_; |
| 417 | 111 |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
112 my $clone = SELF->new( $this->owner ); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
113 $clone->name($this->name); |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
114 $clone->container( $this->container ); |
| 421 | 115 $clone->_services( $this->_services ); |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
116 $clone->instances( $this->instances ); |
| 421 | 117 $clone->_stack( [ @{ $this->_stack } ] ); |
| 417 | 118 |
| 421 | 119 return $clone; |
| 407 | 120 } |
| 121 | |
| 122 1; | |
| 123 __END__ | |
| 124 | |
| 125 =pod | |
| 126 | |
| 127 =head1 NAME | |
| 128 | |
| 129 C<IMPL::Config::ActivationContext> - an activation context for the service | |
| 130 | |
| 131 =head1 SYNOPSIS | |
| 132 | |
| 133 For the internal use only | |
| 134 | |
| 135 =head1 MEMBERS | |
| 136 | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
137 =head2 PROPERTIES |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
138 |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
139 =head3 [get] container |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
140 |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
141 Current container for the activation context, this container changes |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
142 during resolution process to match the one in which the resulting |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
143 descriptor were defined. Descriptors can use this property to |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
144 access the cache of theirs container. |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
145 |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
146 =head3 [get] owner |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
147 |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
148 The container which created this context. Descriptors can use this |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
149 property during theirs activation. |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
150 |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
151 =head3 [get] instances |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
152 |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
153 The activation cache which can be used to store instances during |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
154 single resolution process. |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
155 |
| 407 | 156 =head2 METHODS |
| 157 | |
|
422
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
158 =head3 Resolve($serviceId): $instance |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
159 |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
160 Activates and returns an instance specified by C<$serviceId> |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
161 |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
162 =head3 Activate($descriptor): $instance |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
163 |
|
b0481c071bea
IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents:
421
diff
changeset
|
164 Activates and returns an instance of the services for the specified descriptor/ |
| 407 | 165 |
| 415 | 166 =cut |
