comparison lib/IMPL/Config/Container.pm @ 420:df591e3afd10 ref20150831

sync
author cin
date Sat, 25 Feb 2017 22:35:26 +0300
parents bbc4739c4d48
children 7798345304bc
comparison
equal deleted inserted replaced
419:bbc4739c4d48 420:df591e3afd10
1 package IMPL::Config::Container; 1 package IMPL::Config::Container;
2 use strict; 2 use strict;
3 3
4 use Scalar::Util qw(blessed);
4 use IMPL::Exception(); 5 use IMPL::Exception();
5 use IMPL::lang qw(:base); 6 use IMPL::lang qw(:base);
6 use IMPL::declare { 7 use IMPL::declare {
7 require => { 8 require => {
8 Descriptor => 'IMPL::Config::Descriptor', 9 Descriptor => 'IMPL::Config::Descriptor',
9 ActivationContext => 'IMPL::Config::ActivationContext', 10 ActivationContext => 'IMPL::Config::ActivationContext',
10 Hierarchy => 'IMPL::Config::Hierarchy', 11 Hierarchy => 'IMPL::Config::Hierarchy',
11 Bag => 'IMPL::Config::Bag', 12 Bag => 'IMPL::Config::Bag',
12 Loader => 'IMPL::Code::Loader' 13 Loader => 'IMPL::Code::Loader'
13 }, 14 },
14 base => [ 15 base => [
15 'IMPL::Object' => undef 16 'IMPL::Object' => undef,
16 ], 17 'IMPL::Object::Disposable' => undef
17 props => [ 18 ],
18 roles => 'r', 19 props => [
19 services => 'r', 20 roles => 'r',
20 instances => 'r', 21 services => 'r',
21 parent => 'r', 22 instances => 'r',
22 root => 'r', 23 parent => 'r',
23 loader => 'r' 24 root => 'r',
24 ] 25 loader => 'r'
26 ]
25 }; 27 };
26 28
27 my $nextRoleId = 1; 29 my $nextRoleId = 1;
28 30
29 sub CTOR { 31 sub CTOR {
30 my ( $this, $parent, %opts ) = @_; 32 my ( $this, $parent, %opts ) = @_;
31 33
32 $this->instances( {} ); 34 $this->instances( {} );
33 35
34 $this->loader( $opts{loader} || Loader->safe ); 36 $this->loader( $opts{loader} || Loader->safe );
35 37
36 if ($parent) { 38 if ($parent) {
37 $this->roles( Hierarchy->new( $parent->roles ) ); 39 $this->roles( Hierarchy->new( $parent->roles ) );
38 $this->services( Bag->new( $parent->services ) ); 40 $this->services( Bag->new( $parent->services ) );
39 $this->parent($parent); 41 $this->parent($parent);
40 $this->root( $parent->root ); 42 $this->root( $parent->root );
41 } 43 }
42 else { 44 else {
43 $this->roles( Hierarchy->new() ); 45 $this->roles( Hierarchy->new() );
44 $this->services( Bag->new() ); 46 $this->services( Bag->new() );
45 $this->root($this); 47 $this->root($this);
46 } 48 }
49 }
50
51 sub Dispose {
52 my ($this) = @_;
53
54 my $d;
55 foreach my $v ( values %{ $this->instances } ) {
56 if ( $d = blessed($v) && $v->can('Dispose') ) {
57 &$d($v);
58 }
59 }
47 } 60 }
48 61
49 sub Require { 62 sub Require {
50 my ($this, $class) = @_; 63 my ( $this, $class ) = @_;
51 64
52 return $this->loader->Require($class); 65 return $this->loader->Require($class);
53 } 66 }
54 67
55 sub Register { 68 sub Register {
56 my ( $this, $role, $service ) = @_; 69 my ( $this, $role, $service ) = @_;
57 70
58 die IMPL::InvalidArgumentException->new('service') 71 die IMPL::InvalidArgumentException->new('service')
59 unless is( $service, Descriptor ); 72 unless is( $service, Descriptor );
60 $this->services->Register( $this->GetLinearRoleHash($role), $service ); 73 $this->services->Register( $this->GetLinearRoleHash($role), $service );
61 } 74 }
62 75
63 sub GetLinearRoleHash { 76 sub GetLinearRoleHash {
64 my ( $this, $role ) = @_; 77 my ( $this, $role ) = @_;
65 78
66 die IMPL::InvalidArgumentException->new( 79 die IMPL::InvalidArgumentException->new(
67 role => 'The argument is required' ) 80 role => 'The argument is required' )
68 unless $role; 81 unless $role;
69 82
70 if ( isarray($role) ) { 83 if ( isarray($role) ) {
71 my $tempRole = "unnamed-" . $nextRoleId++; 84 my $tempRole = "unnamed-" . $nextRoleId++;
72 $this->roles->AddRole( $tempRole, $role ); 85 $this->roles->AddRole( $tempRole, $role );
73 $role = $tempRole; 86 $role = $tempRole;
74 } 87 }
75 88
76 $this->roles->GetLinearRoleHash($role); 89 $this->roles->GetLinearRoleHash($role);
77 } 90 }
78 91
79 sub Resolve { 92 sub Resolve {
80 my ( $this, $role, %opts ) = @_; 93 my ( $this, $role, %opts ) = @_;
81 94
82 my $descriptor = $this->services->Resolve($role); 95 my $descriptor = $this->services->Resolve($role);
83 96
84 return $descriptor->Activate( ActivationContext->new($this) ) 97 return $descriptor->Activate( ActivationContext->new($this) )
85 if $descriptor; 98 if $descriptor;
86 99
87 return $opts{default} if exists $opts{default}; 100 return $opts{default} if exists $opts{default};
88 } 101 }
89 102
90 sub ResolveAll { 103 sub ResolveAll {
91 my ( $this, $role, %opts ) = @_; 104 my ( $this, $role, %opts ) = @_;
92 105
93 my $all = $this->services->ResolveAll($role); 106 my $all = $this->services->ResolveAll($role);
94 107
95 my $context; 108 my $context;
96 109
97 my @result; 110 my @result;
98 111
99 foreach my $service (@$all) { 112 foreach my $service (@$all) {
100 $context = ActivationContext->new($this) 113 $context = ActivationContext->new($this)
101 unless $context || $opts{shared}; 114 unless $context || $opts{shared};
102 115
103 push @result, $service->Activate($context); 116 push @result, $service->Activate($context);
104 } 117 }
105 118
106 return \@result; 119 return \@result;
107 } 120 }
108
109 121
110 1; 122 1;
111 123
112 __END__ 124 __END__
113 125