| IOC documentation | Contained in the IOC distribution. |
IOC::Visitor::SearchForService - Visitor for searching a IOC::Container hierarchy
use IOC::Visitor::SearchForService;
This is a IOC::Visitor object used for searching a IOC::Container hierarchy.
+------------------+
| <<IOC::Visitor>> |
+------------------+
|
^
|
+--------------------------------+
| IOC::Visitor::SearchForService |
+--------------------------------+
Creates a new instance which will search for a Service at a given $name. If no $name is given, than an IOC::InsufficientArguments exception is thrown.
Given a $container, the invocant will attempt to locate the service with the $name (given to the constuctor) from within the $container's hierarchy.
None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it.
I use Devel::Cover to test the code coverage of my tests, see the CODE COVERAGE section of IOC for more information.
stevan little, <stevan@iinteractive.com>
Copyright 2004-2007 by Infinity Interactive, Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| IOC documentation | Contained in the IOC distribution. |
package IOC::Visitor::SearchForService; 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, $service_name) = @_; ($service_name) || throw IOC::InsufficientArguments "You must provide a name of a service to find"; my $class = ref($_class) || $_class; my $visitor = { service_to_find => $service_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 $service_to_find = $self->{service_to_find}; return $self->_recursiveSearch($container, $service_to_find); } sub _recursiveSearch { my ($self, $container, $service_to_find) = @_; # look through all the current services return $container->get($service_to_find) if $container->hasService($service_to_find); # if we have sub-containers, ... if ($container->hasSubContainers()) { # then loop through all the sub-containers foreach my $sub_container ($container->getAllSubContainers()) { my $service = $self->_recursiveSearch($sub_container, $service_to_find); return $service if defined $service; } } return undef; } 1; __END__