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;
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 ();
}
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);
=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(@_);
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;
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 ) {