X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst.pm;h=352f762f48557fc0d955fef771ac305b5f121d0d;hb=f5dfb6ffcafcb88c7072329e7ec45fef6e04d301;hp=e6a9f4d8e7bf03dbd1cb4eb579966bb79a62e0c1;hpb=1c1bb322fd27968c04987a1b65c0144a99ba7284;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index e6a9f4d..352f762 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -27,6 +27,7 @@ use URI::https; use Tree::Simple qw/use_weak_refs/; use Tree::Simple::Visitor::FindByUID; use Class::C3::Adopt::NEXT; +use List::Util qw/first/; use List::MoreUtils qw/uniq/; use attributes; use utf8; @@ -695,27 +696,42 @@ sub component { my ( $c, $component, @args ) = @_; if ( $component ) { - my ($type, $name) = _get_component_type_name($component); - - if ($type && $c->container->has_sub_container($type)) { - my $container = $c->container->get_sub_container($type); - - if( !ref $component && $container->has_service($name) ) { - return $container->get_component( $name, $c, @args ); - } - - return $container->get_component_regexp( $name, $c, @args ); - } - if (ref $component) { + my @result; for my $subcontainer_name (qw/model view controller/) { my $subcontainer = $c->container->get_sub_container($subcontainer_name); my @components = $subcontainer->get_service_list; - my @result = grep { m{$component} } @components; + @result = grep { m{$component} } @components; return map { $subcontainer->get_component( $_, $c, @args ) } @result if @result; } + + # it expects an empty list on failed searches + return @result; + } + else { + my ($type, $name) = _get_component_type_name($component); + + if ($type && $c->container->has_sub_container($type)) { + my $container = $c->container->get_sub_container($type); + + if ( !ref $component && $container->has_service($name) ) { + return $container->get_component( $name, $c, @args ); + } + + return $container->get_component_regexp( $name, $c, @args ); + } + else { + for my $subcontainer_name (qw/model view controller/) { + my $subcontainer = $c->container->get_sub_container($subcontainer_name); + my @components = $subcontainer->get_service_list; + my $result = first { $_ eq $component } @components; + + return $subcontainer->get_component( $result, $c, @args ) + if $result; + } + } } # FIXME: I probably shouldn't be doing this @@ -2388,10 +2404,17 @@ sub setup_components { $containers->{view}->make_single_default; } +# FIXME: should this sub exist? +# should it be moved to Catalyst::Utils, +# or replaced by something already existing there? sub _get_component_type_name { my $component = shift; my @parts = split /::/, $component; + if (scalar @parts == 1) { + return (undef, $component); + } + while (my $type = shift @parts) { return ('controller', join '::', @parts) if $type =~ /^(c|controller)$/i;