annotate lib/IMPL/Config/Container.pm @ 419:bbc4739c4d48 ref20150831

working on IMPL::Config::Container
author cin
date Sun, 29 Jan 2017 10:30:20 +0300
parents 3f38dabaf5cc
children df591e3afd10
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 ActivationContext => 'IMPL::Config::ActivationContext',
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
10 Hierarchy => 'IMPL::Config::Hierarchy',
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
11 Bag => 'IMPL::Config::Bag',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
12 Loader => 'IMPL::Code::Loader'
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
13 },
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
14 base => [
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
15 'IMPL::Object' => undef
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
16 ],
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
17 props => [
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
18 roles => 'r',
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
19 services => 'r',
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
20 instances => 'r',
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
21 parent => 'r',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
22 root => 'r',
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
23 loader => 'r'
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
24 ]
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 my $nextRoleId = 1;
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
28
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
29 sub CTOR {
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
30 my ( $this, $parent, %opts ) = @_;
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
31
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
32 $this->instances( {} );
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
33
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
34 $this->loader( $opts{loader} || Loader->safe );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
35
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
36 if ($parent) {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
37 $this->roles( Hierarchy->new( $parent->roles ) );
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
38 $this->services( Bag->new( $parent->services ) );
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
39 $this->parent($parent);
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
40 $this->root( $parent->root );
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
41 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
42 else {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
43 $this->roles( Hierarchy->new() );
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
44 $this->services( Bag->new() );
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
45 $this->root($this);
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
46 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
47 }
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
48
417
3ed0c58e9da3 working on di container, tests
cin
parents: 415
diff changeset
49 sub Require {
3ed0c58e9da3 working on di container, tests
cin
parents: 415
diff changeset
50 my ($this, $class) = @_;
3ed0c58e9da3 working on di container, tests
cin
parents: 415
diff changeset
51
3ed0c58e9da3 working on di container, tests
cin
parents: 415
diff changeset
52 return $this->loader->Require($class);
3ed0c58e9da3 working on di container, tests
cin
parents: 415
diff changeset
53 }
3ed0c58e9da3 working on di container, tests
cin
parents: 415
diff changeset
54
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
55 sub Register {
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
56 my ( $this, $role, $service ) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
57
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
58 die IMPL::InvalidArgumentException->new('service')
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
59 unless is( $service, Descriptor );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
60 $this->services->Register( $this->GetLinearRoleHash($role), $service );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
61 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
62
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
63 sub GetLinearRoleHash {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
64 my ( $this, $role ) = @_;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
65
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
66 die IMPL::InvalidArgumentException->new(
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
67 role => 'The argument is required' )
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
68 unless $role;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
69
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
70 if ( isarray($role) ) {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
71 my $tempRole = "unnamed-" . $nextRoleId++;
417
3ed0c58e9da3 working on di container, tests
cin
parents: 415
diff changeset
72 $this->roles->AddRole( $tempRole, $role );
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
73 $role = $tempRole;
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
74 }
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
75
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
76 $this->roles->GetLinearRoleHash($role);
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
77 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
78
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
79 sub Resolve {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
80 my ( $this, $role, %opts ) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
81
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
82 my $descriptor = $this->services->Resolve($role);
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
83
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
84 return $descriptor->Activate( ActivationContext->new($this) )
417
3ed0c58e9da3 working on di container, tests
cin
parents: 415
diff changeset
85 if $descriptor;
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
86
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
87 return $opts{default} if exists $opts{default};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
88 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
89
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
90 sub ResolveAll {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
91 my ( $this, $role, %opts ) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
92
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
93 my $all = $this->services->ResolveAll($role);
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
94
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
95 my $context;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
96
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
97 my @result;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
98
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
99 foreach my $service (@$all) {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
100 $context = ActivationContext->new($this)
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
101 unless $context || $opts{shared};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
102
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
103 push @result, $service->Activate($context);
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 return \@result;
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
107 }
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108
418
cin
parents: 417
diff changeset
109
407
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