formatting fixed; pod for view and model methods in Catalyst.pm
[catagits/Catalyst-Runtime.git] / lib / Catalyst.pm
index 25673f8..c0ad6b8 100644 (file)
@@ -9,6 +9,7 @@ use Catalyst::Exception;
 use Catalyst::Log;
 use Catalyst::Utils;
 use Catalyst::Controller;
+use Catalyst::Context;
 use Devel::InnerPackage ();
 use Module::Pluggable::Object ();
 use Text::SimpleTable ();
@@ -31,6 +32,7 @@ __PACKAGE__->mk_classdata($_)
   engine_class context_class request_class response_class stats_class
   setup_finished/;
 
+__PACKAGE__->context_class('Catalyst::Context');
 __PACKAGE__->dispatcher_class('Catalyst::Dispatcher');
 __PACKAGE__->engine_class('Catalyst::Engine::CGI');
 __PACKAGE__->request_class('Catalyst::Request');
@@ -50,6 +52,8 @@ $VERSION = eval $VERSION;
 
 our $COUNT     = 1;
 our $START     = time;
+our $DETACH    = Catalyst::Exception::Detach->new;
+our $GO        = Catalyst::Exception::Go->new;
 
 sub import {
     my ( $class, @arguments ) = @_;
@@ -258,6 +262,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(@_);
@@ -1019,7 +1113,8 @@ sub prepare {
     # into the application.
     $class->context_class( ref $class || $class ) unless $class->context_class;
 
-    my $c = $class->context_class->new({});
+    my $app = $class->new({});
+    my $c = $class->context_class->new( application => $app );
 
     # For on-demand data
     $c->request->_context($c);
@@ -1049,7 +1144,7 @@ sub prepare {
         $c->prepare_read;
 
         # Parse the body unless the user wants it on-demand
-        unless ( ref($c)->config->{parse_on_demand} ) {
+        unless ( $app->config->{parse_on_demand} ) {
             $c->prepare_body;
         }
     }