X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst.pm;h=1b47918ad1ee3708386a9e5e8e7be9db81c43a62;hb=2328d24b6808f6d51f7338858bed5d0f12b95821;hp=369b6248b4d057b6d8c267777bfa5918af782f79;hpb=83db87df7ee52592ca190f353529778900e75f18;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 369b624..1b47918 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -15,9 +15,7 @@ use Catalyst::Response; use Catalyst::Utils; use Catalyst::Controller; use Data::OptList; -use Devel::InnerPackage (); use File::stat; -use Module::Pluggable::Object (); use Text::SimpleTable (); use Path::Class::Dir (); use Path::Class::File (); @@ -29,8 +27,15 @@ use Tree::Simple::Visitor::FindByUID; use Class::C3::Adopt::NEXT; use List::MoreUtils qw/uniq/; use attributes; +use String::RewritePrefix; +use Catalyst::EngineLoader; use utf8; use Carp qw/croak carp shortmess/; +use Try::Tiny; +use Plack::Middleware::Conditional; +use Plack::Middleware::ReverseProxy; +use Plack::Middleware::IIS6ScriptNameFix; +use Plack::Middleware::LighttpdScriptNameFix; BEGIN { require 5.008004; } @@ -67,19 +72,18 @@ our $GO = Catalyst::Exception::Go->new; #I imagine that very few of these really need to be class variables. if any. #maybe we should just make them attributes with a default? __PACKAGE__->mk_classdata($_) - for qw/container components arguments dispatcher engine log dispatcher_class - engine_class context_class request_class response_class stats_class - setup_finished/; + for qw/container arguments dispatcher engine log dispatcher_class + engine_loader context_class request_class response_class stats_class + setup_finished _psgi_app loading_psgi_file/; __PACKAGE__->dispatcher_class('Catalyst::Dispatcher'); -__PACKAGE__->engine_class('Catalyst::Engine::CGI'); __PACKAGE__->request_class('Catalyst::Request'); __PACKAGE__->response_class('Catalyst::Response'); __PACKAGE__->stats_class('Catalyst::Stats'); # Remember to update this in Catalyst::Runtime as well! -our $VERSION = '5.80032'; +our $VERSION = '5.90002'; sub import { my ( $class, @arguments ) = @_; @@ -92,10 +96,12 @@ sub import { return if $caller eq 'main'; my $meta = Moose::Meta::Class->initialize($caller); - unless ( $caller->isa('Catalyst') ) { - my @superclasses = ($meta->superclasses, $class, 'Catalyst::Controller'); - $meta->superclasses(@superclasses); - } + + unless ( $caller->isa('Catalyst') ) { # XXX - Remove! + my @superclasses = ($meta->superclasses, $class, 'Catalyst::Component'); # XXX - Remove! + $meta->superclasses(@superclasses); # XXX - Remove! + } # XXX - Remove! + # Avoid possible C3 issues if 'Moose::Object' is already on RHS of MyApp $meta->superclasses(grep { $_ ne 'Moose::Object' } $meta->superclasses); @@ -112,6 +118,15 @@ sub import { $caller->setup_home; } +sub MODIFY_CODE_ATTRIBUTES { + Catalyst::Exception->throw( + "Catalyst applications (aka MyApp) cannot be controllers anymore. " . + "That has been deprecated and removed. You should create a " . + "controller class called Root.pm, and move relevant code to that class." + ); +} + + sub _application { $_[0] } =head1 NAME @@ -555,44 +570,7 @@ If you want to search for controllers, pass in a regexp as the argument. =cut -sub controller { - my ( $c, $name, @args ) = @_; - my $container = $c->container->get_sub_container('controller'); - - my $appclass = ref $c || $c; - if( $name ) { - if ( !ref $name ) { # Direct component hash lookup to avoid costly regexps - return $container->resolve(service => $name, parameters => { context => [ $c, @args ] } ) - if $container->has_service($name); - } - - return - if $c->config->{disable_component_resolution_regex_fallback} && !ref $name; - - my $query = ref $name ? $name : qr{$name}; - $query =~ s/^${appclass}::(C|Controller):://; - my @comps = $container->get_service_list; - my @result; - for (@comps) { - push @result, $container->resolve( service => $_, parameters => { context => [ $c, @args ] } ) - if m/$query/; - } - - if (@result) { - if (!ref $name) { - $c->log->warn( Carp::shortmess(qq(Found results for "${name}" using regexp fallback)) ); - $c->log->warn( 'Relying on the regexp fallback behavior for component resolution' ); - $c->log->warn( 'is unreliable and unsafe. You have been warned' ); - } - - return @result; - } - - return; - } - - return $c->component( $c->action->class ); -} +sub controller { shift->_lookup_mvc('controller', @_) } =head2 $c->model($name) @@ -615,64 +593,7 @@ If you want to search for models, pass in a regexp as the argument. =cut -sub model { - my ( $c, $name, @args ) = @_; - my $appclass = ref($c) || $c; - my $container = $c->container->get_sub_container('model'); - - if( $name ) { - if ( !ref $name && $container->has_service($name)) { # Direct component hash lookup to avoid costly regexps - return $container->resolve( service => $name, parameters => { context => [ $c, @args ] } ); - } - - return - if $c->config->{disable_component_resolution_regex_fallback} && !ref $name; - - my $query = ref $name ? $name : qr{$name}; - $query =~ s/^${appclass}::(M|Model):://; - my @comps = $container->get_service_list; - my @result; - for (@comps) { - push @result, $container->resolve( service => $_, parameters => { context => [ $c, @args ] } ) - if m/$query/; - } - - if (@result) { - if (!ref $name) { - $c->log->warn( Carp::shortmess(qq(Found results for "${name}" using regexp fallback)) ); - $c->log->warn( 'Relying on the regexp fallback behavior for component resolution' ); - $c->log->warn( 'is unreliable and unsafe. You have been warned' ); - } - - return @result; - } - - return; - } - - if (ref $c) { - 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( $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 $container->resolve( service => $comp, parameters => { context => [ $c, @args ] } ); -} - +sub model { shift->_lookup_mvc('model', @_) } =head2 $c->view($name) @@ -695,66 +616,23 @@ If you want to search for views, pass in a regexp as the argument. =cut -sub view { - my ( $c, $name, @args ) = @_; - my $appclass = ref($c) || $c; - my $container = $c->container->get_sub_container('view'); - - if( $name ) { - if ( !ref $name ) { # Direct component hash lookup to avoid costly regexps - if ( $container->has_service($name) ) { - return $container->resolve( service => $name, parameters => { context => [ $c, @args ] } ); - } - else { - $c->log->warn( "Attempted to use view '$name', but does not exist" ); - } - } - - return - if $c->config->{disable_component_resolution_regex_fallback} && !ref $name; - - my $query = ref $name ? $name : qr{$name}; - $query =~ s/^${appclass}::(V|View):://; - my @comps = $container->get_service_list; - my @result; - for (@comps) { - push @result, $container->resolve( service => $_, parameters => { context => [ $c, @args ] } ) - if m/$query/; - } +sub view { shift->_lookup_mvc('view', @_) } - if (@result) { - if (!ref $name) { - $c->log->warn( Carp::shortmess(qq(Found results for "${name}" using regexp fallback)) ); - $c->log->warn( 'Relying on the regexp fallback behavior for component resolution' ); - $c->log->warn( 'is unreliable and unsafe. You have been warned' ); - } - - return @result; - } - - return; - } - - if (ref $c) { - 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}; +sub _lookup_mvc { + my ( $c, $type, $name, @args ) = @_; - my( $comp, $rest ) = $container->get_service_list; + if (ref $c && !$name) { + my $current_instance = $c->stash->{"current_${type}_instance"}; + return $current_instance + if $current_instance && $type ne 'controller'; - 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 = $type eq 'controller' + ? Catalyst::Utils::class2classshortsuffix($c->action->class) + : $c->stash->{"current_${type}"} + ; } - return $container->resolve( service => $comp, parameters => { context => [ $c, @args ] } ); + return $c->container->get_component_from_sub_container($type, $name, $c, @args); } =head2 $c->controllers @@ -803,57 +681,50 @@ should be used instead. If C<$name> is a regexp, a list of components matched against the full component name will be returned. -If Catalyst can't find a component by name, it will fallback to regex -matching by default. To disable this behaviour set -disable_component_resolution_regex_fallback to a true value. - - __PACKAGE__->config( disable_component_resolution_regex_fallback => 1 ); - =cut sub component { my ( $c, $component, @args ) = @_; - if ( $component ) { - my ($type, $name) = _get_component_type_name($component); + unless ($component) { + $c->log->warn('Calling $c->component with no args is deprecated and '); + $c->log->warn('will be removed in a future release.'); + $c->log->warn('Use $c->component_list instead.'); + return $c->component_list; + } - if ($type && $c->container->has_sub_container($type)) { - my $container = $c->container->get_sub_container($type); + my @result = $c->container->find_component( $component, $c, @args ); - if( !ref $component && $container->has_service($name) ) { - return $container->resolve( service => $name, parameters => { context => [ $c, @args ] } ); - } - } + # list context for regexp searches + return @result if ref $component; + + # only one component (if it's found) for string searches + return shift @result if @result; - return - if $c->config->{disable_component_resolution_regex_fallback}; + if (ref $c eq $component) { + $c->log->warn('You are calling $c->comp("MyApp"). This behaviour is'); + $c->log->warn('deprecated, and will be removed in a future release.'); + return $c; + } - # This is here so $c->comp( '::M::' ) works - my $query = ref $component ? $component : qr{$component}i; + $c->log->warn("Looking for '$component', but nothing was found."); - 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{$query} } @components; + # 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 deprecated and will be'); + $c->log->warn('removed in a future release. Use $c->component_list'); + $c->log->warn('instead.'); - if (@result) { - return map { $subcontainer->resolve( service => $_, parameters => { context => [$c, @args] } ) } @result - if ref $component; + return $c->component_list; +} - $c->log->warn( Carp::shortmess(qq(Found results for "${component}" using regexp fallback)) ); - $c->log->warn( 'Relying on the regexp fallback behavior for component resolution' ); - $c->log->warn( 'is unreliable and unsafe. You have been warned' ); +=head2 $c->component_list - return $subcontainer->resolve( service => $result[0], parameters => { context => [$c, @args] } ); - } - } +Returns the sorted list of the component names of the application. - # I would expect to return an empty list here, but that breaks back-compat - } +=cut - # fallback - return sort keys %{ $c->components }; -} +sub component_list { sort keys %{ shift->components } } =head2 CLASS DATA AND HELPER CLASSES @@ -1019,7 +890,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 ); @@ -1089,7 +960,10 @@ sub setup { $class->setup_log( delete $flags->{log} ); $class->setup_plugins( delete $flags->{plugins} ); $class->setup_dispatcher( delete $flags->{dispatcher} ); - $class->setup_engine( delete $flags->{engine} ); + if (my $engine = delete $flags->{engine}) { + $class->log->warn("Specifying the engine in ->setup is no longer supported, see Catalyst::Upgrading"); + } + $class->setup_engine(); $class->setup_stats( delete $flags->{stats} ); for my $flag ( sort keys %{$flags} ) { @@ -1148,25 +1022,17 @@ EOF $class->setup unless $Catalyst::__AM_RESTARTING; } - # Initialize our data structure - $class->components( {} ); - $class->setup_components; - if ( $class->debug ) { + if ( + $class->debug and + my $comps = $class->container->get_all_components($class) + ) { my $column_width = Catalyst::Utils::term_width() - 8 - 9; my $t = Text::SimpleTable->new( [ $column_width, 'Class' ], [ 8, 'Type' ] ); - for my $comp ( sort keys %{ $class->components } ) { - my $type = ref $class->components->{$comp} ? 'instance' : 'class'; - $t->row( $comp, $type ); - } - $class->log->debug( "Loaded components:\n" . $t->draw . "\n" ) - if ( keys %{ $class->components } ); - } + $t->row( $_ => ref($comps->{$_}) ? 'instance' : 'class' ) for keys %$comps; - # Add our self to components, since we are also a component - if( $class->isa('Catalyst::Controller') ){ - $class->components->{$class} = $class; + $class->log->debug( "Loaded components:\n" . $t->draw . "\n" ); } $class->setup_actions; @@ -1566,33 +1432,20 @@ Returns a hash of components. =cut -around components => sub { - my $orig = shift; - my $class = shift; - my $comps = shift; - - return $class->$orig if ( !$comps ); - -# FIXME: should this ugly kludge exist? - $class->setup_config unless defined $class->container; +sub components { + my ( $class, $comps ) = @_; -# FIXME: should there be a warning here, not to use this accessor to create the components? - my $components = {}; + # people create components calling this sub directly, before setup + $class->setup_config unless $class->container; - my $containers; - $containers->{$_} = $class->container->get_sub_container($_) for qw(model view controller); + my $container = $class->container; - for my $component ( keys %$comps ) { - $components->{ $component } = $comps->{$component}; - - my ($type, $name) = _get_component_type_name($component); - -# FIXME: shouldn't the service name be $name? - $containers->{$type}->add_service(Catalyst::BlockInjection->new( name => $name, block => sub { return $class->setup_component($component) } )); + if ( $comps ) { + $container->add_component( $_ ) for keys %$comps; } - return $class->$orig($components); -}; + return $container->get_all_components($class); +} =head2 $c->context_class @@ -1860,6 +1713,7 @@ sub finalize_headers { $response->body( qq{
This item has moved here.
} ); + $response->content_type('text/html; charset=utf-8'); } } @@ -1869,9 +1723,9 @@ sub finalize_headers { # get the length from a filehandle if ( blessed( $response->body ) && $response->body->can('read') || ref( $response->body ) eq 'GLOB' ) { - my $stat = stat $response->body; - if ( $stat && $stat->size > 0 ) { - $response->content_length( $stat->size ); + my $size = -s $response->body; + if ( $size ) { + $response->content_length( $size ); } else { $c->log->warn('Serving filehandle without a content-length'); @@ -1945,7 +1799,7 @@ sub handle_request { # Always expect worst case! my $status = -1; - eval { + try { if ($class->debug) { my $secs = time - $START || 1; my $av = sprintf '%.3f', $COUNT / $secs; @@ -1956,12 +1810,11 @@ sub handle_request { my $c = $class->prepare(@arguments); $c->dispatch; $status = $c->finalize; - }; - - if ( my $error = $@ ) { - chomp $error; - $class->log->error(qq/Caught exception in engine "$error"/); } + catch { + chomp(my $error = $_); + $class->log->error(qq/Caught exception in engine "$error"/); + }; $COUNT++; @@ -1998,28 +1851,38 @@ sub prepare { $c->res->headers->header( 'X-Catalyst' => $Catalyst::VERSION ); } - #XXX reuse coderef from can - # Allow engine to direct the prepare flow (for POE) - if ( $c->engine->can('prepare') ) { - $c->engine->prepare( $c, @arguments ); - } - else { - $c->prepare_request(@arguments); - $c->prepare_connection; - $c->prepare_query_parameters; - $c->prepare_headers; - $c->prepare_cookies; - $c->prepare_path; - - # Prepare the body for reading, either by prepare_body - # or the user, if they are using $c->read - $c->prepare_read; - - # Parse the body unless the user wants it on-demand - unless ( ref($c)->config->{parse_on_demand} ) { - $c->prepare_body; + try { + # Allow engine to direct the prepare flow (for POE) + if ( my $prepare = $c->engine->can('prepare') ) { + $c->engine->$prepare( $c, @arguments ); + } + else { + $c->prepare_request(@arguments); + $c->prepare_connection; + $c->prepare_query_parameters; + $c->prepare_headers; + $c->prepare_cookies; + $c->prepare_path; + + # Prepare the body for reading, either by prepare_body + # or the user, if they are using $c->read + $c->prepare_read; + + # Parse the body unless the user wants it on-demand + unless ( ref($c)->config->{parse_on_demand} ) { + $c->prepare_body; + } } } + # VERY ugly and probably shouldn't rely on ->finalize actually working + catch { + # failed prepare is always due to an invalid request, right? + $c->response->status(400); + $c->response->content_type('text/plain'); + $c->response->body('Bad Request'); + $c->finalize; + die $_; + }; my $method = $c->req->method || ''; my $path = $c->req->path; @@ -2398,7 +2261,12 @@ Starts the engine. =cut -sub run { my $c = shift; return $c->engine->run( $c, @_ ) } +sub run { + my $app = shift; + $app->engine_loader->needs_psgi_engine_compat_hack ? + $app->engine->run($app, @_) : + $app->engine->run( $app, $app->_finalized_psgi_app, @_ ); +} =head2 $c->set_action( $action, $code, $namespace, $attrs ) @@ -2425,20 +2293,25 @@ sub setup_config { my %args = %{ $class->config || {} }; - my @container_classes = ( "${class}::Container", 'Catalyst::Container'); - unshift @container_classes, delete $args{container_class} if exists $args{container_class}; + my $container_class; - my $container_class = Class::MOP::load_first_existing_class(@container_classes); + if ( exists $args{container_class} ) { + $container_class = delete $args{container_class}; + Class::MOP::load_class($container_class); + } + else { + $container_class = Class::MOP::load_first_existing_class("${class}::Container", 'Catalyst::IOC::Container'); + } - my $container = $container_class->new( %args, name => "$class" ); + my $container = $container_class->new( %args, application_name => "$class", name => "$class" ); $class->container($container); - my $config = $container->resolve(service => 'config'); + my $config = $container->resolve( service => 'config' ); $class->config($config); $class->finalize_config; # back-compat } -=head $c->finalize_config +=head2 $c->finalize_config =cut @@ -2449,164 +2322,29 @@ sub finalize_config { } This method is called internally to set up the application's components. It finds modules by calling the L