407
|
1 package IMPL::Config::ActivationContext;
|
|
2
|
416
|
3 use IMPL::lang qw(:base);
|
407
|
4 use IMPL::Const qw(:prop);
|
|
5 use IMPL::Exception();
|
|
6 use IMPL::declare {
|
|
7 require => {
|
417
|
8 Bag => 'IMPL::Config::ServicesBag',
|
416
|
9 ServiceNotFoundException => 'IMPL::Config::ServiceNotFoundException',
|
407
|
10 },
|
|
11 base => {
|
|
12 'IMPL::Object' => '@_'
|
|
13 },
|
417
|
14 props => [
|
415
|
15 container => PROP_RW,
|
417
|
16 instances => PROP_RW,
|
407
|
17 _services => PROP_RW,
|
415
|
18 _stack => PROP_RW
|
417
|
19 ]
|
407
|
20 };
|
|
21
|
415
|
22 sub CTOR {
|
|
23 my ( $this, $container ) = @_;
|
|
24
|
|
25 $this->container($container)
|
416
|
26 or die IMPL::InvalidArgumentException->new('container');
|
417
|
27 $this->_services( $container->services );
|
|
28 $this->instances( {} );
|
|
29 $this->_stack( [] );
|
407
|
30 }
|
|
31
|
|
32 sub EnterScope {
|
415
|
33 my ( $this, $name, $services ) = @_;
|
|
34
|
407
|
35 my $info = { name => $name };
|
415
|
36
|
|
37 if ($services) {
|
416
|
38 die IMPL::InvalidArgumentException->new(
|
|
39 services => 'An array is required' )
|
|
40 unless isarray($services);
|
|
41
|
|
42 my $bag = $this->container->serviceCache->{ ref($services) };
|
|
43
|
|
44 unless ($bag) {
|
|
45 my $container = $this->container;
|
|
46 $bag = Bag->new( $this->_services );
|
415
|
47
|
417
|
48 #
|
416
|
49 $bag->Register(
|
|
50 $container->GetLinearRoleHash( $_->{role}, $_->{descriptor} ) )
|
|
51 foreach @$services;
|
|
52
|
|
53 $container->serviceCache->{ ref($services) } = $bag;
|
|
54 }
|
|
55
|
|
56 $info->{services} = $this->_services;
|
|
57 $this->_services($bag);
|
407
|
58 }
|
415
|
59
|
417
|
60 push @{ $this->_stack }, $info;
|
407
|
61 }
|
|
62
|
|
63 sub LeaveScope {
|
|
64 my ($this) = @_;
|
415
|
65
|
417
|
66 my $info = pop @{ $this->_stack }
|
416
|
67 or die IMPL::InvalidOperationException->new();
|
|
68
|
|
69 $this->_services( $info->{services} ) if $info->{services};
|
|
70 }
|
415
|
71
|
|
72 sub Resolve {
|
416
|
73 my ( $this, $role, %opts ) = @_;
|
417
|
74
|
|
75 my $d = $this->_services->Resolve($role);
|
|
76
|
|
77 unless ($d) {
|
416
|
78 die ServiceNotFoundException->new($role) unless $opts{optional};
|
|
79 return $opts{default};
|
417
|
80 }
|
|
81 else {
|
416
|
82 return $d->Activate($this);
|
|
83 }
|
415
|
84 }
|
|
85
|
|
86 sub Clone {
|
|
87 my ($this) = @_;
|
417
|
88
|
|
89 my $clone = SELF->new( $this->container );
|
|
90
|
|
91 $clone->_services( $this->_services );
|
|
92 $clone->instances( { %{ $this->instances } } );
|
|
93 $clone->_stack( [ @{ $this->_stack } ] );
|
|
94
|
|
95 return $clone;
|
407
|
96 }
|
|
97
|
|
98 1;
|
|
99 __END__
|
|
100
|
|
101 =pod
|
|
102
|
|
103 =head1 NAME
|
|
104
|
|
105 C<IMPL::Config::ActivationContext> - an activation context for the service
|
|
106
|
|
107 =head1 SYNOPSIS
|
|
108
|
|
109 For the internal use only
|
|
110
|
|
111 =head1 MEMBERS
|
|
112
|
|
113 =head2 METHODS
|
|
114
|
416
|
115 =head3 Resolve($serviceId)
|
407
|
116
|
415
|
117 =cut
|