annotate lib/IMPL/Config/ActivationContext.pm @ 416:cc2cf8c0edc2 ref20150831

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