comparison lib/IMPL/Config/ActivationContext.pm @ 417:3ed0c58e9da3 ref20150831

working on di container, tests
author cin
date Mon, 02 Nov 2015 01:56:53 +0300
parents cc2cf8c0edc2
children 7798345304bc
comparison
equal deleted inserted replaced
416:cc2cf8c0edc2 417:3ed0c58e9da3
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 _services => PROP_RW, 17 _services => PROP_RW,
17 _cache => 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->_cache({}); 27 $this->_services( $container->services );
28 $this->_stack([]); 28 $this->instances( {} );
29 $this->_stack( [] );
29 } 30 }
30 31
31 sub EnterScope { 32 sub EnterScope {
32 my ( $this, $name, $services ) = @_; 33 my ( $this, $name, $services ) = @_;
33 34
42 43
43 unless ($bag) { 44 unless ($bag) {
44 my $container = $this->container; 45 my $container = $this->container;
45 $bag = Bag->new( $this->_services ); 46 $bag = Bag->new( $this->_services );
46 47
48 #
47 $bag->Register( 49 $bag->Register(
48 $container->GetLinearRoleHash( $_->{role}, $_->{descriptor} ) ) 50 $container->GetLinearRoleHash( $_->{role}, $_->{descriptor} ) )
49 foreach @$services; 51 foreach @$services;
50 52
51 $container->serviceCache->{ ref($services) } = $bag; 53 $container->serviceCache->{ ref($services) } = $bag;
53 55
54 $info->{services} = $this->_services; 56 $info->{services} = $this->_services;
55 $this->_services($bag); 57 $this->_services($bag);
56 } 58 }
57 59
58 push @{$this->_stack}, $info; 60 push @{ $this->_stack }, $info;
59 } 61 }
60 62
61 sub LeaveScope { 63 sub LeaveScope {
62 my ($this) = @_; 64 my ($this) = @_;
63 65
64 my $info = pop @{$this->_stack} 66 my $info = pop @{ $this->_stack }
65 or die IMPL::InvalidOperationException->new(); 67 or die IMPL::InvalidOperationException->new();
66 68
67 $this->_services( $info->{services} ) if $info->{services}; 69 $this->_services( $info->{services} ) if $info->{services};
68 } 70 }
69 71
70 sub GuardScope {
71 my ( $this, $name, $services, $action ) = @_;
72
73 $this->EnterScope( $name, $service );
74 eval { $action ($this) if $action; } my $err = $@;
75 $this->LeaveScope();
76 die $err if $err;
77 }
78
79 sub Resolve { 72 sub Resolve {
80 my ( $this, $role, %opts ) = @_; 73 my ( $this, $role, %opts ) = @_;
81 74
82 my $d = $this->_services->Reolve($role); 75 my $d = $this->_services->Resolve($role);
83 76
84 unless($d) { 77 unless ($d) {
85 die ServiceNotFoundException->new($role) unless $opts{optional}; 78 die ServiceNotFoundException->new($role) unless $opts{optional};
86 return $opts{default}; 79 return $opts{default};
87 } else { 80 }
81 else {
88 return $d->Activate($this); 82 return $d->Activate($this);
89 } 83 }
90 } 84 }
91 85
92 sub Clone { 86 sub Clone {
93 my ($this) = @_; 87 my ($this) = @_;
94 88
95 my $clone = SELF->new($this->container); 89 my $clone = SELF->new( $this->container );
96 90
97 $clone->_ 91 $clone->_services( $this->_services );
92 $clone->instances( { %{ $this->instances } } );
93 $clone->_stack( [ @{ $this->_stack } ] );
94
95 return $clone;
98 } 96 }
99 97
100 1; 98 1;
101 __END__ 99 __END__
102 100