The Go and visit tests don't like this as you get told to go away in a different...
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
index 5af713a..81916cf 100644 (file)
@@ -3,6 +3,11 @@ package Catalyst;
 use Moose;
 use Moose::Meta::Class ();
 extends 'Catalyst::Component';
+with qw/
+    MooseX::Emulate::Class::Accessor::Fast
+    Catalyst::Config
+    Catalyst::ClassData
+/;
 use Moose::Util qw/find_meta/;
 use B::Hooks::EndOfScope ();
 use Catalyst::Exception;
@@ -10,6 +15,8 @@ use Catalyst::Log;
 use Catalyst::Utils;
 use Catalyst::Controller;
 use Catalyst::Context;
+use Catalyst::Exception::Detach;
+use Catalyst::Exception::Go;
 use Devel::InnerPackage ();
 use Module::Pluggable::Object ();
 use Text::SimpleTable ();
@@ -71,9 +78,9 @@ sub import {
     }
 
     my $meta = Moose::Meta::Class->initialize($caller);
+    # Make the caller inherit from Catalyst
     unless ( $caller->isa('Catalyst') ) {
-        my @superclasses = ($meta->superclasses, $class, 'Catalyst::Controller');
-        $meta->superclasses(@superclasses);
+        $meta->superclasses($meta->superclasses, 'Catalyst');
     }
     # Avoid possible C3 issues if 'Moose::Object' is already on RHS of MyApp
     $meta->superclasses(grep { $_ ne 'Moose::Object' } $meta->superclasses);
@@ -262,6 +269,96 @@ e.g.
 
 =cut
 
+=head2 $c->controller($name)
+
+Gets a L<Catalyst::Controller> instance by name.
+
+    $c->controller('Foo')->do_stuff;
+
+If the name is omitted, will return the controller for the dispatched
+action.
+
+If you want to search for controllers, pass in a regexp as the argument.
+
+    # find all controllers that start with Foo
+    my @foo_controllers = $c->controller(qr{^Foo});
+
+
+=cut
+
+sub controller {
+    my ( $c, $name, @args ) = @_;
+
+    if( $name ) {
+        my @result = $c->_comp_search_prefixes( $name, qw/Controller C/ );
+        return map { $c->_filter_component( $_, @args ) } @result if ref $name;
+        return $c->_filter_component( $result[ 0 ], @args );
+    }
+
+    return $c->component( $c->action->class );
+}
+
+=head2 $c->view($name)
+
+Gets a L<Catalyst::View> instance by name.
+
+=cut
+
+sub view {
+    my ( $c, $name, @args ) = @_;
+
+    if( $name ) {
+        my @result = $c->_comp_search_prefixes( $name, qw/View V/ );
+        return map { $c->_filter_component( $_, @args ) } @result if ref $name;
+        return $c->_filter_component( $result[ 0 ], @args );
+    }
+
+    return $c->view( $c->config->{default_view} )
+      if $c->config->{default_view};
+    my( $comp, $rest ) = $c->_comp_search_prefixes( undef, qw/View V/);
+
+    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.' );
+    }
+
+    return $c->_filter_component( $comp );
+}
+
+=head2 $c->model($name)
+
+Gets a L<Catalyst::Model> instance by name.
+
+=cut
+
+sub model {
+    my ( $c, $name, @args ) = @_;
+    if( $name ) {
+        my @result = $c->_comp_search_prefixes( $name, qw/Model M/ );
+        return map { $c->_filter_component( $_, @args ) } @result if ref $name;
+        return $c->_filter_component( $result[ 0 ], @args );
+    }
+
+    return $c->model( $c->config->{default_model} )
+      if $c->config->{default_model};
+
+    my( $comp, $rest ) = $c->_comp_search_prefixes( undef, qw/Model M/);
+
+    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.' );
+    }
+
+    return $c->_filter_component( $comp );
+}
+
+
 sub _comp_search_prefixes {
     my $c = shift;
     return map $c->components->{ $_ }, $c->_comp_names_search_prefixes(@_);
@@ -431,6 +528,7 @@ sub component {
             return $c->_filter_component( $comp, @args ) if $comp;
         }
 
+        return if $c->config->{disable_component_resolution_regex_fallback};
         # This is here so $c->comp( '::M::' ) works
         my $query = ref $name ? $name : qr{$name}i;
 
@@ -736,11 +834,6 @@ EOF
           if ( keys %{ $class->components } );
     }
 
-    # Add our self to components, since we are also a component
-    if( $class->isa('Catalyst::Controller') ){
-      $class->components->{$class} = $class;
-    }
-
     $class->setup_actions;
 
     if ( $class->debug ) {