annotate 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
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',
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
12 Bag => 'IMPL::Config::Bag'
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',
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
21 parent => 'r'
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
22 ]
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
23 };
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 my $nextRoleId = 1;
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
26
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
27 sub CTOR {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
28 my ( $this, $parent ) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
29
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
30 $this->instances( {} );
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
31
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
32 if ($parent) {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
33 $this->roles( Hierarchy->new( $parent->roles ) );
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
34 $this->services( Bag->new( $parent->services ) );
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
35 $this->parent($parent);
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
36 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
37 else {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
38 $this->roles( Hierarchy->new() );
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
39 $this->services( Bag->new() );
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
40 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
41 }
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
42
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
43 sub Register {
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
44 my ( $this, $role, $service ) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
45
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
46 die IMPL::InvalidArgumentException->new(
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
47 role => 'The argument is required' )
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
48 unless $role;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
49 die IMPL::InvalidArgumentException->new('service')
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
50 unless is( $service, Descriptor );
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
51
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
52 if ( isarray($role) ) {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
53 my $tempRole = "unnamed-" . $nextRoleId++;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
54 $this->role->AddRole( $tempRole, $role );
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
55 $role = $tempRole;
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
56 }
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
57
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
58 $service = ValueDescriptor->new( value => $service )
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
59 unless is( $service, Descriptor );
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
60
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
61 $this->services->Register( $role, $this->roles->GetLinearRoleHash($role), $service );
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
62 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
63
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
64 sub Resolve {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
65 my ( $this, $role, %opts ) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
66
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
67 my $descriptor = $this->services->Resolve($role);
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
68
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
69 return $descriptor->Activate( ActivationContext->new($this) )
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
70 if $descirptor;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
71
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
72 return $opts{default} if exists $opts{default};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
73 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
74
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
75 sub ResolveAll {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
76 my ( $this, $role, %opts ) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
77
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
78 my $all = $this->services->ResolveAll($role);
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
79
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
80 my $context;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
81
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
82 my @result;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
83
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
84 foreach my $service (@$all) {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
85 $context = ActivationContext->new($this)
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
86 unless $context || $opts{shared};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
87
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
88 push @result, $service->Activate($context);
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 return \@result;
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
92 }
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 C<IMPL::Config::Container> - dependency injection container
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 =head2 METHODS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 =head3 GetService($serviceId)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 =item * $serviceId
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 A string indetifier of the service, it can be in two forms: class name or service name,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 for the class name it should be prefixed with C<@>, for example: C<@Foo::Bar>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 The activation container maintains two maps, one for classes and the other for names.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 The first one is useful when we searching for an implementation the second one when
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 we need a particular service.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 =head3 RegisterService($descriptor)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
125 =cut