comparison lib/IMPL/Config/Container.pm @ 413:af8d359ee4cc ref20150831

working on di container
author cin
date Thu, 24 Sep 2015 12:19:30 +0300
parents 30e8c6a74937
children ec6f2d389d1e
comparison
equal deleted inserted replaced
412:30e8c6a74937 413:af8d359ee4cc
1 package IMPL::Config::Container; 1 package IMPL::Config::Container;
2 use strict; 2 use strict;
3 3
4 use IMPL::Exception();
4 use IMPL::lang qw(:base); 5 use IMPL::lang qw(:base);
5 use IMPL::declare { 6 use IMPL::declare {
6 require => { 7 require => {
7 Descriptor => 'IMPL::Config::Descriptor' 8 Descriptor => 'IMPL::Config::Descriptor',
9 ValueDescriptor => 'IMPL::Config::ValueDescriptor',
10 ActivationContext => 'IMPL::Config::ActivationContext',
11 Hierarchy => 'IMPL::Config::Hierarchy',
12 Bag => 'IMPL::Config::Bag'
8 }, 13 },
9 base => [ 14 base => [
10 'IMPL::Object' => undef 15 'IMPL::Object' => undef
11 ], 16 ],
12 props => [ 17 props => [
13 roles => 'r', 18 roles => 'r',
14 services => 'r', 19 services => 'r',
15 instances => 'r' 20 instances => 'r',
21 parent => 'r'
16 ] 22 ]
17 }; 23 };
18 24
19 my $nextRoleId = 1; 25 my $nextRoleId = 1;
20 26
21 use IMPL::Exception(); 27 sub CTOR {
28 my ( $this, $parent ) = @_;
29
30 $this->instances( {} );
31
32 if ($parent) {
33 $this->roles( Hierarchy->new( $parent->roles ) );
34 $this->services( Bag->new( $parent->services ) );
35 $this->parent($parent);
36 }
37 else {
38 $this->roles( Hierarchy->new() );
39 $this->services( Bag->new() );
40 }
41 }
22 42
23 sub Register { 43 sub Register {
24 my ($this, $role, $service) = @_; 44 my ( $this, $role, $service ) = @_;
25 45
26 die IMPL::InvalidArgumentException->new(role => 'The argument is required') unless $role; 46 die IMPL::InvalidArgumentException->new(
27 die IMPL::InvalidArgumentException->new('service') unless is($service, Descriptor); 47 role => 'The argument is required' )
28 48 unless $role;
29 if (isarray($role)) { 49 die IMPL::InvalidArgumentException->new('service')
30 my $tempRole = "unnamed-" . $nextRoleId++; 50 unless is( $service, Descriptor );
31 $this->role->AddRole($tempRole, $role); 51
32 $role = $tempRole; 52 if ( isarray($role) ) {
53 my $tempRole = "unnamed-" . $nextRoleId++;
54 $this->role->AddRole( $tempRole, $role );
55 $role = $tempRole;
33 } 56 }
34 57
35 $this->services->Register($role, $service); 58 $service = ValueDescriptor->new( value => $service )
36 59 unless is( $service, Descriptor );
60
61 $this->services->Register( $role, $this->roles->GetLinearRoleHash($role), $service );
62 }
63
64 sub Resolve {
65 my ( $this, $role, %opts ) = @_;
66
67 my $descriptor = $this->services->Resolve($role);
68
69 return $descriptor->Activate( ActivationContext->new($this) )
70 if $descirptor;
71
72 return $opts{default} if exists $opts{default};
73 }
74
75 sub ResolveAll {
76 my ( $this, $role, %opts ) = @_;
77
78 my $all = $this->services->ResolveAll($role);
79
80 my $context;
81
82 my @result;
83
84 foreach my $service (@$all) {
85 $context = ActivationContext->new($this)
86 unless $context || $opts{shared};
87
88 push @result, $service->Activate($context);
89 }
90
91 return \@result;
37 } 92 }
38 93
39 1; 94 1;
40 95
41 __END__ 96 __END__