annotate lib/IMPL/Config/Container.pm @ 412:30e8c6a74937 ref20150831

working on di container (role based registrations)
author cin
date Mon, 21 Sep 2015 19:54:10 +0300
parents c6e90e02dd17
children af8d359ee4cc
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
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
4 use IMPL::lang qw(:base);
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
5 use IMPL::declare {
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
6 require => {
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
7 Descriptor => 'IMPL::Config::Descriptor'
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
8 },
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
9 base => [
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
10 'IMPL::Object' => undef
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
11 ],
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
12 props => [
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
13 roles => 'r',
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
14 services => 'r',
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
15 instances => 'r'
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 };
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
18
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
19 my $nextRoleId = 1;
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
20
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
21 use IMPL::Exception();
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 sub Register {
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
24 my ($this, $role, $service) = @_;
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 die IMPL::InvalidArgumentException->new(role => 'The argument is required') unless $role;
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
27 die IMPL::InvalidArgumentException->new('service') unless is($service, Descriptor);
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 if (isarray($role)) {
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
30 my $tempRole = "unnamed-" . $nextRoleId++;
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
31 $this->role->AddRole($tempRole, $role);
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
32 $role = $tempRole;
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
33 }
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
34
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
35 $this->services->Register($role, $service);
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
36
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
37 }
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 C<IMPL::Config::Container> - dependency injection container
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 =head2 METHODS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 =head3 GetService($serviceId)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 =item * $serviceId
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 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
60 for the class name it should be prefixed with C<@>, for example: C<@Foo::Bar>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64 The activation container maintains two maps, one for classes and the other for names.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 The first one is useful when we searching for an implementation the second one when
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 we need a particular service.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 =head3 RegisterService($descriptor)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69
412
30e8c6a74937 working on di container (role based registrations)
cin
parents: 407
diff changeset
70 =cut