Scroll to navigation

Bread::Board::Container(3pm) User Contributed Perl Documentation Bread::Board::Container(3pm)

NAME

Bread::Board::Container - A container for services and other containers

VERSION

version 0.37

SYNOPSIS

  use Bread::Board;
  my $c = container MCP => as {
      container Users => as {
          service flynn => ...;
          service bradley => ...;
          service dillinger => ...;
      };
      container Programs => as {
          container Rebels => as {
              service tron => ...;
              service yori => ...;
              alias flynn => '/Users/flynn';
          };
          # nested container
          container Slaves => as {
              service sark => ...;
              service crom => ...;
          };
      };
  };
  # OR directly...
  my $guardians => Bread::Board::Container->new( name => 'Guardians' );
  $guardians->add_service(
      Bread::Board::ConstructorInjection->new(
          name => 'dumont',
          ...,
      )
  );
  $c->get_sub_container('Programs')->add_sub_container($guardians);

DESCRIPTION

This class implements the container for Bread::Board: a container is a thing that contains services and other containers. Each container and service has a name, so you end up with a tree of named nodes, just like files and directories in a filesystem: each item can be referenced using a path (see Bread::Board::Traversable for the details).

ATTRIBUTES

"name"

Read/write string, required. Every container needs a name, by which it can be referenced when fetching it.

"services"

Hashref, constrained by "Bread::Board::Container::ServiceList", mapping names to services directly contained in this container. Every service added here will have its "parent" set to this container.

You can pass an arrayref of services instead of a hashref, the keys will be the names of the services.

You should probably use "add_service" and "get_service" to manipulate this attribute, instead of modifying it directly.

"sub_containers"

Hashref, constrained by "Bread::Board::Container::SubContainerList", mapping names to containers directly contained in this container. Every container added here will have its "parent" set to this container.

You can pass an arrayref of containers instead of a hashref, the keys will be the names of the containers.

You should probably use "add_sub_container" and "get_sub_container" to manipulate this attribute, instead of modifying it directly.

Containers added here can either be normal Bread::Board::Container or Bread::Board::Container::Parameterized.

METHODS

"add_service"

  $container->add_service($service);

Adds a service into the "services" map, using its name as the key.

"get_service"

  my $service = $container->get_service($name);

Returns a service by name, or "undef" if there's no such service in the "services" map.

"has_service"

  if ($container->has_service($name)) { ... }

Returns true if a service with the given name name exists in the "services" map, false otherwise.

"has_services"

  if ($container->has_services) { ... }

Returns true if the "services" map contains any services, false if it's empty.

"get_service_list"

  my @service_names = $container->get_service_list();

Returns the names off all services present in the "services" map.

"add_sub_container"

  $container->add_sub_container($container);

Adds a container into the "sub_containers" map, using its name as the key.

"get_sub_container"

  my $container = $container->get_sub_container($name);

Returns a container by name, or "undef" if there's no such container in the "sub_containers" map.

"has_sub_container"

  if ($container->has_sub_container($name)) { ... }

Returns true if a container with the given name name exists in the "sub_containers" map, false otherwise.

"has_sub_containers"

  if ($container->has_sub_containers) { ... }

Returns true if the "sub_containers" map contains any contains, false if it's empty.

"get_sub_container_list"

  my @container_names = $container->get_sub_container_list();

Returns the names off all containers present in the "sub_containers" map.

"add_type_mapping_for"

  $containers->add_type_mapping_for( $type_name, $service );

Adds a mapping from a Moose type to a service: whenever we try to "resolve" that type, we'll use that service to instantiate it.

"get_type_mapping_for"

  my $service = $container->get_type_mapping_for( $type_name );

Returns the service to use to instantiate the given type name.

Important: if a mapping for the exact type can't be found, but a mapping for a subtype of it can, you'll get the latter instead:

  package Superclass { use Moose };
  package Subclass { use Moose; exends 'Superclass' };
  $c->add_type_mapping_for(
   'Subclass',
   Bread::Board::ConstructorInjection->new(name=>'sc',class=>'Subclass'),
 );
 my $o = $c->get_type_mapping_for('Superclass')->get;

$o is an instance of "Subclass". If there are more than one sub-type mapped, you get a random one. This is probably a bad idea.

"has_type_mapping_for"

  if ($container->has_type_mapping_for( $type_name )) { ... }

Returns true if we have a service defined to instantiate the given type name, but see the note on "get_type_mapping_for" about subtype mapping.

"resolve"

  my $object = $container->resolve(service=>$service_name);
  my $object = $container->resolve(service=>$service_name,parameters=>\%p);

When given a service name, this method will fetch the service, then call "get" on it, optionally passing the given parameters.

  my $object = $container->resolve(type=>$type);
  my $object = $container->resolve(type=>$type,parameters=>\%p);

When given a type name, this method will use "get_type_mapping_for" to get the service, then call "get" on it, optionally passing the given parameters. If the instance is not of the expected type, the method will die.

AUTHOR

Stevan Little <stevan@iinteractive.com>

BUGS

Please report any bugs or feature requests on the bugtracker website https://github.com/stevan/BreadBoard/issues

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

COPYRIGHT AND LICENSE

This software is copyright (c) 2019, 2017, 2016, 2015, 2014, 2013, 2011, 2009 by Infinity Interactive.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

2022-12-12 perl v5.36.0