annotate lib/IMPL/Config/Container.pm @ 415:3d24b10dd0d5 ref20150831

working on IMPL::Config::Container
author cin
date Tue, 20 Oct 2015 07:32:55 +0300
parents ec6f2d389d1e
children 3ed0c58e9da3
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
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
50 sub Register {
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
51 my ( $this, $role, $service ) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
52
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
53 die IMPL::InvalidArgumentException->new('service')
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
54 unless is( $service, Descriptor );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
55 $this->services->Register( $this->GetLinearRoleHash($role), $service );
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
56 }
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
57
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
58 sub GetLinearRoleHash {
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
59 my ( $this, $role ) = @_;
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
60
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
61 die IMPL::InvalidArgumentException->new(
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
62 role => 'The argument is required' )
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
63 unless $role;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
64
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
65 if ( isarray($role) ) {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
66 my $tempRole = "unnamed-" . $nextRoleId++;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
67 $this->role->AddRole( $tempRole, $role );
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
68 $role = $tempRole;
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
69 }
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
70
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
71 $this->roles->GetLinearRoleHash($role);
413
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
72 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
73
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
74 sub Resolve {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
75 my ( $this, $role, %opts ) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
76
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
77 my $descriptor = $this->services->Resolve($role);
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
78
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
79 return $descriptor->Activate( ActivationContext->new($this) )
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
80 if $descirptor;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
81
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
82 return $opts{default} if exists $opts{default};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
83 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
84
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
85 sub ResolveAll {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
86 my ( $this, $role, %opts ) = @_;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
87
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
88 my $all = $this->services->ResolveAll($role);
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
89
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
90 my $context;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
91
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
92 my @result;
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
93
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
94 foreach my $service (@$all) {
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
95 $context = ActivationContext->new($this)
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
96 unless $context || $opts{shared};
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
97
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
98 push @result, $service->Activate($context);
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
99 }
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
100
af8d359ee4cc working on di container
cin
parents: 412
diff changeset
101 return \@result;
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
102 }
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 C<IMPL::Config::Container> - dependency injection container
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 =head2 METHODS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
118 =head3 Resolve($role)
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
120 =head3 ResolveAll($role, shared => $useSharedContext)
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
122 =head3 Register($role, $service)
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
124 =cut