removed a little bit of repetition, and commented
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
index 46d0e8b..37371ab 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;
@@ -558,10 +559,18 @@ If you want to search for controllers, pass in a regexp as the argument.
 sub controller {
     my ( $c, $name, @args ) = @_;
 
-    return $c->container->get_component_from_sub_container( 'controller', $name, $c, @args)
-        if( $name );
+# FIXME: should this be a Catalyst::Utils method?
+    if (!$name) {
+        my $class  = $c->action->class;
 
-    return $c->component( $c->action->class );
+        my $prefix = length Catalyst::Utils::class2classprefix($class);
+
+        # MyApp::Controller::Foo becomes Foo
+        # the + 2 is because of the ::
+        $name = substr $class, $prefix + 2;
+    }
+
+    return $c->container->get_component_from_sub_container( 'controller', $name, $c, @args);
 }
 
 =head2 $c->model($name)
@@ -587,33 +596,16 @@ If you want to search for models, pass in a regexp as the argument.
 
 sub model {
     my ( $c, $name, @args ) = @_;
-    my $appclass = ref($c) || $c;
-    my $container = $c->container->get_sub_container('model');
 
-    return $c->container->get_component_from_sub_container( 'model', $name, $c, @args)
-        if( $name );
-
-    if (ref $c) {
+    if (ref $c && !$name) {
         return $c->stash->{current_model_instance}
-          if $c->stash->{current_model_instance};
-        return $c->model( $c->stash->{current_model} )
-          if $c->stash->{current_model};
-    }
-    return $c->model( $appclass->config->{default_model} )
-      if $appclass->config->{default_model};
-
-# FIXME: will this still be mantained?
-    my( $comp, $rest ) = $container->get_service_list;
+            if $c->stash->{current_model_instance};
 
-    if( $rest ) {
-        $c->log->warn( Carp::shortmess('Calling $c->model() will return a random model unless you specify one of:') );
-        $c->log->warn( '* $c->config(default_model => "the name of the default model to use")' );
-        $c->log->warn( '* $c->stash->{current_model} # the name of the model to use for this request' );
-        $c->log->warn( '* $c->stash->{current_model_instance} # the instance of the model to use for this request' );
-        $c->log->warn( 'NB: in version 5.81, the "random" behavior will not work at all.' );
+        $name = $c->stash->{current_model}
+            if $c->stash->{current_model};
     }
 
-    return $container->get_component( $comp, $c, @args );
+    return $c->container->get_component_from_sub_container( 'model', $name, $c, @args);
 }
 
 
@@ -640,32 +632,16 @@ If you want to search for views, pass in a regexp as the argument.
 
 sub view {
     my ( $c, $name, @args ) = @_;
-    my $appclass = ref($c) || $c;
-    my $container = $c->container->get_sub_container('view');
-
-    return $c->container->get_component_from_sub_container( 'view', $name, $c, @args)
-        if( $name );
 
-    if (ref $c) {
+    if (ref $c && !$name) {
         return $c->stash->{current_view_instance}
-          if $c->stash->{current_view_instance};
-        return $c->view( $c->stash->{current_view} )
-          if $c->stash->{current_view};
-    }
-    return $c->view( $appclass->config->{default_view} )
-      if $appclass->config->{default_view};
-
-    my( $comp, $rest ) = $container->get_service_list;
+            if $c->stash->{current_view_instance};
 
-    if( $rest ) {
-        $c->log->warn( 'Calling $c->view() will return a random view unless you specify one of:' );
-        $c->log->warn( '* $c->config(default_view => "the name of the default view to use")' );
-        $c->log->warn( '* $c->stash->{current_view} # the name of the view to use for this request' );
-        $c->log->warn( '* $c->stash->{current_view_instance} # the instance of the view to use for this request' );
-        $c->log->warn( 'NB: in version 5.81, the "random" behavior will not work at all.' );
+        $name = $c->stash->{current_view}
+            if $c->stash->{current_view};
     }
 
-    return $container->get_component( $comp, $c, @args );
+    return $c->container->get_component_from_sub_container( 'view', $name, $c, @args);
 }
 
 =head2 $c->controllers
@@ -702,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)
@@ -719,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 };
 }
 
@@ -1487,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) } ));
     }
 
@@ -2407,12 +2400,22 @@ sub setup_components {
             $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;
 
+    if (scalar @parts == 1) {
+        return (undef, $component);
+    }
+
     while (my $type = shift @parts) {
         return ('controller', join '::', @parts)
             if $type =~ /^(c|controller)$/i;