annotate lib/IMPL/Config/Container.pm @ 417:3ed0c58e9da3 ref20150831

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