annotate lib/IMPL/Config/Container.pm @ 418:3f38dabaf5cc ref20150831

sync
author cin
date Mon, 28 Dec 2015 15:11:35 +0300
parents 3ed0c58e9da3
children bbc4739c4d48
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 sub Configure {
cin
parents: 417
diff changeset
110 my ($this, $config) = @_;
cin
parents: 417
diff changeset
111
cin
parents: 417
diff changeset
112 if (isarray($config)) {
cin
parents: 417
diff changeset
113 $this->Configure($_) foreach @$config;
cin
parents: 417
diff changeset
114 } elsif (ishash($config)) {
cin
parents: 417
diff changeset
115
cin
parents: 417
diff changeset
116 } else {
cin
parents: 417
diff changeset
117 die IMPL::ArgumentException->new(config => 'Either a hash or an array is required');
cin
parents: 417
diff changeset
118 }
cin
parents: 417
diff changeset
119 }
cin
parents: 417
diff changeset
120
cin
parents: 417
diff changeset
121 sub ParseDescriptor {
cin
parents: 417
diff changeset
122 my ($this, $data) = @_;
cin
parents: 417
diff changeset
123
cin
parents: 417
diff changeset
124 if ()
cin
parents: 417
diff changeset
125 }
cin
parents: 417
diff changeset
126
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 C<IMPL::Config::Container> - dependency injection container
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 =head2 METHODS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
141 =head3 Resolve($role)
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
143 =head3 ResolveAll($role, shared => $useSharedContext)
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144
415
3d24b10dd0d5 working on IMPL::Config::Container
cin
parents: 414
diff changeset
145 =head3 Register($role, $service)
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
147 =cut