annotate lib/IMPL/Config/ActivationContext.pm @ 422:b0481c071bea ref20150831

IMPL::Config::Container tests, YAMLConfiguration now works and tested
author cin
date Sun, 20 Aug 2017 00:20:41 +0300
parents 7798345304bc
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::ActivationContext;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2
416
cin
parents: 415
diff changeset
3 use IMPL::lang qw(:base);
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use IMPL::Exception();
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use IMPL::declare {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
6 require => {
422
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
7 Bag => 'IMPL::Config::Bag',
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
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
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
10 },
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
11 base => {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
12 'IMPL::Object' => '@_'
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
13 },
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
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
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
21 ]
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
24 sub CTOR {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
25 my ( $this, $container ) = @_;
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
26
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
27 $this->container($container)
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
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
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
30 $this->_services( $container->services );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
31 $this->instances( {} );
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
32 $this->_stack( [] );
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
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
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
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
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
46
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
47 if ($services) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
48 die IMPL::InvalidArgumentException->new(
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
49 services => 'An array is required' )
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
50 unless isarray($services);
416
cin
parents: 415
diff changeset
51
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
52 my $bag = $this->container->serviceCache->{ ref($services) };
416
cin
parents: 415
diff changeset
53
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
54 unless ($bag) {
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
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
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
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
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
60 foreach @$services;
416
cin
parents: 415
diff changeset
61
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
62 $container->serviceCache->{ ref($services) } = $bag;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
63 }
416
cin
parents: 415
diff changeset
64
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
65 $info->{services} = $this->_services;
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
66 $this->_services($bag);
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
67 }
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
68
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
69 push @{ $this->_stack }, $info;
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 sub LeaveScope {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
73 my ($this) = @_;
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
74
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
75 my $info = pop @{ $this->_stack }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
76 or die IMPL::InvalidOperationException->new();
416
cin
parents: 415
diff changeset
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
cin
parents: 415
diff changeset
81 }
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
82
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
83 sub Resolve {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
84 my ( $this, $role, %opts ) = @_;
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
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
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
88
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
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
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
91 return $opts{default};
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
92 }
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
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
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
98 }
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
99 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
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
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
109 sub Clone {
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
110 my ($this) = @_;
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
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
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
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
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
117 $clone->_stack( [ @{ $this->_stack } ] );
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
118
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 417
diff changeset
119 return $clone;
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 C<IMPL::Config::ActivationContext> - an activation context for the service
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 For the internal use only
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 =head1 MEMBERS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
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
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 =head2 METHODS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
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
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
166 =cut