| 1 | package Bread::Board; | ||||||||||||||||||||||||||||||||
| 2 | 54 | our $AUTHORITY = 'cpan:STEVAN'; | |||||||||||||||||||||||||||||||
| 3 | 54 | $Bread::Board::VERSION = '0.35'; | |||||||||||||||||||||||||||||||
| 4 | 54 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 54 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 54 | ▶ | use Carp qw(confess); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 54 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | # ABSTRACT: A solderless way to wire up your application components | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 54 | ▶ | use Bread::Board::Types; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 54 | ▶ | use Bread::Board::ConstructorInjection; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 54 | ▶ | use Bread::Board::SetterInjection; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 54 | ▶ | use Bread::Board::BlockInjection; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 54 | ▶ | use Bread::Board::Literal; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 54 | ▶ | use Bread::Board::Container; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 54 | ▶ | use Bread::Board::Container::Parameterized; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 54 | ▶ | use Bread::Board::Dependency; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 54 | ▶ | use Bread::Board::LifeCycle::Singleton; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 54 | ▶ | use Bread::Board::Service::Inferred; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | 54 | ▶ | use Bread::Board::Service::Alias; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 54 | ▶ | use Exporter qw( import ); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 54 | our @EXPORT = qw[ | |||||||||||||||||||||||||||||||
| 25 | as | ||||||||||||||||||||||||||||||||
| 26 | container | ||||||||||||||||||||||||||||||||
| 27 | depends_on | ||||||||||||||||||||||||||||||||
| 28 | service | ||||||||||||||||||||||||||||||||
| 29 | alias | ||||||||||||||||||||||||||||||||
| 30 | wire_names | ||||||||||||||||||||||||||||||||
| 31 | include | ||||||||||||||||||||||||||||||||
| 32 | typemap | ||||||||||||||||||||||||||||||||
| 33 | infer | ||||||||||||||||||||||||||||||||
| 34 | literal | ||||||||||||||||||||||||||||||||
| 35 | ]; | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | # This is a chopped down version of what's in Moose::Exporter. It'd be nice if | ||||||||||||||||||||||||||||||||
| 38 | # there was something on CPAN that implemented this without all the additional | ||||||||||||||||||||||||||||||||
| 39 | # Moose::Exporter baggage (particularly the enabling of strict & warnings). | ||||||||||||||||||||||||||||||||
| 40 | sub unimport { | ||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | my $package = caller(); | ||||||||||||||||||||||||||||||
|
Subroutine
unimport: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | 1 | foreach my $name (@EXPORT) { | |||||||||||||||||||||||||||||||
| 44 | 54 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 45 | 10 | ▶ | next unless defined &{ $package . '::' . $name }; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | 10 | my $sub = \&{ $package . '::' . $name }; | |||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | 10 | ▶ | next unless $sub eq __PACKAGE__->can($name); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 10 | delete ${ $package . '::' }{$name}; | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 116 | ▶ | sub as (&) { $_[0] } | ||||||||||||||||||||||||||||||
|
Subroutine
as: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 54 | our $CC; | |||||||||||||||||||||||||||||||
| 58 | 54 | our $in_container; | |||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | sub set_root_container { | ||||||||||||||||||||||||||||||||
| 61 | 5 | ▶ | confess "Can't set the root container when we're already in a container" | ||||||||||||||||||||||||||||||
|
Subroutine
set_root_container: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 62 | if $in_container; | ||||||||||||||||||||||||||||||||
| 63 | 4 | $CC = shift; | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | sub container ($;$$) { | ||||||||||||||||||||||||||||||||
| 67 | 124 | ▶ | my $name = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
container: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | 124 | my $c; | |||||||||||||||||||||||||||||||
| 70 | 124 | ▶ | if (blessed $name) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | 15 | ▶ | confess 'an object used as a container must inherit from Bread::Board::Container or Bread::Board::Container::Parameterized' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$name->isa("Bread::Board::Container") or $name->isa("Bread::Board::Container::Parameterized")
MC/DC100%2/2
$name->isa("Bread::Board::Container") or $name->isa("Bread::Board::Container::Parameterized")
$name->isa("Bread::Board::Container")
$name->isa("Bread::Board::Container::Parameterized")
Truth table100%3/3
A or B
A= $name->isa("Bread::Board::Container")B= $name->isa("Bread::Board::Container::Parameterized")
| |||||||||||||||||||||||||||||||||
| 72 | unless $name->isa('Bread::Board::Container') || $name->isa('Bread::Board::Container::Parameterized'); | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 14 | ▶ | confess 'container($object, ...) is not supported for parameterized containers' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 75 | if scalar @_ > 1; | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | # this is basically: | ||||||||||||||||||||||||||||||||
| 78 | # container( A::Bread::Board::Container->new, ... ) | ||||||||||||||||||||||||||||||||
| 79 | # or someone using &container as a constructor | ||||||||||||||||||||||||||||||||
| 80 | 13 | $c = $name; | |||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | # if we're in the context of another container | ||||||||||||||||||||||||||||||||
| 83 | # then we're a subcontainer of it | ||||||||||||||||||||||||||||||||
| 84 | 13 | ▶ | $CC->add_sub_container($c) if defined $CC; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | else { | ||||||||||||||||||||||||||||||||
| 87 | 109 | my $is_inheriting = $name =~ s/^\+//; | |||||||||||||||||||||||||||||||
| 88 | 109 | ▶ | confess "Inheriting containers isn't possible outside of the context of a container" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$is_inheriting and not defined $Bread::Board::CC
MC/DC100%2/2
$is_inheriting and not defined $Bread::Board::CC
$is_inheriting
not defined $Bread::Board::CC
Truth table100%3/3
A and not(B)
A= $is_inheritingB= not defined $Bread::Board::CC
| |||||||||||||||||||||||||||||||||
| 89 | if $is_inheriting && !defined $CC; | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | # if we have more than 1 argument, then we are a parameterized | ||||||||||||||||||||||||||||||||
| 92 | # container, so we need to act accordingly | ||||||||||||||||||||||||||||||||
| 93 | 108 | ▶ | if (scalar @_ > 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 94 | 11 | ▶ | confess 'Declaring container parameters when inheriting is not supported' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | if $is_inheriting; | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 10 | my $param_names = shift; | |||||||||||||||||||||||||||||||
| 98 | 10 | $c = Bread::Board::Container::Parameterized->new({ | |||||||||||||||||||||||||||||||
| 99 | name => $name, | ||||||||||||||||||||||||||||||||
| 100 | allowed_parameter_names => $param_names, | ||||||||||||||||||||||||||||||||
| 101 | }); | ||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||||||
| 104 | 97 | ▶ | $c = $is_inheriting | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 105 | ? $CC->fetch($name) | ||||||||||||||||||||||||||||||||
| 106 | : Bread::Board::Container->new({ name => $name }); | ||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | # if we're in the context of another container | ||||||||||||||||||||||||||||||||
| 110 | # then we're a subcontainer of it, unless we're inheriting, | ||||||||||||||||||||||||||||||||
| 111 | # in which case we already got a parent | ||||||||||||||||||||||||||||||||
| 112 | 106 | ▶ | $CC->add_sub_container($c) if !$is_inheriting && defined $CC; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $is_inheriting and defined $Bread::Board::CC
MC/DC100%2/2
not $is_inheriting and defined $Bread::Board::CC
not $is_inheriting
defined $Bread::Board::CC
Truth table100%3/3
not(A) and B
A= not $is_inheritingB= defined $Bread::Board::CC
| |||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 119 | my $body = shift; | |||||||||||||||||||||||||||||||
| 116 | # if we have more arguments | ||||||||||||||||||||||||||||||||
| 117 | # then they are likely a body | ||||||||||||||||||||||||||||||||
| 118 | # and so we should execute it | ||||||||||||||||||||||||||||||||
| 119 | 119 | ▶ | if (defined $body) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | 113 | local $_ = $c; | |||||||||||||||||||||||||||||||
| 121 | 113 | local $CC = $c; | |||||||||||||||||||||||||||||||
| 122 | 113 | local $in_container = 1; | |||||||||||||||||||||||||||||||
| 123 | 113 | $body->($c); | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 107 | return $c; | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | sub include ($) { | ||||||||||||||||||||||||||||||||
| 130 | 6 | ▶ | my $file = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
include: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 131 | 6 | ▶ | if (my $ret = do $file) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 132 | 3 | return $ret; | |||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||||||
| 135 | 3 | ▶ | confess "Couldn't compile $file: $@" if $@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 136 | 2 | ▶ | confess "Couldn't open $file for reading: $!" if $!; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 137 | 1 | confess "Unknown error when compiling $file " | |||||||||||||||||||||||||||||||
| 138 | . "(or $file doesn't return a true value)"; | ||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | sub service ($@) { | ||||||||||||||||||||||||||||||||
| 143 | 167 | ▶ | my $name = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
service: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||||||
| 144 | 167 | my $s; | |||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | 167 | my $is_inheriting = ($name =~ s/^\+//); | |||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | 167 | ▶ | if (scalar @_ == 1) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 149 | 56 | ▶ | confess "Service inheritance doesn't make sense for literal services" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 150 | if $is_inheriting; | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | 55 | $s = Bread::Board::Literal->new(name => $name, value => $_[0]); | |||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||
| 154 | elsif (scalar(@_) % 2 == 0) { | ||||||||||||||||||||||||||||||||
| 155 | 110 | my %params = @_; | |||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | 110 | my $class = $params{service_class}; | |||||||||||||||||||||||||||||||
| 158 | $class ||= defined $params{service_type} ? "Bread::Board::$params{service_type}Injection" | ||||||||||||||||||||||||||||||||
| 159 | 110 | ▶ | : exists $params{block} ? 'Bread::Board::BlockInjection' | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$class ||= defined $params{"service_type"} ? qq[Bread::Board::$params{"service_type"}Injection] : (exists $params{"block"} ? "Bread::Board::BlockInjection" : "Bread::Board::ConstructorInjection")
MC/DC0%0/2
$class ||= defined $params{"service_type"} ? qq[Bread::Board::$params{"service_type"}Injection] : (exists $params{"block"} ? "Bread::Board::BlockInjection" : "Bread::Board::ConstructorInjection")
$class
defined $params{"service_type"} ? qq[Bread::Board::$params{"service_type"}Injection] : (exists $params{"block"} ? "Bread::Board::BlockInjection" : "Bread::Board::ConstructorInjection")
Truth table66%2/3
A ||= B
A= $classB= defined $params{"service_type"} ? qq[Bread::Board::$params{"service_type"}Injection] : (exists $params{"block"} ? "Bread::Board::BlockInjection" : "Bread::Board::ConstructorInjection")
| |||||||||||||||||||||||||||||||||
| 160 | : 'Bread::Board::ConstructorInjection'; | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | 110 | ▶ | $class->does('Bread::Board::Service') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 163 | or confess "The service class must do the Bread::Board::Service role"; | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 109 | ▶ | if ($is_inheriting) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 166 | 9 | ▶ | confess "Inheriting services isn't possible outside of the context of a container" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 167 | unless defined $CC; | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | 9 | ▶ | my $container = ($CC->isa('Bread::Board::Container::Parameterized') ? $CC->container : $CC); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 170 | 9 | my $prototype_service = $container->fetch($name); | |||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | 9 | ▶ | confess sprintf( | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 173 | "Trying to inherit from service '%s', but found a %s", | ||||||||||||||||||||||||||||||||
| 174 | $name, blessed $prototype_service, | ||||||||||||||||||||||||||||||||
| 175 | ) unless $prototype_service->does('Bread::Board::Service'); | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | 8 | $s = $prototype_service->clone_and_inherit_params( | |||||||||||||||||||||||||||||||
| 178 | service_class => $class, | ||||||||||||||||||||||||||||||||
| 179 | %params, | ||||||||||||||||||||||||||||||||
| 180 | ); | ||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||
| 182 | else { | ||||||||||||||||||||||||||||||||
| 183 | 100 | $s = $class->new(name => $name, %params); | |||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||
| 186 | else { | ||||||||||||||||||||||||||||||||
| 187 | 1 | confess "A service is defined by a name and either a single value or hash of parameters; you have supplied neither"; | |||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||
| 189 | 158 | ▶ | return $s unless defined $CC; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 190 | 155 | $CC->add_service($s); | |||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | sub alias ($$@) { | ||||||||||||||||||||||||||||||||
| 194 | 24 | ▶ | my $name = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
alias: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 195 | 24 | my $path = shift; | |||||||||||||||||||||||||||||||
| 196 | 24 | my %params = @_; | |||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | 24 | my $s = Bread::Board::Service::Alias->new( | |||||||||||||||||||||||||||||||
| 199 | name => $name, | ||||||||||||||||||||||||||||||||
| 200 | aliased_from_path => $path, | ||||||||||||||||||||||||||||||||
| 201 | %params, | ||||||||||||||||||||||||||||||||
| 202 | ); | ||||||||||||||||||||||||||||||||
| 203 | 24 | ▶ | return $s unless defined $CC; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 204 | 23 | $CC->add_service($s); | |||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | sub typemap ($@) { | ||||||||||||||||||||||||||||||||
| 208 | 33 | ▶ | my $type = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
typemap: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | 33 | ▶ | (scalar @_ == 1) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 211 | || confess "typemap takes a single argument"; | ||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | 32 | my $service; | |||||||||||||||||||||||||||||||
| 214 | 32 | ▶ | if (blessed $_[0]) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 215 | 26 | ▶ | if ($_[0]->does('Bread::Board::Service')) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 216 | 1 | $service = $_[0]; | |||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||
| 218 | elsif ($_[0]->isa('Bread::Board::Service::Inferred')) { | ||||||||||||||||||||||||||||||||
| 219 | 24 | $service = $_[0]->infer_service( $type ); | |||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||
| 221 | else { | ||||||||||||||||||||||||||||||||
| 222 | 1 | confess $_[0] . " isn't a service"; | |||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||
| 225 | else { | ||||||||||||||||||||||||||||||||
| 226 | 6 | $service = $CC->fetch( $_[0] ); | |||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | 29 | $CC->add_type_mapping_for( $type, $service ); | |||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||
| 232 | sub infer { | ||||||||||||||||||||||||||||||||
| 233 | 24 | ▶ | if (@_ == 1) { | ||||||||||||||||||||||||||||||
|
Subroutine
infer: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 234 | 1 | return Bread::Board::Service::Inferred->new( | |||||||||||||||||||||||||||||||
| 235 | current_container => $CC, | ||||||||||||||||||||||||||||||||
| 236 | service => $_[0], | ||||||||||||||||||||||||||||||||
| 237 | infer_params => 1, | ||||||||||||||||||||||||||||||||
| 238 | ); | ||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||
| 240 | else { | ||||||||||||||||||||||||||||||||
| 241 | 23 | my %params = @_; | |||||||||||||||||||||||||||||||
| 242 | 23 | return Bread::Board::Service::Inferred->new( | |||||||||||||||||||||||||||||||
| 243 | current_container => $CC, | ||||||||||||||||||||||||||||||||
| 244 | service_args => \%params, | ||||||||||||||||||||||||||||||||
| 245 | infer_params => 1, | ||||||||||||||||||||||||||||||||
| 246 | ); | ||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||
| 250 | 2 | ▶ | sub wire_names { +{ map { $_ => depends_on($_) } @_ }; } | ||||||||||||||||||||||||||||||
|
Subroutine
wire_names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||
| 252 | sub depends_on ($) { | ||||||||||||||||||||||||||||||||
| 253 | 65 | ▶ | my $path = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
depends_on: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 254 | 65 | Bread::Board::Dependency->new(service_path => $path); | |||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||
| 257 | 54 | my $LITERAL_ANON = 0; | |||||||||||||||||||||||||||||||
| 258 | sub literal($) { | ||||||||||||||||||||||||||||||||
| 259 | 2 | ▶ | my $value = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
literal: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 260 | 2 | Bread::Board::Literal->new( | |||||||||||||||||||||||||||||||
| 261 | name => 'LITERAL_ANON_' . $LITERAL_ANON++, | ||||||||||||||||||||||||||||||||
| 262 | value => $value, | ||||||||||||||||||||||||||||||||
| 263 | ); | ||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||
| 266 | 54 | 1; | |||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||
| 268 | __END__ | ||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||
| 270 | =pod | ||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||
| 272 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||
| 276 | Bread::Board - A solderless way to wire up your application components | ||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||
| 278 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||
| 280 | version 0.35 | ||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||
| 282 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | use Bread::Board; | ||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||
| 286 | my $c = container 'MyApp' => as { | ||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||
| 288 | service 'log_file_name' => "logfile.log"; | ||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||
| 290 | service 'logger' => ( | ||||||||||||||||||||||||||||||||
| 291 | class => 'FileLogger', | ||||||||||||||||||||||||||||||||
| 292 | lifecycle => 'Singleton', | ||||||||||||||||||||||||||||||||
| 293 | dependencies => [ 'log_file_name' ], | ||||||||||||||||||||||||||||||||
| 294 | ] | ||||||||||||||||||||||||||||||||
| 295 | ); | ||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||
| 297 | container 'Database' => as { | ||||||||||||||||||||||||||||||||
| 298 | service 'dsn' => "dbi:SQLite:dbname=my-app.db"; | ||||||||||||||||||||||||||||||||
| 299 | service 'username' => "user234"; | ||||||||||||||||||||||||||||||||
| 300 | service 'password' => "****"; | ||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||
| 302 | service 'dbh' => ( | ||||||||||||||||||||||||||||||||
| 303 | block => sub { | ||||||||||||||||||||||||||||||||
| 304 | my $s = shift; | ||||||||||||||||||||||||||||||||
| 305 | require DBI; | ||||||||||||||||||||||||||||||||
| 306 | DBI->connect( | ||||||||||||||||||||||||||||||||
| 307 | $s->param('dsn'), | ||||||||||||||||||||||||||||||||
| 308 | $s->param('username'), | ||||||||||||||||||||||||||||||||
| 309 | $s->param('password'), | ||||||||||||||||||||||||||||||||
| 310 | ) || die "Could not connect"; | ||||||||||||||||||||||||||||||||
| 311 | }, | ||||||||||||||||||||||||||||||||
| 312 | dependencies => [ 'dsn', 'username', 'password' ] | ||||||||||||||||||||||||||||||||
| 313 | ); | ||||||||||||||||||||||||||||||||
| 314 | }; | ||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||
| 316 | service 'application' => ( | ||||||||||||||||||||||||||||||||
| 317 | class => 'MyApplication', | ||||||||||||||||||||||||||||||||
| 318 | dependencies => { | ||||||||||||||||||||||||||||||||
| 319 | logger => 'logger', | ||||||||||||||||||||||||||||||||
| 320 | dbh => 'Database/dbh', | ||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||
| 322 | ); | ||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||
| 324 | }; | ||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | no Bread::Board; # removes keywords | ||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||
| 328 | # get an instance of MyApplication | ||||||||||||||||||||||||||||||||
| 329 | # from the container | ||||||||||||||||||||||||||||||||
| 330 | my $app = $c->resolve( service => 'application' ); | ||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||
| 332 | # now user your MyApplication | ||||||||||||||||||||||||||||||||
| 333 | # as you normally would ... | ||||||||||||||||||||||||||||||||
| 334 | $app->run; | ||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||
| 336 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||
| 338 | Bread::Board is an inversion of control framework with a focus on | ||||||||||||||||||||||||||||||||
| 339 | dependency injection and lifecycle management. It's goal is to | ||||||||||||||||||||||||||||||||
| 340 | help you write more decoupled objects and components by removing | ||||||||||||||||||||||||||||||||
| 341 | the need for you to manually wire those objects/components together. | ||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||
| 343 | Want to know more? See the L<Bread::Board::Manual>. | ||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||
| 345 | +-----------------------------------------+ | ||||||||||||||||||||||||||||||||
| 346 | | A B C D E F G H I J | | ||||||||||||||||||||||||||||||||
| 347 | |-----------------------------------------| | ||||||||||||||||||||||||||||||||
| 348 | | o o | 1 o-o-o-o-o v o-o-o-o-o 1 | o o | | ||||||||||||||||||||||||||||||||
| 349 | | o o | 2 o-o-o-o-o o-o-o-o-o 2 | o o | | ||||||||||||||||||||||||||||||||
| 350 | | o o | 3 o-o-o-o-o o-o-o-o-o 3 | o o | | ||||||||||||||||||||||||||||||||
| 351 | | o o | 4 o-o-o-o-o o-o-o-o-o 4 | o o | | ||||||||||||||||||||||||||||||||
| 352 | | o o | 5 o-o-o-o-o o-o-o-o-o 5 | o o | | ||||||||||||||||||||||||||||||||
| 353 | | | 6 o-o-o-o-o o-o-o-o-o 6 | | | ||||||||||||||||||||||||||||||||
| 354 | | o o | 7 o-o-o-o-o o-o-o-o-o 7 | o o | | ||||||||||||||||||||||||||||||||
| 355 | | o o | 8 o-o-o-o-o o-o-o-o-o 8 | o o | | ||||||||||||||||||||||||||||||||
| 356 | | o o | 9 o-o-o-o-o o-o-o-o-o 9 | o o | | ||||||||||||||||||||||||||||||||
| 357 | | o o | 10 o-o-o-o-o o-o-o-o-o 10 | o o | | ||||||||||||||||||||||||||||||||
| 358 | | o o | 11 o-o-o-o-o o-o-o-o-o 11 | o o | | ||||||||||||||||||||||||||||||||
| 359 | | | 12 o-o-o-o-o o-o-o-o-o 12 | | | ||||||||||||||||||||||||||||||||
| 360 | | o o | 13 o-o-o-o-o o-o-o-o-o 13 | o o | | ||||||||||||||||||||||||||||||||
| 361 | | o o | 14 o-o-o-o-o o-o-o-o-o 14 | o o | | ||||||||||||||||||||||||||||||||
| 362 | | o o | 15 o-o-o-o-o o-o-o-o-o 15 | o o | | ||||||||||||||||||||||||||||||||
| 363 | | o o | 16 o-o-o-o-o o-o-o-o-o 16 | o o | | ||||||||||||||||||||||||||||||||
| 364 | | o o | 17 o-o-o-o-o o-o-o-o-o 17 | o o | | ||||||||||||||||||||||||||||||||
| 365 | | | 18 o-o-o-o-o o-o-o-o-o 18 | | | ||||||||||||||||||||||||||||||||
| 366 | | o o | 19 o-o-o-o-o o-o-o-o-o 19 | o o | | ||||||||||||||||||||||||||||||||
| 367 | | o o | 20 o-o-o-o-o o-o-o-o-o 20 | o o | | ||||||||||||||||||||||||||||||||
| 368 | | o o | 21 o-o-o-o-o o-o-o-o-o 21 | o o | | ||||||||||||||||||||||||||||||||
| 369 | | o o | 22 o-o-o-o-o o-o-o-o-o 22 | o o | | ||||||||||||||||||||||||||||||||
| 370 | | o o | 22 o-o-o-o-o o-o-o-o-o 22 | o o | | ||||||||||||||||||||||||||||||||
| 371 | | | 23 o-o-o-o-o o-o-o-o-o 23 | | | ||||||||||||||||||||||||||||||||
| 372 | | o o | 24 o-o-o-o-o o-o-o-o-o 24 | o o | | ||||||||||||||||||||||||||||||||
| 373 | | o o | 25 o-o-o-o-o o-o-o-o-o 25 | o o | | ||||||||||||||||||||||||||||||||
| 374 | | o o | 26 o-o-o-o-o o-o-o-o-o 26 | o o | | ||||||||||||||||||||||||||||||||
| 375 | | o o | 27 o-o-o-o-o o-o-o-o-o 27 | o o | | ||||||||||||||||||||||||||||||||
| 376 | | o o | 28 o-o-o-o-o ^ o-o-o-o-o 28 | o o | | ||||||||||||||||||||||||||||||||
| 377 | +-----------------------------------------+ | ||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||
| 379 | Loading this package will automatically load the rest of the packages needed by | ||||||||||||||||||||||||||||||||
| 380 | your Bread::Board configuration. | ||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | =head1 EXPORTED FUNCTIONS | ||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||
| 384 | The functions of this package provide syntactic sugar to help you build your | ||||||||||||||||||||||||||||||||
| 385 | Bread::Board configuration. You can build such a configuration by constructing | ||||||||||||||||||||||||||||||||
| 386 | the objects manually instead, but your code may be more difficult to | ||||||||||||||||||||||||||||||||
| 387 | understand. | ||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||
| 389 | =head2 C<container> | ||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||
| 391 | =head3 simple case | ||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||
| 393 | container $name, \&body; | ||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||
| 395 | This function constructs and returns an instance of L<Bread::Board::Container>. | ||||||||||||||||||||||||||||||||
| 396 | The (optional) C<&body> block may be used to add services or sub-containers | ||||||||||||||||||||||||||||||||
| 397 | within the newly constructed container. Usually, the block is not passed | ||||||||||||||||||||||||||||||||
| 398 | directly, but passed using the C<as> function. | ||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||
| 400 | For example, | ||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||
| 402 | container 'MyWebApp' => as { | ||||||||||||||||||||||||||||||||
| 403 | service my_dispatcher => ( | ||||||||||||||||||||||||||||||||
| 404 | class => 'MyWebApp::Dispatcher', | ||||||||||||||||||||||||||||||||
| 405 | ); | ||||||||||||||||||||||||||||||||
| 406 | }; | ||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||
| 408 | If C<$name> starts with C<'+'>, and the container is being declared inside | ||||||||||||||||||||||||||||||||
| 409 | another container, then this declaration will instead extend an existing | ||||||||||||||||||||||||||||||||
| 410 | container with the name C<$name> (without the C<'+'>). | ||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||
| 412 | =head3 from an instance | ||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||
| 414 | container $container_instance, \&body | ||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||
| 416 | In many cases, subclassing L<Bread::Board::Container> is the easiest route to | ||||||||||||||||||||||||||||||||
| 417 | getting access to this framework. You can do this and still get all the | ||||||||||||||||||||||||||||||||
| 418 | benefits of the syntactic sugar for configuring that class by passing an | ||||||||||||||||||||||||||||||||
| 419 | instance of your container subclass to C<container>. | ||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||
| 421 | You could, for example, configure your container inside the C<BUILD> method of | ||||||||||||||||||||||||||||||||
| 422 | your class: | ||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||
| 424 | package MyWebApp; | ||||||||||||||||||||||||||||||||
| 425 | use Moose; | ||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||
| 427 | extends 'Bread::Board::Container'; | ||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||
| 429 | sub BUILD { | ||||||||||||||||||||||||||||||||
| 430 | my $self = shift; | ||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||
| 432 | container $self => as { | ||||||||||||||||||||||||||||||||
| 433 | service dbh => ( ... ); | ||||||||||||||||||||||||||||||||
| 434 | }; | ||||||||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||
| 437 | =head3 with parameters | ||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||
| 439 | container $name, \@parameters, \&body | ||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||
| 441 | A third way of using the C<container> function is to build a parameterized | ||||||||||||||||||||||||||||||||
| 442 | container. These are useful as a way of providing a placeholder for parts of | ||||||||||||||||||||||||||||||||
| 443 | the configuration that may be provided later. You may not use an instance | ||||||||||||||||||||||||||||||||
| 444 | object in place of the C<$name> in this case. | ||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||
| 446 | For more detail on how you might use parameterized containers, see | ||||||||||||||||||||||||||||||||
| 447 | L<Bread::Board::Manual::Concepts::Advanced/Parameterized Containers>. | ||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||
| 449 | =head2 C<as> | ||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||
| 451 | as { some_code() }; | ||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||
| 453 | This is just a replacement for the C<sub> keyword that is easier to read when | ||||||||||||||||||||||||||||||||
| 454 | defining containers. | ||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||
| 456 | =head2 C<service> | ||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||
| 458 | service $name, $literal; | ||||||||||||||||||||||||||||||||
| 459 | service $name, %service_description; | ||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||
| 461 | Within the C<as> blocks for your containers, you may construct services using | ||||||||||||||||||||||||||||||||
| 462 | the C<service> function. This can construct several different kinds of services | ||||||||||||||||||||||||||||||||
| 463 | based upon how it is called. | ||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||
| 465 | =head3 literal services | ||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||
| 467 | To build a literal service (a L<Bread::Board::Literal> object), just specify a | ||||||||||||||||||||||||||||||||
| 468 | scalar value or reference you want to use as the literal value: | ||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||
| 470 | # In case you need to adjust the gravitational constant of the Universe | ||||||||||||||||||||||||||||||||
| 471 | service gravitational_constant => 6.673E-11; | ||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||
| 473 | =head3 using injections | ||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||
| 475 | To build a service using one of the injection services, just fill in all the | ||||||||||||||||||||||||||||||||
| 476 | details required to use that sort of injection: | ||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||
| 478 | service search_service => ( | ||||||||||||||||||||||||||||||||
| 479 | class => 'MyApp::Search', | ||||||||||||||||||||||||||||||||
| 480 | block => sub { | ||||||||||||||||||||||||||||||||
| 481 | my $s = shift; | ||||||||||||||||||||||||||||||||
| 482 | MyApp::Search->new($s->param('url'), $s->param('type')); | ||||||||||||||||||||||||||||||||
| 483 | }, | ||||||||||||||||||||||||||||||||
| 484 | dependencies => { | ||||||||||||||||||||||||||||||||
| 485 | url => 'search_url', | ||||||||||||||||||||||||||||||||
| 486 | }, | ||||||||||||||||||||||||||||||||
| 487 | parameters => { | ||||||||||||||||||||||||||||||||
| 488 | type => { isa => 'Str', default => 'text' }, | ||||||||||||||||||||||||||||||||
| 489 | }, | ||||||||||||||||||||||||||||||||
| 490 | ); | ||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||
| 492 | The type of injection performed depends on the parameters used. You may use | ||||||||||||||||||||||||||||||||
| 493 | the C<service_class> parameter to pick a specific injector class. For | ||||||||||||||||||||||||||||||||
| 494 | instance, this is useful if you need to use L<Bread::Board::SetterInjection> | ||||||||||||||||||||||||||||||||
| 495 | or have defined a custom injection service. If you specify a C<block>, block | ||||||||||||||||||||||||||||||||
| 496 | injection will be performed using L<Bread::Board::BlockInjection>. If neither | ||||||||||||||||||||||||||||||||
| 497 | of these is present, constructor injection will be used with | ||||||||||||||||||||||||||||||||
| 498 | L<Bread::Board::ConstructorInjection> (and you must provide the C<class> | ||||||||||||||||||||||||||||||||
| 499 | option). | ||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||
| 501 | =head3 service dependencies | ||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||
| 503 | The C<dependencies> parameter takes a hashref of dependency names mapped to | ||||||||||||||||||||||||||||||||
| 504 | L<Bread::Board::Dependency> objects, but there are several coercions and sugar | ||||||||||||||||||||||||||||||||
| 505 | functions available to make specifying dependencies as easy as possible. The | ||||||||||||||||||||||||||||||||
| 506 | simplest case is when the names of the services you're depending on are the | ||||||||||||||||||||||||||||||||
| 507 | same as the names that the service you're defining will be accessing them with. | ||||||||||||||||||||||||||||||||
| 508 | In this case, you can just specify an arrayref of service names: | ||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||
| 510 | service foo => ( | ||||||||||||||||||||||||||||||||
| 511 | dependencies => [ 'bar', 'baz' ], | ||||||||||||||||||||||||||||||||
| 512 | # ... | ||||||||||||||||||||||||||||||||
| 513 | ); | ||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||
| 515 | If you need to use a different name, you can specify the dependencies as a | ||||||||||||||||||||||||||||||||
| 516 | hashref instead: | ||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||
| 518 | service foo => ( | ||||||||||||||||||||||||||||||||
| 519 | dependencies => { | ||||||||||||||||||||||||||||||||
| 520 | dbh => 'foo_dbh', | ||||||||||||||||||||||||||||||||
| 521 | }, | ||||||||||||||||||||||||||||||||
| 522 | # ... | ||||||||||||||||||||||||||||||||
| 523 | ); | ||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||
| 525 | You can also specify parameters when depending on a parameterized service: | ||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||
| 527 | service foo => ( | ||||||||||||||||||||||||||||||||
| 528 | dependencies => [ | ||||||||||||||||||||||||||||||||
| 529 | { bar => { bar_param => 1 } }, | ||||||||||||||||||||||||||||||||
| 530 | 'baz', | ||||||||||||||||||||||||||||||||
| 531 | ], | ||||||||||||||||||||||||||||||||
| 532 | # ... | ||||||||||||||||||||||||||||||||
| 533 | ); | ||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||
| 535 | Finally, services themselves can also be specified as dependencies, in which | ||||||||||||||||||||||||||||||||
| 536 | case they will just be resolved directly: | ||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||
| 538 | service foo => ( | ||||||||||||||||||||||||||||||||
| 539 | dependencies => { | ||||||||||||||||||||||||||||||||
| 540 | dsn => Bread::Board::Literal->new( | ||||||||||||||||||||||||||||||||
| 541 | name => 'dsn', | ||||||||||||||||||||||||||||||||
| 542 | value => 'dbi:mysql:mydb', | ||||||||||||||||||||||||||||||||
| 543 | ), | ||||||||||||||||||||||||||||||||
| 544 | }, | ||||||||||||||||||||||||||||||||
| 545 | # ... | ||||||||||||||||||||||||||||||||
| 546 | ); | ||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||
| 548 | As a special case, an arrayref of dependencies will be interpreted as a service | ||||||||||||||||||||||||||||||||
| 549 | which returns an arrayref containing the resolved values of those dependencies: | ||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||
| 551 | service foo => ( | ||||||||||||||||||||||||||||||||
| 552 | dependencies => { | ||||||||||||||||||||||||||||||||
| 553 | # items will resolve to [ $bar_service->get, $baz_service->get ] | ||||||||||||||||||||||||||||||||
| 554 | items => [ | ||||||||||||||||||||||||||||||||
| 555 | 'bar', | ||||||||||||||||||||||||||||||||
| 556 | Bread::Board::Literal->new(name => 'baz', value => 'BAZ'), | ||||||||||||||||||||||||||||||||
| 557 | ], | ||||||||||||||||||||||||||||||||
| 558 | }, | ||||||||||||||||||||||||||||||||
| 559 | # ... | ||||||||||||||||||||||||||||||||
| 560 | ); | ||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||
| 562 | =head3 inheriting and extending services | ||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||
| 564 | If the C<$name> starts with a C<'+'>, the service definition will instead | ||||||||||||||||||||||||||||||||
| 565 | extend an existing service with the given C<$name> (without the C<'+'>). This | ||||||||||||||||||||||||||||||||
| 566 | works similarly to the C<has '+foo'> syntax in Moose. It is most useful when | ||||||||||||||||||||||||||||||||
| 567 | defining a container class where the container is built up in C<BUILD> methods, | ||||||||||||||||||||||||||||||||
| 568 | as each class in the inheritance hierarchy can modify services defined in | ||||||||||||||||||||||||||||||||
| 569 | superclasses. The C<dependencies> and C<parameters> options will be merged with | ||||||||||||||||||||||||||||||||
| 570 | the existing values, rather than overridden. Note that literal services can't | ||||||||||||||||||||||||||||||||
| 571 | be extended, because there's nothing to extend. You can still override them | ||||||||||||||||||||||||||||||||
| 572 | entirely by declaring the service name without a leading C<'+'>. | ||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||
| 574 | =head2 C<literal> | ||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||
| 576 | literal($value); | ||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||
| 578 | Creates an anonymous L<Bread::Board::Literal> object with the given value. | ||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||
| 580 | service 'dbh' => ( | ||||||||||||||||||||||||||||||||
| 581 | block => sub { | ||||||||||||||||||||||||||||||||
| 582 | my $s = shift; | ||||||||||||||||||||||||||||||||
| 583 | require DBI; | ||||||||||||||||||||||||||||||||
| 584 | DBI->connect( | ||||||||||||||||||||||||||||||||
| 585 | $s->param('dsn'), | ||||||||||||||||||||||||||||||||
| 586 | $s->param('username'), | ||||||||||||||||||||||||||||||||
| 587 | $s->param('password'), | ||||||||||||||||||||||||||||||||
| 588 | ) || die "Could not connect"; | ||||||||||||||||||||||||||||||||
| 589 | }, | ||||||||||||||||||||||||||||||||
| 590 | dependencies => { | ||||||||||||||||||||||||||||||||
| 591 | dsn => literal 'dbi:SQLite:somedb', | ||||||||||||||||||||||||||||||||
| 592 | username => literal 'foo', | ||||||||||||||||||||||||||||||||
| 593 | password => literal 'password', | ||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||
| 595 | }, | ||||||||||||||||||||||||||||||||
| 596 | ); | ||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||
| 598 | =head2 C<depends_on> | ||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||
| 600 | depends_on($service_path); | ||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||
| 602 | The C<depends_on> function creates a L<Bread::Board::Dependency> object for the | ||||||||||||||||||||||||||||||||
| 603 | named C<$service_path> and returns it. | ||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||
| 605 | =head2 C<wire_names> | ||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||
| 607 | wire_names(@service_names); | ||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||
| 609 | This function is just a shortcut for passing a hash reference of dependencies | ||||||||||||||||||||||||||||||||
| 610 | into the service. It is not typically needed, since Bread::Board can usually | ||||||||||||||||||||||||||||||||
| 611 | understand what you mean - these declarations are all equivalent: | ||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||
| 613 | service foo => ( | ||||||||||||||||||||||||||||||||
| 614 | class => 'Pity::TheFoo', | ||||||||||||||||||||||||||||||||
| 615 | dependencies => { | ||||||||||||||||||||||||||||||||
| 616 | foo => depends_on('foo'), | ||||||||||||||||||||||||||||||||
| 617 | bar => depends_on('bar'), | ||||||||||||||||||||||||||||||||
| 618 | baz => depends_on('baz'), | ||||||||||||||||||||||||||||||||
| 619 | }, | ||||||||||||||||||||||||||||||||
| 620 | ); | ||||||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||||||
| 622 | service foo => ( | ||||||||||||||||||||||||||||||||
| 623 | class => 'Pity::TheFoo', | ||||||||||||||||||||||||||||||||
| 624 | dependencies => wire_names(qw( foo bar baz )), | ||||||||||||||||||||||||||||||||
| 625 | ); | ||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||
| 627 | service foo => ( | ||||||||||||||||||||||||||||||||
| 628 | class => 'Pity::TheFoo', | ||||||||||||||||||||||||||||||||
| 629 | dependencies => { | ||||||||||||||||||||||||||||||||
| 630 | foo => 'foo', | ||||||||||||||||||||||||||||||||
| 631 | bar => 'bar', | ||||||||||||||||||||||||||||||||
| 632 | baz => 'baz', | ||||||||||||||||||||||||||||||||
| 633 | }, | ||||||||||||||||||||||||||||||||
| 634 | ); | ||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||
| 636 | service foo => ( | ||||||||||||||||||||||||||||||||
| 637 | class => 'Pity::TheFoo', | ||||||||||||||||||||||||||||||||
| 638 | dependencies => [ qw(foo bar baz ) ], | ||||||||||||||||||||||||||||||||
| 639 | ); | ||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||
| 641 | =head2 C<typemap> | ||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||
| 643 | typemap $type, $service; | ||||||||||||||||||||||||||||||||
| 644 | typemap $type, $service_path; | ||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||
| 646 | This creates a type mapping for the named type. Typically, it is paired with | ||||||||||||||||||||||||||||||||
| 647 | the C<infer> call like so: | ||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||
| 649 | typemap 'MyApp::Model::UserAccount' => infer; | ||||||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||||||
| 651 | For more details on what type mapping is and how it works, see | ||||||||||||||||||||||||||||||||
| 652 | L<Bread::Board::Manual::Concepts::Typemap>. | ||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||
| 654 | =head2 C<infer> | ||||||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||||||
| 656 | infer; | ||||||||||||||||||||||||||||||||
| 657 | infer(%hints); | ||||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||
| 659 | This is used with C<typemap> to help create the typemap inference. It can be | ||||||||||||||||||||||||||||||||
| 660 | used with no arguments to do everything automatically. However, in some cases, | ||||||||||||||||||||||||||||||||
| 661 | you may want to pass a service instance as the argument or a hash of service | ||||||||||||||||||||||||||||||||
| 662 | arguments to change how the type map works. For example, if your type needs to | ||||||||||||||||||||||||||||||||
| 663 | be constructed using a setter injection, you can use an inference similar to | ||||||||||||||||||||||||||||||||
| 664 | this: | ||||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||
| 666 | typemap 'MyApp::Model::UserPassword' => infer( | ||||||||||||||||||||||||||||||||
| 667 | service_class => 'Bread::Board::SetterInjection', | ||||||||||||||||||||||||||||||||
| 668 | ); | ||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||
| 670 | For more details on what type mapping is and how it works, see | ||||||||||||||||||||||||||||||||
| 671 | L<Bread::Board::Manual::Concepts::Typemap>. | ||||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||
| 673 | =head2 C<include> | ||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||
| 675 | include $file; | ||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||
| 677 | This is a shortcut for loading a Bread::Board configuration from another file. | ||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||
| 679 | include "filename.pl"; | ||||||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||||||
| 681 | The above is pretty much identical to running: | ||||||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||||||
| 683 | do "filename.pl"; | ||||||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||||||
| 685 | However, you might find it more readable to use C<include>. | ||||||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||||||
| 687 | =head2 C<alias> | ||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||
| 689 | alias $service_name, $service_path, %service_description; | ||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||
| 691 | This helper allows for the creation of L<service | ||||||||||||||||||||||||||||||||
| 692 | aliases|Bread::Board::Service::Alias>, which allows you to define a | ||||||||||||||||||||||||||||||||
| 693 | service in one place and then reuse that service with a different name | ||||||||||||||||||||||||||||||||
| 694 | somewhere else. This is sort of like a symbolic link for | ||||||||||||||||||||||||||||||||
| 695 | services. Aliases will be L<resolved | ||||||||||||||||||||||||||||||||
| 696 | recursively|Bread::Board::Traversable/fetch>, so an alias can alias an | ||||||||||||||||||||||||||||||||
| 697 | alias. | ||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||
| 699 | For example, | ||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||
| 701 | service file_logger => ( | ||||||||||||||||||||||||||||||||
| 702 | class => 'MyApp::Logger::File', | ||||||||||||||||||||||||||||||||
| 703 | ); | ||||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||
| 705 | alias my_logger => 'file_logger'; | ||||||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||||||
| 707 | =head1 OTHER FUNCTIONS | ||||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||
| 709 | These are not exported, but might be helpful to you. | ||||||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||||||
| 711 | =head2 C<set_root_container> | ||||||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||||||
| 713 | set_root_container $container; | ||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||
| 715 | You may use this to set a top-level root container for all container | ||||||||||||||||||||||||||||||||
| 716 | definitions. | ||||||||||||||||||||||||||||||||
| 717 | |||||||||||||||||||||||||||||||||
| 718 | For example, | ||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||
| 720 | my $app = container MyApp => as { ... }; | ||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||
| 722 | Bread::Board::set_root_container($app); | ||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||
| 724 | my $config = container Config => as { ... }; | ||||||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||||||
| 726 | Here the C<$config> container would be created as a sub-container of C<$app>. | ||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||
| 728 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||
| 730 | Thanks to Daisuke Maki for his contributions and for really | ||||||||||||||||||||||||||||||||
| 731 | pushing the development of this module along. | ||||||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||||||
| 733 | Chuck "sprongie" Adams, for testing/using early (pre-release) | ||||||||||||||||||||||||||||||||
| 734 | versions of this module, and some good suggestions for naming | ||||||||||||||||||||||||||||||||
| 735 | it. | ||||||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||||||
| 737 | Matt "mst" Trout, for finally coming up with the best name | ||||||||||||||||||||||||||||||||
| 738 | for this module. | ||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||
| 740 | Gianni "dakkar" Ceccarelli for writing lots of documentation, and | ||||||||||||||||||||||||||||||||
| 741 | Net-a-Porter.com for paying his salary while he was doing it. | ||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||
| 743 | =head1 ARTICLES | ||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||
| 745 | L<Bread::Board is the right tool for this job|http://domm.plix.at/perl/2013_04_bread_board_is_the_right_rool_for_this_job.html> | ||||||||||||||||||||||||||||||||
| 746 | Thomas Klausner showing a use-case for Bread::Board. | ||||||||||||||||||||||||||||||||
| 747 | |||||||||||||||||||||||||||||||||
| 748 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||||||
| 750 | =over 4 | ||||||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||||||
| 752 | =item L<Bread::Board::Declare> | ||||||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||||||
| 754 | This provides more powerful syntax for writing Bread::Board container classes. | ||||||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||||||
| 756 | =item L<IOC> | ||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||
| 758 | Bread::Board is basically my re-write of IOC. | ||||||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||||||
| 760 | =item L<http://en.wikipedia.org/wiki/Breadboard> | ||||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||
| 762 | =back | ||||||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||||||
| 764 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||
| 766 | Stevan Little <stevan@iinteractive.com> | ||||||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||||||
| 768 | =head1 BUGS | ||||||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||||||
| 770 | Please report any bugs or feature requests on the bugtracker website | ||||||||||||||||||||||||||||||||
| 771 | https://github.com/stevan/BreadBoard/issues | ||||||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||||||
| 773 | When submitting a bug or request, please include a test-file or a | ||||||||||||||||||||||||||||||||
| 774 | patch to an existing test-file that illustrates the bug or desired | ||||||||||||||||||||||||||||||||
| 775 | feature. | ||||||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||||||
| 777 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||
| 779 | This software is copyright (c) 2017, 2016, 2015, 2014, 2013, 2011, 2009 by Infinity Interactive. | ||||||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||||||
| 781 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||
| 782 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||||||
| 784 | =cut | ||||||||||||||||||||||||||||||||