annotate lib/IMPL/Config/Container.pm @ 421:7798345304bc ref20150831

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