Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 146 lines (101 sloc) 3.359 kb
c22b669 @jasonmay initial commit
authored
1 module IoC;
2
3 use IoC::Container;
92d76a8 @jasonmay ioc sugar
authored
4 use IoC::ConstructorInjection;
5 use IoC::BlockInjection;
ad635db @jasonmay support for literals
authored
6 use IoC::Literal;
c22b669 @jasonmay initial commit
authored
7
92d76a8 @jasonmay ioc sugar
authored
8 my %containers;
9 my $container-name;
c22b669 @jasonmay initial commit
authored
10
11 sub container($pair) is export {
92d76a8 @jasonmay ioc sugar
authored
12 %containers{$pair.key} = IoC::Container.new(
13 :name($pair.key),
14 );
15
16 $container-name = $pair.key;
17
20dcc11 @tadzik Fixes for nom branch, fix a few typos as well. All tests pass
tadzik authored
18 unless $pair.value ~~ Callable {
92d76a8 @jasonmay ioc sugar
authored
19 die "Second param must be invocable";
20 }
21
22 $pair.value.();
23
24 return %containers{$container-name};
25 }
26
27 sub contains(Block $sub) is export { return $sub }
28
29 sub service($pair) is export {
ad635db @jasonmay support for literals
authored
30 my %params = ('name' => $pair.key);
20dcc11 @tadzik Fixes for nom branch, fix a few typos as well. All tests pass
tadzik authored
31 if $pair.value ~~ Str {
ad635db @jasonmay support for literals
authored
32 %params<value> = $pair.value;
33 }
34 else {
35 %params = (%params, $pair.value.pairs);
36 }
92d76a8 @jasonmay ioc sugar
authored
37
438f493 @jasonmay cut out the middle-man to work around some strange p6 errors
authored
38 my $service;
e1d7d5c @tadzik Make IoC::Container hold a type object rather than a string, rename $!cl...
tadzik authored
39 if %params.exists(<block>) {
438f493 @jasonmay cut out the middle-man to work around some strange p6 errors
authored
40 $service = IoC::BlockInjection.new(|%params);
92d76a8 @jasonmay ioc sugar
authored
41 }
e1d7d5c @tadzik Make IoC::Container hold a type object rather than a string, rename $!cl...
tadzik authored
42 elsif %params.exists(<type>) {
438f493 @jasonmay cut out the middle-man to work around some strange p6 errors
authored
43 $service = IoC::ConstructorInjection.new(|%params);
92d76a8 @jasonmay ioc sugar
authored
44 }
e1d7d5c @tadzik Make IoC::Container hold a type object rather than a string, rename $!cl...
tadzik authored
45 elsif %params.exists(<value>) {
438f493 @jasonmay cut out the middle-man to work around some strange p6 errors
authored
46 $service = IoC::Literal.new(|%params);
ad635db @jasonmay support for literals
authored
47 }
92d76a8 @jasonmay ioc sugar
authored
48 else {
49 warn "Service {$pair.key} needs more parameters";
50 return;
51 }
52
53 %containers{$container-name}.add-service($pair.key, $service);
c22b669 @jasonmay initial commit
authored
54 }
4fa0b4c @jasonmay POD in IoC.pm6
authored
55
ba0248c @tadzik Bare =begin is not allowed, use =begin pod instead
tadzik authored
56 =begin pod
4fa0b4c @jasonmay POD in IoC.pm6
authored
57
58 =head1 NAME
59
60 IoC - Wire your application components together using inversion of control
61
62 =head1 SYNOPSIS
63
64 use IoC;
65
66 my $c = container 'myapp' => contains {
67
68 service 'logfile' => 'logfile.txt';
69
70 service 'logger' => {
866c442 @jasonmay flesh out doc
authored
71 'class' => 'MyLogger',
72 'lifecycle' => 'Singleton',
73 'dependencies' => {'logfile' => 'logfile'},
4fa0b4c @jasonmay POD in IoC.pm6
authored
74 };
75
76 service 'storage' => {
866c442 @jasonmay flesh out doc
authored
77 'lifecycle' => 'Singleton',
78 'block' => sub {
79 ...
4fa0b4c @jasonmay POD in IoC.pm6
authored
80 return MyStorage.new();
81 },
82 };
83
84 service 'app' => {
866c442 @jasonmay flesh out doc
authored
85 'class' => 'MyApp',
86 'lifecycle' => 'Signleton',
87 'dependencies' => {
4fa0b4c @jasonmay POD in IoC.pm6
authored
88 'logger' => 'logger',
89 'storage' => 'storage',
90 },
91 };
92
93 };
94
95 my $app = $c.resolve(service => 'app');
96 $app.run();
97
98 =head1 DESCRIPTION
99
8d1ff09 @jasonmay more docs more docs. come on more docs
authored
100 IoC is a port of stevan++'s Perl 5 module Bread::Board.
101
102 =head1 INVERSION OF CONTROL
103
104 Inversion of control is a way of keeping all your component creation logic in
105 one place. Instead of creating an object and explicitly pass it around everywhere,
106 one could just make a I<container> of all these components and allow the components to
107 cleanly interact with each other as I<services>.
4fa0b4c @jasonmay POD in IoC.pm6
authored
108
109 =head1 EXPORTED FUNCTIONS
110
111 =item B<container>
112
113 Creates a new L<IoC::Container> object. In the block you create your services.
114
115 =item B<service>
116
866c442 @jasonmay flesh out doc
authored
117 Adds services to your container, bringing your components together. See
118 C<IoC::Service> for more information on this.
119
4fa0b4c @jasonmay POD in IoC.pm6
authored
120 =head1 BUGS
121
122 All complex software has bugs lurking in it, and this module is no
123 exception. If you find a bug please either email me, or post an issue
124 to http://github.com/jasonmay/perl6-ioc/
125
126 =head1 REFERENCE
127
128 =item L<IoC::Container> - Container of all your application components
129
20dcc11 @tadzik Fixes for nom branch, fix a few typos as well. All tests pass
tadzik authored
130 =item L<IoC::Service> - Service representing a component in your application
4fa0b4c @jasonmay POD in IoC.pm6
authored
131
132 =head1 ACKNOWLEDGEMENTS
133
134 =item Thanks to Stevan Little who is the original author of Perl 5's Bread::Board
135
136 =head1 AUTHOR
137
03be070 @tadzik Remove Perl 5 Pod leftovers
tadzik authored
138 Jason May, <jason.a.may@gmail.com>
4fa0b4c @jasonmay POD in IoC.pm6
authored
139
140 =head1 LICENSE
141
142 This library is free software; you can redistribute it and/or modify
143 it under the same terms as Perl itself.
144
ba0248c @tadzik Bare =begin is not allowed, use =begin pod instead
tadzik authored
145 =end pod
Something went wrong with that request. Please try again.