fix comment
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
index 134f5b6..427d51c 100644 (file)
@@ -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;
@@ -677,6 +678,28 @@ sub views {
     return $c->container->get_sub_container('view')->get_service_list;
 }
 
+sub _find_component {
+    my ($c, $component, @args) = @_;
+    my @result;
+
+    my $query = ref $component
+              ? $component
+              : qr{^$component$}
+              ;
+
+    for my $subcontainer_name (qw/model view controller/) {
+        my $subcontainer = $c->container->get_sub_container($subcontainer_name);
+        my @components   = $subcontainer->get_service_list;
+        @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;
+}
+
 =head2 $c->comp($name)
 
 =head2 $c->component($name)
@@ -694,39 +717,35 @@ component name will be returned.
 sub component {
     my ( $c, $component, @args ) = @_;
 
-    if ( $component ) {
-        # FIXME: I probably shouldn't be doing this
-        return $c->components->{$component}
-            if exists $c->components->{$component};
+    return sort keys %{ $c->components }
+        unless $component;
 
-        my ($type, $name) = _get_component_type_name($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);
+    return $c->container->get_component_from_sub_container(
+        $type, $name, $c, @args
+    ) if $type;
 
-            if( !ref $component && $container->has_service($name) ) {
-                return $container->get_component( $name, $c, @args );
-            }
+    my @result = $c->_find_component( $component, @args );
 
-            return $container->get_component_regexp( $name, $c, @args );
-        }
+    # list context for regexp searches
+    return @result if ref $component;
 
-        if (ref $component) {
-            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;
+    # only one component (if it's found) for string searches
+    return shift @result if @result;
 
-                return map { $subcontainer->get_component( $_, $c, @args ) } @result;
-            }
-        }
+    # FIXME: I probably shouldn't be doing this
+    # I'm keeping it temporarily for things like $c->comp('MyApp')
+    return $c->components->{$component}
+        if exists $c->components->{$component} and !@args;
 
-        $c->log->warn("Looking for '$component', but nothing was found.");
+    $c->log->warn("Looking for '$component', but nothing was found.");
 
-        # I would expect to return an empty list here, but that breaks back-compat
-    }
+    # I would expect to return an empty list here, but that breaks back-compat
+    $c->log->warn("Component not found, returning the list of existing");
+    $c->log->warn("components. This behavior is going to be deprecated");
+    $c->log->warn("in future releases.");
 
-    # fallback
     return sort keys %{ $c->components };
 }
 
@@ -894,7 +913,7 @@ Please do not use this functionality in new code.
 sub plugin {
     my ( $class, $name, $plugin, @args ) = @_;
 
-    # See block comment in t/unit_core_plugin.t
+    # See block comment in t/aggregate/unit_core_plugin.t
     $class->log->warn(qq/Adding plugin using the ->plugin method is deprecated, and will be removed in Catalyst 5.81/);
 
     $class->_register_plugin( $plugin, 1 );
@@ -1462,7 +1481,6 @@ around components => sub {
 
         my ($type, $name) = _get_component_type_name($component);
 
-# FIXME: shouldn't the service name be $name?
         $containers->{$type}->add_service(Catalyst::IOC::BlockInjection->new( name => $name, block => sub { return $class->setup_component($component) } ));
     }
 
@@ -2338,6 +2356,7 @@ sub setup_components {
     my $class = shift;
 
     my $config  = $class->config->{ setup_components };
+    my $search_extra = $config->{ search_extra };
 
     my @comps = $class->locate_components($config);
     my %comps = map { $_ => 1 } @comps;
@@ -2361,7 +2380,7 @@ sub setup_components {
 
     for my $component (@comps) {
         my $instance = $class->components->{ $component } = $class->setup_component($component);
-        if ( my ($type, $name) = _get_component_type_name($component) ) {
+        if ( my ($type, $name) = _get_component_type_name($component, $search_extra) ) {
             $containers->{$type}->add_service(Catalyst::IOC::BlockInjection->new( name => $name, block => sub { return $instance } ));
         }
         my @expanded_components = $instance->can('expand_modules')
@@ -2375,18 +2394,31 @@ sub setup_components {
                 qq{Please switch your class names to ::Model::, ::View:: and ::Controller: as appropriate.\n}
             ) if $deprecatedcatalyst_component_names;
 
-            if (my ($type, $name) = _get_component_type_name($component)) {
+            if (my ($type, $name) = _get_component_type_name($component, $search_extra)) {
                 $containers->{$type}->add_service(Catalyst::IOC::BlockInjection->new( name => $name, block => sub { return $class->setup_component($component) } ));
             }
 
             $class->components->{ $component } = $class->setup_component($component);
         }
     }
+
+    $containers->{model}->make_single_default;
+    $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;
+    my ( $component, $search_extra) = @_;
+    $search_extra ||= [];
+    my @search_extra = map { s/^:://; lc $_ } @$search_extra;
+
+    my @parts = split /::/, $component;
+
+    if (scalar @parts == 1) {
+        return (undef, $component);
+    }
 
     while (my $type = shift @parts) {
         return ('controller', join '::', @parts)
@@ -2397,9 +2429,20 @@ sub _get_component_type_name {
 
         return ('view', join '::', @parts)
             if $type =~ /^(v|view)$/i;
+
+        return (_get_component_type($component), join '::', @parts)
+            if @search_extra and ( grep { lc $type eq $_ } @search_extra );
     }
 }
 
+sub _get_component_type {
+    my ( $instance ) = @_;
+
+    return 'controller' if $instance->isa('Catalyst::Controller');
+    return 'model'      if $instance->isa('Catalyst::Model');
+    return 'view'       if $instance->isa('Catalyst::View');
+}
+
 =head2 $c->locate_components( $setup_component_config )
 
 This method is meant to provide a list of component modules that should be