X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst.pm;h=1b47918ad1ee3708386a9e5e8e7be9db81c43a62;hb=2328d24b6808f6d51f7338858bed5d0f12b95821;hp=352f762f48557fc0d955fef771ac305b5f121d0d;hpb=f5dfb6ffcafcb88c7072329e7ec45fef6e04d301;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 352f762..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 (); @@ -27,11 +25,17 @@ 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 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; } @@ -68,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 ) = @_; @@ -93,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); @@ -113,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 @@ -556,22 +570,7 @@ If you want to search for controllers, pass in a regexp as the argument. =cut -sub controller { - my ( $c, $name, @args ) = @_; - -# FIXME: should this be a Catalyst::Utils method? - if (!$name) { - my $class = $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); -} +sub controller { shift->_lookup_mvc('controller', @_) } =head2 $c->model($name) @@ -594,20 +593,7 @@ If you want to search for models, pass in a regexp as the argument. =cut -sub model { - my ( $c, $name, @args ) = @_; - - if (ref $c && !$name) { - return $c->stash->{current_model_instance} - if $c->stash->{current_model_instance}; - - $name = $c->stash->{current_model} - if $c->stash->{current_model}; - } - - return $c->container->get_component_from_sub_container( 'model', $name, $c, @args); -} - +sub model { shift->_lookup_mvc('model', @_) } =head2 $c->view($name) @@ -630,18 +616,23 @@ If you want to search for views, pass in a regexp as the argument. =cut -sub view { - my ( $c, $name, @args ) = @_; +sub view { shift->_lookup_mvc('view', @_) } + +sub _lookup_mvc { + my ( $c, $type, $name, @args ) = @_; if (ref $c && !$name) { - return $c->stash->{current_view_instance} - if $c->stash->{current_view_instance}; + my $current_instance = $c->stash->{"current_${type}_instance"}; + return $current_instance + if $current_instance && $type ne 'controller'; - $name = $c->stash->{current_view} - if $c->stash->{current_view}; + $name = $type eq 'controller' + ? Catalyst::Utils::class2classshortsuffix($c->action->class) + : $c->stash->{"current_${type}"} + ; } - return $c->container->get_component_from_sub_container( 'view', $name, $c, @args); + return $c->container->get_component_from_sub_container($type, $name, $c, @args); } =head2 $c->controllers @@ -695,58 +686,45 @@ component name will be returned. sub component { my ( $c, $component, @args ) = @_; - if ( $component ) { - if (ref $component) { - my @result; - 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; + 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; + } - return map { $subcontainer->get_component( $_, $c, @args ) } @result - if @result; - } + my @result = $c->container->find_component( $component, $c, @args ); - # it expects an empty list on failed searches - return @result; - } - else { - my ($type, $name) = _get_component_type_name($component); + # list context for regexp searches + return @result if ref $component; - if ($type && $c->container->has_sub_container($type)) { - my $container = $c->container->get_sub_container($type); + # only one component (if it's found) for string searches + return shift @result if @result; - if ( !ref $component && $container->has_service($name) ) { - return $container->get_component( $name, $c, @args ); - } + 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; + } - return $container->get_component_regexp( $name, $c, @args ); - } - else { - 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 = first { $_ eq $component } @components; - - return $subcontainer->get_component( $result, $c, @args ) - if $result; - } - } - } + $c->log->warn("Looking for '$component', but nothing was found."); - # 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; + # 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.'); - $c->log->warn("Looking for '$component', but nothing was found."); + return $c->component_list; +} - # I would expect to return an empty list here, but that breaks back-compat - } +=head2 $c->component_list - # fallback - return sort keys %{ $c->components }; -} +Returns the sorted list of the component names of the application. + +=cut + +sub component_list { sort keys %{ shift->components } } =head2 CLASS DATA AND HELPER CLASSES @@ -912,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 ); @@ -982,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} ) { @@ -1041,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; @@ -1459,32 +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; - -# FIXME: should there be a warning here, not to use this accessor to create the components? - my $components = {}; +sub components { + my ( $class, $comps ) = @_; - my $containers; - $containers->{$_} = $class->container->get_sub_container($_) for qw(model view controller); + # people create components calling this sub directly, before setup + $class->setup_config unless $class->container; - for my $component ( keys %$comps ) { - $components->{ $component } = $comps->{$component}; + my $container = $class->container; - my ($type, $name) = _get_component_type_name($component); - - $containers->{$type}->add_service(Catalyst::IOC::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 @@ -1752,6 +1713,7 @@ sub finalize_headers { $response->body( qq{
This item has moved here.
} ); + $response->content_type('text/html; charset=utf-8'); } } @@ -1761,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'); @@ -1837,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; @@ -1848,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++; @@ -1890,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; @@ -2290,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 ) @@ -2317,15 +2293,20 @@ sub setup_config { my %args = %{ $class->config || {} }; - my @container_classes = ( "${class}::Container", 'Catalyst::IOC::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 } @@ -2341,174 +2322,29 @@ sub finalize_config { } This method is called internally to set up the application's components. It finds modules by calling the L