IOC::Visitor::SearchForContainer - Visitor for searching a IOC::Container hierarchy


IOC documentation Contained in the IOC distribution.

Index


Code Index:

NAME

Top

IOC::Visitor::SearchForContainer - Visitor for searching a IOC::Container hierarchy

SYNOPSIS

Top

  use IOC::Visitor::SearchForContainer;

DESCRIPTION

Top

This is a IOC::Visitor object used for searching a IOC::Container hierarchy

          +------------------+
          | <<IOC::Visitor>> |
          +------------------+
                    |
                    ^
                    |
   +----------------------------------+
   | IOC::Visitor::SearchForContainer |
   +----------------------------------+

METHODS

Top

new ($name)

Creates a new instance which will search for a Container at a given $name. If no $name is given, than an IOC::InsufficientArguments exception is thrown.

visit ($container)

Given a $container, the invocant will attempt to locate the container with the $name (given to the constuctor) from within the $container's hierarchy.

TO DO

Top

Work on the documentation

BUGS

Top

None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it.

CODE COVERAGE

Top

I use Devel::Cover to test the code coverage of my tests, see the CODE COVERAGE section of IOC for more information.

SEE ALSO

Top

AUTHOR

Top

stevan little, <stevan@iinteractive.com>

COPYRIGHT AND LICENSE

Top


IOC documentation Contained in the IOC distribution.

package IOC::Visitor::SearchForContainer;

use strict;
use warnings;

our $VERSION = '0.03';

use Scalar::Util qw(blessed);

use IOC::Interfaces;
use IOC::Exceptions;

use base 'IOC::Visitor';

sub new {
    my ($_class, $container_name) = @_;
    ($container_name) 
        || throw IOC::InsufficientArguments "You must provide a name of a container to find";
    my $class = ref($_class) || $_class;
    my $visitor = {
        container_to_find => $container_name
        };
    bless($visitor, $class);
    return $visitor;
}

sub visit {
    my ($self, $container) = @_;
    (blessed($container) && $container->isa('IOC::Container'))
        || throw IOC::InsufficientArguments "You must provide an IOC::Container object to search";
    my $container_to_find = $self->{container_to_find};
    return $self->_recursiveSearch($container, $container_to_find);
}

sub _recursiveSearch {
    my ($self, $container, $container_to_find) = @_;
    # if we have it stored, then return it
    return $container->getSubContainer($container_to_find)
        if $container->hasSubContainer($container_to_find);    
    # otherwise, loop through all the sub-containers
    # sort the names too, which will make sure the
    # search will roughly take the same amount of time
    # each time we do it, otherwise we are at the 
    # mercy of the hash ordering 
    foreach my $sub_container_name (sort $container->getSubContainerList()) {
        # otherwise we need to search the next level
        my $sub_container = $container->getSubContainer($sub_container_name);
        $self->_recursiveSearch($sub_container, $container_to_find) 
            if $sub_container->hasSubContainers();
    }
    return undef;
}

1;

__END__