Mercurial > pub > Impl
comparison lib/IMPL/Config/ActivationContext.pm @ 416:cc2cf8c0edc2 ref20150831
sync
author | cin |
---|---|
date | Thu, 29 Oct 2015 03:50:25 +0300 |
parents | 3d24b10dd0d5 |
children | 3ed0c58e9da3 |
comparison
equal
deleted
inserted
replaced
415:3d24b10dd0d5 | 416:cc2cf8c0edc2 |
---|---|
1 package IMPL::Config::ActivationContext; | 1 package IMPL::Config::ActivationContext; |
2 | 2 |
3 use IMPL::lang qw(:base); | |
3 use IMPL::Const qw(:prop); | 4 use IMPL::Const qw(:prop); |
4 use IMPL::Exception(); | 5 use IMPL::Exception(); |
5 use IMPL::declare { | 6 use IMPL::declare { |
6 require => { | 7 require => { |
7 PropertyBag => 'IMPL::Config::ServicesBag' | 8 Bag => 'IMPL::Config::ServicesBag', |
9 ServiceNotFoundException => 'IMPL::Config::ServiceNotFoundException', | |
8 }, | 10 }, |
9 base => { | 11 base => { |
10 'IMPL::Object' => '@_' | 12 'IMPL::Object' => '@_' |
11 }, | 13 }, |
12 props => { | 14 props => { |
19 | 21 |
20 sub CTOR { | 22 sub CTOR { |
21 my ( $this, $container ) = @_; | 23 my ( $this, $container ) = @_; |
22 | 24 |
23 $this->container($container) | 25 $this->container($container) |
24 or die IMPL::InvalidArgumentException('container'); | 26 or die IMPL::InvalidArgumentException->new('container'); |
27 $this->_cache({}); | |
28 $this->_stack([]); | |
25 } | 29 } |
26 | 30 |
27 sub EnterScope { | 31 sub EnterScope { |
28 my ( $this, $name, $services ) = @_; | 32 my ( $this, $name, $services ) = @_; |
29 | 33 |
30 my $info = { name => $name }; | 34 my $info = { name => $name }; |
31 | 35 |
32 if ($services) { | 36 if ($services) { |
37 die IMPL::InvalidArgumentException->new( | |
38 services => 'An array is required' ) | |
39 unless isarray($services); | |
40 | |
41 my $bag = $this->container->serviceCache->{ ref($services) }; | |
42 | |
43 unless ($bag) { | |
44 my $container = $this->container; | |
45 $bag = Bag->new( $this->_services ); | |
46 | |
47 $bag->Register( | |
48 $container->GetLinearRoleHash( $_->{role}, $_->{descriptor} ) ) | |
49 foreach @$services; | |
50 | |
51 $container->serviceCache->{ ref($services) } = $bag; | |
52 } | |
53 | |
33 $info->{services} = $this->_services; | 54 $info->{services} = $this->_services; |
34 | 55 $this->_services($bag); |
35 $this->_services( $services ); | |
36 } | 56 } |
37 | 57 |
38 $this->_stack->Push($info); | 58 push @{$this->_stack}, $info; |
39 } | 59 } |
40 | 60 |
41 sub LeaveScope { | 61 sub LeaveScope { |
42 my ($this) = @_; | 62 my ($this) = @_; |
43 | 63 |
44 my $info = $this->_stack->Pop() | 64 my $info = pop @{$this->_stack} |
45 or die IMPL::InvalidOperationException(); | 65 or die IMPL::InvalidOperationException->new(); |
46 | 66 |
47 if ( $info->{services} ) $this->_services( $info->{services} ); | 67 $this->_services( $info->{services} ) if $info->{services}; |
68 } | |
69 | |
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; | |
48 } | 77 } |
49 | 78 |
50 sub Resolve { | 79 sub Resolve { |
51 my ($this, $role, %opts) = @_; | 80 my ( $this, $role, %opts ) = @_; |
81 | |
82 my $d = $this->_services->Reolve($role); | |
83 | |
84 unless($d) { | |
85 die ServiceNotFoundException->new($role) unless $opts{optional}; | |
86 return $opts{default}; | |
87 } else { | |
88 return $d->Activate($this); | |
89 } | |
52 } | 90 } |
53 | 91 |
54 sub Clone { | 92 sub Clone { |
55 my ($this) = @_; | 93 my ($this) = @_; |
94 | |
95 my $clone = SELF->new($this->container); | |
96 | |
97 $clone->_ | |
56 } | 98 } |
57 | 99 |
58 1; | 100 1; |
59 __END__ | 101 __END__ |
60 | 102 |
70 | 112 |
71 =head1 MEMBERS | 113 =head1 MEMBERS |
72 | 114 |
73 =head2 METHODS | 115 =head2 METHODS |
74 | 116 |
75 =head3 GetService($serviceId) | 117 =head3 Resolve($serviceId) |
76 | 118 |
77 =cut | 119 =cut |