forked from jasonmay/perl6-ioc
/
IoC.pm6
57 lines (45 loc) · 1.22 KB
/
IoC.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module IoC;
use IoC::Container;
use IoC::ConstructorInjection;
use IoC::BlockInjection;
use IoC::Literal;
my %containers;
my $container-name;
sub container($pair) is export {
%containers{$pair.key} = IoC::Container.new(
:name($pair.key),
);
$container-name = $pair.key;
unless $pair.value.^isa('Block') {
die "Second param must be invocable";
}
$pair.value.();
return %containers{$container-name};
}
sub contains(Block $sub) is export { return $sub }
sub service($pair) is export {
my %params = ('name' => $pair.key);
if $pair.value.^isa('Str') {
%params<value> = $pair.value;
}
else {
%params = (%params, $pair.value.pairs);
}
my $service-class;
if %params<block> {
$service-class = 'IoC::BlockInjection';
}
elsif %params<class> {
$service-class = 'IoC::ConstructorInjection';
}
elsif %params<value> {
$service-class = 'IoC::Literal';
}
else {
warn "Service {$pair.key} needs more parameters";
return;
}
my $service = eval("{$service-class}.new(|%params)");
#my $service = $service-class.new(|%params);
%containers{$container-name}.add-service($pair.key, $service);
}