comparison lib/IMPL/Config/Container.pm @ 415:3d24b10dd0d5 ref20150831

working on IMPL::Config::Container
author cin
date Tue, 20 Oct 2015 07:32:55 +0300
parents ec6f2d389d1e
children 3ed0c58e9da3
comparison
equal deleted inserted replaced
414:ec6f2d389d1e 415:3d24b10dd0d5
7 require => { 7 require => {
8 Descriptor => 'IMPL::Config::Descriptor', 8 Descriptor => 'IMPL::Config::Descriptor',
9 ValueDescriptor => 'IMPL::Config::ValueDescriptor', 9 ValueDescriptor => 'IMPL::Config::ValueDescriptor',
10 ActivationContext => 'IMPL::Config::ActivationContext', 10 ActivationContext => 'IMPL::Config::ActivationContext',
11 Hierarchy => 'IMPL::Config::Hierarchy', 11 Hierarchy => 'IMPL::Config::Hierarchy',
12 Bag => 'IMPL::Config::Bag' 12 Bag => 'IMPL::Config::Bag',
13 Loader => 'IMPL::Code::Loader'
13 }, 14 },
14 base => [ 15 base => [
15 'IMPL::Object' => undef 16 'IMPL::Object' => undef
16 ], 17 ],
17 props => [ 18 props => [
18 roles => 'r', 19 roles => 'r',
19 services => 'r', 20 services => 'r',
20 instances => 'r', 21 instances => 'r',
21 parent => 'r' 22 parent => 'r',
23 root => 'r',
24 loader => 'r'
22 ] 25 ]
23 }; 26 };
24 27
25 my $nextRoleId = 1; 28 my $nextRoleId = 1;
26 29
27 sub CTOR { 30 sub CTOR {
28 my ( $this, $parent ) = @_; 31 my ( $this, $parent, %opts ) = @_;
29 32
30 $this->instances( {} ); 33 $this->instances( {} );
34
35 $this->loader( $opts{loader} || Loader->safe );
31 36
32 if ($parent) { 37 if ($parent) {
33 $this->roles( Hierarchy->new( $parent->roles ) ); 38 $this->roles( Hierarchy->new( $parent->roles ) );
34 $this->services( Bag->new( $parent->services ) ); 39 $this->services( Bag->new( $parent->services ) );
35 $this->parent($parent); 40 $this->parent($parent);
41 $this->root( $parent->root );
36 } 42 }
37 else { 43 else {
38 $this->roles( Hierarchy->new() ); 44 $this->roles( Hierarchy->new() );
39 $this->services( Bag->new() ); 45 $this->services( Bag->new() );
46 $this->root($this);
40 } 47 }
41 } 48 }
42 49
43 sub Register { 50 sub Register {
44 my ( $this, $role, $service ) = @_; 51 my ( $this, $role, $service ) = @_;
45 52
53 die IMPL::InvalidArgumentException->new('service')
54 unless is( $service, Descriptor );
55 $this->services->Register( $this->GetLinearRoleHash($role), $service );
56 }
57
58 sub GetLinearRoleHash {
59 my ( $this, $role ) = @_;
60
46 die IMPL::InvalidArgumentException->new( 61 die IMPL::InvalidArgumentException->new(
47 role => 'The argument is required' ) 62 role => 'The argument is required' )
48 unless $role; 63 unless $role;
49 die IMPL::InvalidArgumentException->new('service')
50 unless is( $service, Descriptor );
51 64
52 if ( isarray($role) ) { 65 if ( isarray($role) ) {
53 my $tempRole = "unnamed-" . $nextRoleId++; 66 my $tempRole = "unnamed-" . $nextRoleId++;
54 $this->role->AddRole( $tempRole, $role ); 67 $this->role->AddRole( $tempRole, $role );
55 $role = $tempRole; 68 $role = $tempRole;
56 } 69 }
57 70
58 $service = ValueDescriptor->new( value => $service ) 71 $this->roles->GetLinearRoleHash($role);
59 unless is( $service, Descriptor );
60
61 $this->services->Register( $this->roles->GetLinearRoleHash($role), $service );
62 } 72 }
63 73
64 sub Resolve { 74 sub Resolve {
65 my ( $this, $role, %opts ) = @_; 75 my ( $this, $role, %opts ) = @_;
66 76
103 113
104 =head1 SYNOPSIS 114 =head1 SYNOPSIS
105 115
106 =head2 METHODS 116 =head2 METHODS
107 117
108 =head3 GetService($serviceId) 118 =head3 Resolve($role)
109 119
110 =over 120 =head3 ResolveAll($role, shared => $useSharedContext)
111 121
112 =item * $serviceId 122 =head3 Register($role, $service)
113
114 A string indetifier of the service, it can be in two forms: class name or service name,
115 for the class name it should be prefixed with C<@>, for example: C<@Foo::Bar>.
116
117 =back
118
119 The activation container maintains two maps, one for classes and the other for names.
120 The first one is useful when we searching for an implementation the second one when
121 we need a particular service.
122
123 =head3 RegisterService($descriptor)
124 123
125 =cut 124 =cut