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