Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c22b669
Showing
8 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Makefile | ||
blib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module IoC; | ||
|
||
use IoC::Container; | ||
|
||
my %cc; | ||
|
||
sub container($pair) is export { | ||
# XXX | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use IoC::Service; | ||
class IoC::BlockInjection does IoC::Service { | ||
has Sub $.block; | ||
has $.class; | ||
|
||
method get { | ||
if ($!lifecycle eq 'Singleton') { | ||
return ( | ||
$!instance ||= $!block.(); | ||
); | ||
} | ||
|
||
return $!block.(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use IoC::Service; | ||
class IoC::ConstructorInjection does IoC::Service { | ||
has Str $.class; | ||
has %.dependencies = (); | ||
has %.parameters; | ||
has $.container is rw; | ||
|
||
method get { | ||
if ($!lifecycle eq 'Singleton') { | ||
return ( | ||
$!instance ||= self.build-instance(); | ||
); | ||
} | ||
|
||
return self.build-instance(); | ||
} | ||
|
||
method build-instance { | ||
my %params; | ||
|
||
for %!dependencies.pairs -> $pair { | ||
%params{$pair.key} = $!container.fetch($pair.value).get(); | ||
}; | ||
|
||
return $!class.new(|%params); | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class IoC::Container { | ||
has %!services = (); | ||
|
||
method add-service($name, $service) { | ||
if $service.^can('container') { | ||
$service.container = self; | ||
} | ||
$service.name = $name; | ||
%!services{$name} = $service; | ||
} | ||
|
||
method fetch($service-name) { | ||
return %!services{$service-name}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use IoC::Service; | ||
|
||
class IoC::Literal does IoC::Service { | ||
has $.value; | ||
|
||
method get { return self.value } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
role IoC::Service { | ||
has Str $.name is rw; | ||
has Str $!lifecycle; | ||
|
||
# for singletons | ||
has Any $!instance; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
BEGIN { @*INC.push('lib') }; | ||
|
||
use IoC::Container; | ||
use IoC::ConstructorInjection; | ||
use IoC::BlockInjection; | ||
use Test; | ||
|
||
plan 4; | ||
|
||
my $c = IoC::Container.new(); | ||
|
||
class Bar {}; | ||
class Foo { has Bar $.bar; }; | ||
|
||
$c.add-service( | ||
'foo', IoC::ConstructorInjection.new( | ||
:class('Foo'), | ||
:lifecycle('Singleton'), | ||
:dependencies({ | ||
'bar' => 'bar', | ||
}), | ||
) | ||
); | ||
|
||
$c.add-service( | ||
'bar', IoC::BlockInjection.new( | ||
:class('Bar'), | ||
:lifecycle('Singleton'), | ||
:block(sub { | ||
return Bar.new; | ||
}), | ||
) | ||
); | ||
|
||
ok($c.fetch('foo').get); | ||
ok($c.fetch('bar').get); | ||
|
||
ok($c.fetch('foo').get.bar); | ||
is($c.fetch('foo').get.bar, $c.fetch('bar').get); |