comparison lib/IMPL/Config/ActivationContext.pm @ 421:7798345304bc ref20150831

working on IMPL::Config, removed old stuff
author cin
date Sun, 16 Jul 2017 22:59:39 +0300
parents 3ed0c58e9da3
children b0481c071bea
comparison
equal deleted inserted replaced
420:df591e3afd10 421:7798345304bc
2 2
3 use IMPL::lang qw(:base); 3 use IMPL::lang qw(:base);
4 use IMPL::Const qw(:prop); 4 use IMPL::Const qw(:prop);
5 use IMPL::Exception(); 5 use IMPL::Exception();
6 use IMPL::declare { 6 use IMPL::declare {
7 require => { 7 require => {
8 Bag => 'IMPL::Config::ServicesBag', 8 Bag => 'IMPL::Config::ServicesBag',
9 ServiceNotFoundException => 'IMPL::Config::ServiceNotFoundException', 9 ServiceNotFoundException => 'IMPL::Config::ServiceNotFoundException',
10 }, 10 },
11 base => { 11 base => {
12 'IMPL::Object' => '@_' 12 'IMPL::Object' => '@_'
13 }, 13 },
14 props => [ 14 props => [
15 container => PROP_RW, 15 container => PROP_RW,
16 instances => PROP_RW, 16 instances => PROP_RW,
17 _services => PROP_RW, 17 _services => PROP_RW,
18 _stack => PROP_RW 18 _stack => PROP_RW
19 ] 19 ]
20 }; 20 };
21 21
22 sub CTOR { 22 sub CTOR {
23 my ( $this, $container ) = @_; 23 my ( $this, $container ) = @_;
24 24
25 $this->container($container) 25 $this->container($container)
26 or die IMPL::InvalidArgumentException->new('container'); 26 or die IMPL::InvalidArgumentException->new('container');
27 $this->_services( $container->services ); 27 $this->_services( $container->services );
28 $this->instances( {} ); 28 $this->instances( {} );
29 $this->_stack( [] ); 29 $this->_stack( [] );
30 } 30 }
31 31
32 sub EnterScope { 32 sub EnterScope {
33 my ( $this, $name, $services ) = @_; 33 my ( $this, $name, $services ) = @_;
34 34
35 my $info = { name => $name }; 35 my $info = { name => $name };
36 36
37 if ($services) { 37 if ($services) {
38 die IMPL::InvalidArgumentException->new( 38 die IMPL::InvalidArgumentException->new(
39 services => 'An array is required' ) 39 services => 'An array is required' )
40 unless isarray($services); 40 unless isarray($services);
41 41
42 my $bag = $this->container->serviceCache->{ ref($services) }; 42 my $bag = $this->container->serviceCache->{ ref($services) };
43 43
44 unless ($bag) { 44 unless ($bag) {
45 my $container = $this->container; 45 my $container = $this->container;
46 $bag = Bag->new( $this->_services ); 46 $bag = Bag->new( $this->_services );
47 47
48 # 48 #
49 $bag->Register( 49 $bag->Register(
50 $container->GetLinearRoleHash( $_->{role}, $_->{descriptor} ) ) 50 $container->GetLinearRoleHash( $_->{role}, $_->{descriptor} ) )
51 foreach @$services; 51 foreach @$services;
52 52
53 $container->serviceCache->{ ref($services) } = $bag; 53 $container->serviceCache->{ ref($services) } = $bag;
54 } 54 }
55 55
56 $info->{services} = $this->_services; 56 $info->{services} = $this->_services;
57 $this->_services($bag); 57 $this->_services($bag);
58 } 58 }
59 59
60 push @{ $this->_stack }, $info; 60 push @{ $this->_stack }, $info;
61 } 61 }
62 62
63 sub LeaveScope { 63 sub LeaveScope {
64 my ($this) = @_; 64 my ($this) = @_;
65 65
66 my $info = pop @{ $this->_stack } 66 my $info = pop @{ $this->_stack }
67 or die IMPL::InvalidOperationException->new(); 67 or die IMPL::InvalidOperationException->new();
68 68
69 $this->_services( $info->{services} ) if $info->{services}; 69 $this->_services( $info->{services} ) if $info->{services};
70 } 70 }
71 71
72 sub Resolve { 72 sub Resolve {
73 my ( $this, $role, %opts ) = @_; 73 my ( $this, $role, %opts ) = @_;
74 74
75 my $d = $this->_services->Resolve($role); 75 my $d = $this->_services->Resolve($role);
76 76
77 unless ($d) { 77 unless ($d) {
78 die ServiceNotFoundException->new($role) unless $opts{optional}; 78 die ServiceNotFoundException->new($role) unless $opts{optional};
79 return $opts{default}; 79 return $opts{default};
80 } 80 }
81 else { 81 else {
82 return $d->Activate($this); 82 return $d->Activate($this);
83 } 83 }
84 } 84 }
85 85
86 sub Clone { 86 sub Clone {
87 my ($this) = @_; 87 my ($this) = @_;
88 88
89 my $clone = SELF->new( $this->container ); 89 my $clone = SELF->new( $this->container );
90 90
91 $clone->_services( $this->_services ); 91 $clone->_services( $this->_services );
92 $clone->instances( { %{ $this->instances } } ); 92 $clone->instances( { %{ $this->instances } } );
93 $clone->_stack( [ @{ $this->_stack } ] ); 93 $clone->_stack( [ @{ $this->_stack } ] );
94 94
95 return $clone; 95 return $clone;
96 } 96 }
97 97
98 1; 98 1;
99 __END__ 99 __END__
100 100