Beam-Wire
view release on metacpan or search on metacpan
lib/Beam/Wire.pm view on Meta::CPAN
with 'Beam::Emitter';
#pod =attr file
#pod
#pod The path of the file where services are configured (typically a YAML
#pod file). The file's contents should be a single hashref. The keys are
#pod service names, and the values are L<service
#pod configurations|Beam::Wire::Help::Config>.
#pod
#pod =cut
has file => (
is => 'ro',
isa => InstanceOf['Path::Tiny'],
coerce => sub {
if ( !blessed $_[0] || !$_[0]->isa('Path::Tiny') ) {
return path( $_[0] );
}
return $_[0];
},
);
#pod =attr dir
#pod
#pod The directory path or paths to use when searching for inner container files.
#pod Defaults to using the directory which contains the file specified by the
#pod L<file attribute|/file> followed by the C<BEAM_PATH> environment variable
#pod (separated by colons C<:>).
#pod
#pod =cut
has dir => (
is => 'ro',
isa => ArrayRef[InstanceOf['Path::Tiny']],
lazy => 1,
default => sub {
my $dir = [
($_[0]->file ? ($_[0]->file->parent) : ()),
($ENV{BEAM_PATH} ? (map { path($_) } grep !!$_, split /:/, $ENV{BEAM_PATH}) : ()),
];
; print 'Using default paths ', Dumper $dir if DEBUG;
return $dir;
},
coerce => sub {
if ( !ref $_[0] ) {
return [path( $_[0] )];
}
if ( ref $_[0] eq 'ARRAY' ) {
return [map { blessed( $_ ) && $_->isa('Path::Tiny') ? $_ : path($_) } @{$_[0]}];
}
return $_[0];
},
);
#pod =attr config
#pod
#pod The raw configuration data. By default, this data is loaded by
#pod L<Config::Any|Config::Any> using the file specified by the L<file attribute|/file>.
#pod
#pod See L<Beam::Wire::Help::Config for details on what the configuration
#pod data structure looks like|Beam::Wire::Help::Config>.
#pod
#pod If you don't want to load a file, you can specify this attribute in the
#pod Beam::Wire constructor.
#pod
#pod =cut
has config => (
is => 'ro',
isa => HashRef,
lazy => 1,
builder => 1
);
sub _build_config {
my ( $self ) = @_;
return {} if ( !$self->file );
return $self->_load_config( $self->file );
}
#pod =attr services
#pod
#pod A hashref of cached services built from the L<configuration|/config>. If
#pod you want to inject a pre-built object for other services to depend on,
#pod add it here.
#pod
#pod =cut
has services => (
is => 'ro',
isa => HashRef,
lazy => 1,
builder => 1,
);
sub _build_services {
my ( $self ) = @_;
my $services = {};
return $services;
}
#pod =attr meta_prefix
#pod
#pod The character that begins a meta-property inside of a service's C<args>. This
#pod includes C<$ref>, C<$class>, C<$method>, and etc...
#pod
#pod The default value is C<$>. The empty string is allowed.
#pod
#pod =cut
has meta_prefix => (
is => 'ro',
isa => Str,
default => sub { q{$} },
);
#pod =method get
#pod
#pod my $service = $wire->get( $name );
#pod my $service = $wire->get( $name, %overrides )
#pod
lib/Beam/Wire.pm view on Meta::CPAN
class: Person
args:
name: Malcolm Reynolds
rank: Captain
first_officer:
$class: Person
name: Zoë Alleyne Washburne
rank: Commander
# script.pl
use Beam::Wire;
my $wire = Beam::Wire->new( file => 'wire.yml' );
my $captain = $wire->get( 'captain' );
print $captain->name; # "Malcolm Reynolds"
=head1 DESCRIPTION
Beam::Wire is a configuration module and a dependency injection
container. In addition to complex data structures, Beam::Wire configures
and creates plain old Perl objects.
A dependency injection (DI) container creates an inversion of control:
Instead of manually creating all the dependent objects (also called
"services") before creating the main object that we actually want, a DI
container handles that for us: We describe the relationships between
objects, and the objects get built as needed.
Dependency injection is sometimes called the opposite of garbage
collection. Rather than ensure objects are destroyed in the right order,
dependency injection makes sure objects are created in the right order.
Using Beam::Wire in your application brings great flexibility,
allowing users to easily add their own code to customize how your
project behaves.
For an L<introduction to the Beam::Wire service configuration format,
see Beam::Wire::Help::Config|Beam::Wire::Help::Config>.
=head1 ATTRIBUTES
=head2 file
The path of the file where services are configured (typically a YAML
file). The file's contents should be a single hashref. The keys are
service names, and the values are L<service
configurations|Beam::Wire::Help::Config>.
=head2 dir
The directory path or paths to use when searching for inner container files.
Defaults to using the directory which contains the file specified by the
L<file attribute|/file> followed by the C<BEAM_PATH> environment variable
(separated by colons C<:>).
=head2 config
The raw configuration data. By default, this data is loaded by
L<Config::Any|Config::Any> using the file specified by the L<file attribute|/file>.
See L<Beam::Wire::Help::Config for details on what the configuration
data structure looks like|Beam::Wire::Help::Config>.
If you don't want to load a file, you can specify this attribute in the
Beam::Wire constructor.
=head2 services
A hashref of cached services built from the L<configuration|/config>. If
you want to inject a pre-built object for other services to depend on,
add it here.
=head2 meta_prefix
The character that begins a meta-property inside of a service's C<args>. This
includes C<$ref>, C<$class>, C<$method>, and etc...
The default value is C<$>. The empty string is allowed.
=head1 METHODS
=head2 get
my $service = $wire->get( $name );
my $service = $wire->get( $name, %overrides )
The get method resolves and returns the service named C<$name>, creating
it, if necessary, with L<the create_service method|/create_service>.
C<%overrides> is an optional list of name-value pairs. If specified,
get() will create an new, anonymous service that extends the named
service with the given config overrides. For example:
# test.pl
use Beam::Wire;
my $wire = Beam::Wire->new(
config => {
foo => {
args => {
text => 'Hello, World!',
},
},
},
);
my $foo = $wire->get( 'foo', args => { text => 'Hello, Chicago!' } );
print $foo; # prints "Hello, Chicago!"
This allows you to create factories out of any service, overriding service
configuration at run-time.
If C<$name> contains a slash (C</>) character (e.g. C<foo/bar>), the left
side (C<foo>) will be used as the name of an inner container, and the
right side (C<bar>) is a service inside that container. For example,
these two lines are equivalent:
$bar = $wire->get( 'foo/bar' );
$bar = $wire->get( 'foo' )->get( 'bar' );
Inner containers can be nested as deeply as desired (C<foo/bar/baz/fuzz>).
=head2 set
( run in 1.191 second using v1.01-cache-2.11-cpan-5e09290becf )