annotate lib/IMPL/Config/Container.pm @ 427:09e0086a82a7 ref20150831 tip

Merge
author cin
date Tue, 15 May 2018 00:51:33 +0300
parents b0481c071bea
children
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::Container;
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
2 use strict;
422
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
3 use mro;
420
cin
parents: 419
diff changeset
4 use Scalar::Util qw(blessed);
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
5 use IMPL::Exception();
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
6 use IMPL::lang qw(:base);
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
7 use IMPL::declare {
420
cin
parents: 419
diff changeset
8 require => {
cin
parents: 419
diff changeset
9 Descriptor => 'IMPL::Config::Descriptor',
cin
parents: 419
diff changeset
10 ActivationContext => 'IMPL::Config::ActivationContext',
cin
parents: 419
diff changeset
11 Hierarchy => 'IMPL::Config::Hierarchy',
cin
parents: 419
diff changeset
12 Bag => 'IMPL::Config::Bag',
cin
parents: 419
diff changeset
13 Loader => 'IMPL::Code::Loader'
cin
parents: 419
diff changeset
14 },
cin
parents: 419
diff changeset
15 base => [
cin
parents: 419
diff changeset
16 'IMPL::Object' => undef,
cin
parents: 419
diff changeset
17 'IMPL::Object::Disposable' => undef
cin
parents: 419
diff changeset
18 ],
cin
parents: 419
diff changeset
19 props => [
422
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
20 roles => 'r',
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
21 services => 'r',
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
22 serviceCache => 'r',
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
23 instances => 'r',
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
24 parent => 'r',
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
25 root => 'r',
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
26 loader => 'r'
420
cin
parents: 419
diff changeset
27 ]
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
28 };
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
29
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
30 my $nextRoleId = 1;
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
31
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
32 sub CTOR {
420
cin
parents: 419
diff changeset
33 my ( $this, $parent, %opts ) = @_;
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
34
420
cin
parents: 419
diff changeset
35 $this->instances( {} );
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
36
420
cin
parents: 419
diff changeset
37 $this->loader( $opts{loader} || Loader->safe );
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
38
420
cin
parents: 419
diff changeset
39 if ($parent) {
cin
parents: 419
diff changeset
40 $this->roles( Hierarchy->new( $parent->roles ) );
cin
parents: 419
diff changeset
41 $this->services( Bag->new( $parent->services ) );
cin
parents: 419
diff changeset
42 $this->parent($parent);
cin
parents: 419
diff changeset
43 $this->root( $parent->root );
cin
parents: 419
diff changeset
44 }
cin
parents: 419
diff changeset
45 else {
cin
parents: 419
diff changeset
46 $this->roles( Hierarchy->new() );
cin
parents: 419
diff changeset
47 $this->services( Bag->new() );
cin
parents: 419
diff changeset
48 $this->root($this);
cin
parents: 419
diff changeset
49 }
422
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
50
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
51 $this->services->tag($this);
420
cin
parents: 419
diff changeset
52 }
cin
parents: 419
diff changeset
53
cin
parents: 419
diff changeset
54 sub Dispose {
cin
parents: 419
diff changeset
55 my ($this) = @_;
cin
parents: 419
diff changeset
56
cin
parents: 419
diff changeset
57 my $d;
cin
parents: 419
diff changeset
58 foreach my $v ( values %{ $this->instances } ) {
cin
parents: 419
diff changeset
59 if ( $d = blessed($v) && $v->can('Dispose') ) {
cin
parents: 419
diff changeset
60 &$d($v);
cin
parents: 419
diff changeset
61 }
cin
parents: 419
diff changeset
62 }
422
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
63
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
64 $this->next::method();
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
65 }
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
66
417
3ed0c58e9da3 working on di container, tests
cin
parents: 415
diff changeset
67 sub Require {
420
cin
parents: 419
diff changeset
68 my ( $this, $class ) = @_;
cin
parents: 419
diff changeset
69
cin
parents: 419
diff changeset
70 return $this->loader->Require($class);
417
3ed0c58e9da3 working on di container, tests
cin
parents: 415
diff changeset
71 }
3ed0c58e9da3 working on di container, tests
cin
parents: 415
diff changeset
72
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
73 sub Register {
420
cin
parents: 419
diff changeset
74 my ( $this, $role, $service ) = @_;
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
75
420
cin
parents: 419
diff changeset
76 die IMPL::InvalidArgumentException->new('service')
cin
parents: 419
diff changeset
77 unless is( $service, Descriptor );
cin
parents: 419
diff changeset
78 $this->services->Register( $this->GetLinearRoleHash($role), $service );
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
79 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
80
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
81 sub GetLinearRoleHash {
420
cin
parents: 419
diff changeset
82 my ( $this, $role ) = @_;
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
83
420
cin
parents: 419
diff changeset
84 die IMPL::InvalidArgumentException->new(
cin
parents: 419
diff changeset
85 role => 'The argument is required' )
cin
parents: 419
diff changeset
86 unless $role;
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
87
420
cin
parents: 419
diff changeset
88 if ( isarray($role) ) {
cin
parents: 419
diff changeset
89 my $tempRole = "unnamed-" . $nextRoleId++;
cin
parents: 419
diff changeset
90 $this->roles->AddRole( $tempRole, $role );
cin
parents: 419
diff changeset
91 $role = $tempRole;
cin
parents: 419
diff changeset
92 }
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
93
420
cin
parents: 419
diff changeset
94 $this->roles->GetLinearRoleHash($role);
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
95 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
96
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
97 sub Resolve {
422
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
98 my ( $this, $role ) = @_;
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
99
420
cin
parents: 419
diff changeset
100 my $descriptor = $this->services->Resolve($role);
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
101
422
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
102 return ActivationContext->new($this)->Activate($descriptor)
420
cin
parents: 419
diff changeset
103 if $descriptor;
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
104 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
105
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
106 sub ResolveAll {
420
cin
parents: 419
diff changeset
107 my ( $this, $role, %opts ) = @_;
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
108
420
cin
parents: 419
diff changeset
109 my $all = $this->services->ResolveAll($role);
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
110
420
cin
parents: 419
diff changeset
111 my $context;
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
112
420
cin
parents: 419
diff changeset
113 my @result;
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
114
420
cin
parents: 419
diff changeset
115 foreach my $service (@$all) {
cin
parents: 419
diff changeset
116 $context = ActivationContext->new($this)
421
7798345304bc working on IMPL::Config, removed old stuff
cin
parents: 420
diff changeset
117 unless $context && $opts{shared};
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
118
422
b0481c071bea IMPL::Config::Container tests, YAMLConfiguration now works and tested
cin
parents: 421
diff changeset
119 push @result, $context->Activate($service);
420
cin
parents: 419
diff changeset
120 }
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
121
420
cin
parents: 419
diff changeset
122 return \@result;
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
123 }
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 C<IMPL::Config::Container> - dependency injection container
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 =head2 METHODS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
139 =head3 Resolve($role)
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
141 =head3 ResolveAll($role, shared => $useSharedContext)
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
143 =head3 Register($role, $service)
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
145 =cut