annotate 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
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 => {
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
8 Bag => 'IMPL::Config::ServicesBag',
416
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 },
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
14 props => [
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
15 container => PROP_RW,
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
16 instances => PROP_RW,
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 _services => PROP_RW,
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
18 _stack => PROP_RW
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
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 {
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');
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
27 $this->_services( $container->services );
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
28 $this->instances( {} );
3ed0c58e9da3 working on di container, tests
cin
parents: 416
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 {
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
33 my ( $this, $name, $services ) = @_;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
34
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 my $info = { name => $name };
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
36
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
37 if ($services) {
416
cin
parents: 415
diff changeset
38 die IMPL::InvalidArgumentException->new(
cin
parents: 415
diff changeset
39 services => 'An array is required' )
cin
parents: 415
diff changeset
40 unless isarray($services);
cin
parents: 415
diff changeset
41
cin
parents: 415
diff changeset
42 my $bag = $this->container->serviceCache->{ ref($services) };
cin
parents: 415
diff changeset
43
cin
parents: 415
diff changeset
44 unless ($bag) {
cin
parents: 415
diff changeset
45 my $container = $this->container;
cin
parents: 415
diff changeset
46 $bag = Bag->new( $this->_services );
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
47
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
48 #
416
cin
parents: 415
diff changeset
49 $bag->Register(
cin
parents: 415
diff changeset
50 $container->GetLinearRoleHash( $_->{role}, $_->{descriptor} ) )
cin
parents: 415
diff changeset
51 foreach @$services;
cin
parents: 415
diff changeset
52
cin
parents: 415
diff changeset
53 $container->serviceCache->{ ref($services) } = $bag;
cin
parents: 415
diff changeset
54 }
cin
parents: 415
diff changeset
55
cin
parents: 415
diff changeset
56 $info->{services} = $this->_services;
cin
parents: 415
diff changeset
57 $this->_services($bag);
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 }
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
59
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
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 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 my ($this) = @_;
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
65
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
66 my $info = pop @{ $this->_stack }
416
cin
parents: 415
diff changeset
67 or die IMPL::InvalidOperationException->new();
cin
parents: 415
diff changeset
68
cin
parents: 415
diff changeset
69 $this->_services( $info->{services} ) if $info->{services};
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 {
416
cin
parents: 415
diff changeset
73 my ( $this, $role, %opts ) = @_;
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
74
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
75 my $d = $this->_services->Resolve($role);
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
76
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
77 unless ($d) {
416
cin
parents: 415
diff changeset
78 die ServiceNotFoundException->new($role) unless $opts{optional};
cin
parents: 415
diff changeset
79 return $opts{default};
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
80 }
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
81 else {
416
cin
parents: 415
diff changeset
82 return $d->Activate($this);
cin
parents: 415
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 {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 407
diff changeset
87 my ($this) = @_;
417
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
88
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
89 my $clone = SELF->new( $this->container );
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
90
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
91 $clone->_services( $this->_services );
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
92 $clone->instances( { %{ $this->instances } } );
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
93 $clone->_stack( [ @{ $this->_stack } ] );
3ed0c58e9da3 working on di container, tests
cin
parents: 416
diff changeset
94
3ed0c58e9da3 working on di container, tests
cin
parents: 416
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