X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst.pm;h=02f1216efe5a7af6853a8e00a97ebfd5c72ead0f;hp=dd0d1ed8e827f5710399feba9e917364c396d33c;hb=a842f4920bb47e65459bd0ea5df3a21e1ea2497f;hpb=596aaffea2f27d13f0725b72a5b9f79a826d285b diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index dd0d1ed..02f1216 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -19,12 +19,14 @@ use Path::Class::Dir (); use Path::Class::File (); use Time::HiRes qw/gettimeofday tv_interval/; use URI (); +use URI::http; +use URI::https; use Scalar::Util qw/weaken blessed/; use Tree::Simple qw/use_weak_refs/; use Tree::Simple::Visitor::FindByUID; use attributes; use utf8; -use Carp qw/croak/; +use Carp qw/croak carp/; BEGIN { require 5.008001; } @@ -32,8 +34,6 @@ __PACKAGE__->mk_accessors( qw/counter request response state action stack namespace stats/ ); -attributes->import( __PACKAGE__, \&namespace, 'lvalue' ); - sub depth { scalar @{ shift->stack || [] }; } # Laziness++ @@ -49,19 +49,22 @@ our $COUNT = 1; our $START = time; our $RECURSION = 1000; our $DETACH = "catalyst_detach\n"; +our $GO = "catalyst_go\n"; __PACKAGE__->mk_classdata($_) for qw/components arguments dispatcher engine log dispatcher_class - engine_class context_class request_class response_class setup_finished/; + engine_class context_class request_class response_class stats_class + setup_finished/; __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.7006'; +our $VERSION = '5.7099_03'; sub import { my ( $class, @arguments ) = @_; @@ -87,16 +90,20 @@ Catalyst - The Elegant MVC Web Application Framework =head1 SYNOPSIS +See the L distribution for comprehensive +documentation and tutorials. + # Install Catalyst::Devel for helpers and other development tools # use the helper to create a new application catalyst.pl MyApp # add models, views, controllers - script/myapp_create.pl model Database DBIC::SchemaLoader dbi:SQLite:/path/to/db - script/myapp_create.pl view TT TT + script/myapp_create.pl model MyDatabase DBIC::Schema create=dynamic dbi:SQLite:/path/to/db + script/myapp_create.pl view MyTemplate TT script/myapp_create.pl controller Search # built in testserver -- use -r to restart automatically on changes + # --help to see all available options script/myapp_server.pl # command line testing interface @@ -105,7 +112,7 @@ Catalyst - The Elegant MVC Web Application Framework ### in lib/MyApp.pm use Catalyst qw/-Debug/; # include plugins here as well - ### In lib/MyApp/Controller/Root.pm (autocreated) + ### In lib/MyApp/Controller/Root.pm (autocreated) sub foo : Global { # called for /foo, /foo/1, /foo/1/2, etc. my ( $self, $c, @args ) = @_; # args are qw/1 2/ for /foo/1/2 $c->stash->{template} = 'foo.tt'; # set the template @@ -224,10 +231,27 @@ Forces Catalyst to use a specific home directory, e.g.: use Catalyst qw[-Home=/usr/mst]; +This can also be done in the shell environment by setting either the +C environment variable or C; where C +is replaced with the uppercased name of your application, any "::" in +the name will be replaced with underscores, e.g. MyApp::Web should use +MYAPP_WEB_HOME. If both variables are set, the MYAPP_HOME one will be used. + =head2 -Log Specifies log level. +=head2 -Stats + +Enables statistics collection and reporting. You can also force this setting +from the system environment with CATALYST_STATS or _STATS. The +environment settings override the application, with _STATS having the +highest priority. + +e.g. + + use Catalyst qw/-Stats=1/ + =head1 METHODS =head2 INFORMATION ABOUT THE CURRENT REQUEST @@ -262,8 +286,8 @@ cookies, HTTP headers, etc.). See L. Forwards processing to another action, by its private name. If you give a class name but no method, C is called. You may also optionally pass arguments in an arrayref. The action will receive the arguments in -C<@_> and C<$c-Ereq-Eargs>. Upon returning from the function, -C<$c-Ereq-Eargs> will be restored to the previous values. +C<@_> and C<< $c->req->args >>. Upon returning from the function, +C<< $c->req->args >> will be restored to the previous values. Any data Ced from the action forwarded to, will be returned by the call to forward. @@ -304,11 +328,25 @@ When called with no arguments it escapes the processing chain entirely. sub detach { my $c = shift; $c->dispatcher->detach( $c, @_ ) } +=head2 $c->go( $action [, \@arguments ] ) + +=head2 $c->go( $class, $method, [, \@arguments ] ) + +Almost the same as C, but does a full dispatch, instead of just +calling the new C<$action> / C<$class-E$method>. This means that C, +C and the method you go to is called, just like a new request. + +C<$c-Estash> is kept unchanged. + +=cut + +sub go { my $c = shift; $c->dispatcher->go( $c, @_ ) } + =head2 $c->response =head2 $c->res -Returns the current L object, q.v. +Returns the current L object, see there for details. =head2 $c->stash @@ -325,7 +363,7 @@ Catalyst). $c->stash( bar => 1, gorch => 2 ); # equivalent to passing a hashref # stash is automatically passed to the view for use in a template - $c->forward( 'MyApp::V::TT' ); + $c->forward( 'MyApp::View::TT' ); =cut @@ -333,7 +371,7 @@ sub stash { my $c = shift; if (@_) { my $stash = @_ > 1 ? {@_} : $_[0]; - croak('stash takes a hash or hashref') unless ref $stash; + croak('stash takes a hash or hashref') unless ref $stash; foreach my $key ( keys %$stash ) { $c->{stash}->{$key} = $stash->{$key}; } @@ -349,7 +387,7 @@ sub stash { Returns an arrayref containing error messages. If Catalyst encounters an error while processing a request, it stores the error in $c->error. This -method should not be used to store non-fatal error messages. +method should only be used to store fatal error messages. my @error = @{ $c->error }; @@ -391,87 +429,66 @@ sub clear_errors { $c->error(0); } +# search components given a name and some prefixes +sub _comp_search_prefixes { + my ( $c, $name, @prefixes ) = @_; + my $appclass = ref $c || $c; + my $filter = "^${appclass}::(" . join( '|', @prefixes ) . ')::'; -# search via regex -sub _comp_search { - my ( $c, @names ) = @_; - - foreach my $name (@names) { - foreach my $component ( keys %{ $c->components } ) { - return $c->components->{$component} if $component =~ /$name/i; - } - } + # map the original component name to the sub part that we will search against + my %eligible = map { my $n = $_; $n =~ s{^$appclass\::[^:]+::}{}; $_ => $n; } + grep { /$filter/ } keys %{ $c->components }; - return undef; -} + # undef for a name will return all + return keys %eligible if !defined $name; -# try explicit component names -sub _comp_explicit { - my ( $c, @names ) = @_; + my $query = ref $name ? $name : qr/^$name$/i; + my @result = grep { $eligible{$_} =~ m{$query} } keys %eligible; - foreach my $try (@names) { - return $c->components->{$try} if ( exists $c->components->{$try} ); - } + return map { $c->components->{ $_ } } @result if @result; - return undef; -} + # if we were given a regexp to search against, we're done. + return if ref $name; -# like component, but try just these prefixes before regex searching, -# and do not try to return "sort keys %{ $c->components }" -sub _comp_prefixes { - my ( $c, $name, @prefixes ) = @_; + # regexp fallback + $query = qr/$name/i; + @result = map { $c->components->{ $_ } } grep { $eligible{ $_ } =~ m{$query} } keys %eligible; - my $appclass = ref $c || $c; + # no results? try against full names + if( !@result ) { + @result = map { $c->components->{ $_ } } grep { m{$query} } keys %eligible; + } - my @names = map { "${appclass}::${_}::${name}" } @prefixes; + # don't warn if we didn't find any results, it just might not exist + if( @result ) { + $c->log->warn( qq(Found results for "${name}" using regexp fallback.) ); + $c->log->warn( 'Relying on the regexp fallback behavior for component resolution is unreliable and unsafe.' ); + $c->log->warn( 'If you really want to search, pass in a regexp as the argument.' ); + } - my $comp = $c->_comp_explicit(@names); - return $comp if defined($comp); - $comp = $c->_comp_search($name); - return $comp; + return @result; } # Find possible names for a prefix - sub _comp_names { my ( $c, @prefixes ) = @_; - my $appclass = ref $c || $c; - my @pre = map { "${appclass}::${_}::" } @prefixes; - - my @names; - - COMPONENT: foreach my $comp ($c->component) { - foreach my $p (@pre) { - if ($comp =~ s/^$p//) { - push(@names, $comp); - next COMPONENT; - } - } - } + my $filter = "^${appclass}::(" . join( '|', @prefixes ) . ')::'; + my @names = map { s{$filter}{}; $_; } $c->_comp_search_prefixes( undef, @prefixes ); return @names; } -# Return a component if only one matches. -sub _comp_singular { - my ( $c, @prefixes ) = @_; - - my $appclass = ref $c || $c; - - my ( $comp, $rest ) = - map { $c->_comp_search("^${appclass}::${_}::") } @prefixes; - return $comp unless $rest; -} - # Filter a component before returning by calling ACCEPT_CONTEXT if available sub _filter_component { my ( $c, $comp, @args ) = @_; + if ( eval { $comp->can('ACCEPT_CONTEXT'); } ) { return $comp->ACCEPT_CONTEXT( $c, @args ); } - else { return $comp } + + return $comp; } =head2 COMPONENT ACCESSORS @@ -485,13 +502,23 @@ Gets a L instance by name. 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 ) = @_; - return $c->_filter_component( $c->_comp_prefixes( $name, qw/Controller C/ ), - @args ) - if ($name); + + 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 ); } @@ -501,40 +528,50 @@ Gets a L instance by name. $c->model('Foo')->do_stuff; +Any extra arguments are directly passed to ACCEPT_CONTEXT. + If the name is omitted, it will look for - - a model object in $c->stash{current_model_instance}, then + - a model object in $c->stash->{current_model_instance}, then - a model name in $c->stash->{current_model}, then - a config setting 'default_model', or - check if there is only one model, and return it if that's the case. +If you want to search for models, pass in a regexp as the argument. + + # find all models that start with Foo + my @foo_models = $c->model(qr{^Foo}); + =cut sub model { my ( $c, $name, @args ) = @_; - return $c->_filter_component( $c->_comp_prefixes( $name, qw/Model M/ ), - @args ) - if $name; + + 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 ); + } + 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( $c->config->{default_model} ) - if $c->config->{default_model}; } - return $c->_filter_component( $c->_comp_singular(qw/Model M/), @args ); - -} - -=head2 $c->controllers + return $c->model( $c->config->{default_model} ) + if $c->config->{default_model}; -Returns the available names which can be passed to $c->controller + my( $comp, $rest ) = $c->_comp_search_prefixes( undef, qw/Model M/); -=cut + if( $rest ) { + $c->log->warn( '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.80, the "random" behavior will not work at all.' ); + } -sub controllers { - my ( $c ) = @_; - return $c->_comp_names(qw/Controller C/); + return $c->_filter_component( $comp ); } @@ -544,28 +581,61 @@ Gets a L instance by name. $c->view('Foo')->do_stuff; +Any extra arguments are directly passed to ACCEPT_CONTEXT. + If the name is omitted, it will look for - - a view object in $c->stash{current_view_instance}, then + - a view object in $c->stash->{current_view_instance}, then - a view name in $c->stash->{current_view}, then - a config setting 'default_view', or - check if there is only one view, and return it if that's the case. +If you want to search for views, pass in a regexp as the argument. + + # find all views that start with Foo + my @foo_views = $c->view(qr{^Foo}); + =cut sub view { my ( $c, $name, @args ) = @_; - return $c->_filter_component( $c->_comp_prefixes( $name, qw/View V/ ), - @args ) - if $name; + + 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 ); + } + 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( $c->config->{default_view} ) - if $c->config->{default_view}; } - return $c->_filter_component( $c->_comp_singular(qw/View V/) ); + 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.80, the "random" behavior will not work at all.' ); + } + + return $c->_filter_component( $comp ); +} + +=head2 $c->controllers + +Returns the available names which can be passed to $c->controller + +=cut + +sub controllers { + my ( $c ) = @_; + return $c->_comp_names(qw/Controller C/); } =head2 $c->models @@ -595,40 +665,57 @@ sub views { =head2 $c->component($name) -Gets a component object by name. This method is no longer recommended, +Gets a component object by name. This method is not recommended, unless you want to get a specific component by full -class. C<$c-Econtroller>, C<$c-Emodel>, and C<$c-Eview> +class. C<< $c->controller >>, C<< $c->model >>, and C<< $c->view >> should be used instead. +If C<$name> is a regexp, a list of components matched against the full +component name will be returned. + =cut sub component { - my $c = shift; + my ( $c, $name, @args ) = @_; - if (@_) { + if( $name ) { + my $comps = $c->components; - my $name = shift; + if( !ref $name ) { + # is it the exact name? + return $c->_filter_component( $comps->{ $name }, @args ) + if exists $comps->{ $name }; - my $appclass = ref $c || $c; + # perhaps we just omitted "MyApp"? + my $composed = ( ref $c || $c ) . "::${name}"; + return $c->_filter_component( $comps->{ $composed }, @args ) + if exists $comps->{ $composed }; - my @names = ( - $name, "${appclass}::${name}", - map { "${appclass}::${_}::${name}" } - qw/Model M Controller C View V/ - ); + # search all of the models, views and controllers + my( $comp ) = $c->_comp_search_prefixes( $name, qw/Model M Controller C View V/ ); + return $c->_filter_component( $comp, @args ) if $comp; + } + + # This is here so $c->comp( '::M::' ) works + my $query = ref $name ? $name : qr{$name}i; - my $comp = $c->_comp_explicit(@names); - return $c->_filter_component( $comp, @_ ) if defined($comp); + my @result = grep { m{$query} } keys %{ $c->components }; + return map { $c->_filter_component( $_, @args ) } @result if ref $name; + + if( $result[ 0 ] ) { + $c->log->warn( 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 $c->_filter_component( $result[ 0 ], @args ); + } - $comp = $c->_comp_search($name); - return $c->_filter_component( $comp, @_ ) if defined($comp); + # I would expect to return an empty list here, but that breaks back-compat } + # fallback return sort keys %{ $c->components }; } - - =head2 CLASS DATA AND HELPER CLASSES =head2 $c->config @@ -637,8 +724,9 @@ Returns or takes a hashref containing the application's configuration. __PACKAGE__->config( { db => 'dsn:SQLite:foo.db' } ); -You can also use a L config file like myapp.yml in your -applications home directory. +You can also use a C, C or C config file +like myapp.yml in your applications home directory. See +L. --- db: dsn:SQLite:foo.db @@ -699,8 +787,8 @@ L. =head2 $c->path_to(@path) -Merges C<@path> with C<$c-Econfig-E{home}> and returns a -L object. +Merges C<@path> with C<< $c->config->{home} >> and returns a +L object. For example: @@ -796,6 +884,7 @@ sub setup { $class->setup_plugins( delete $flags->{plugins} ); $class->setup_dispatcher( delete $flags->{dispatcher} ); $class->setup_engine( delete $flags->{engine} ); + $class->setup_stats( delete $flags->{stats} ); for my $flag ( sort keys %{$flags} ) { @@ -889,6 +978,9 @@ If the last argument to C is a hash reference, it is assumed to contain GET parameter key/value pairs, which will be appended to the URI in standard fashion. +Note that uri_for is destructive to the passed hashref. Subsequent calls +with the same hashref may have unintended results. + Instead of C<$path>, you can also optionally pass a C<$action> object which will be resolved to a path using C<< $c->dispatcher->uri_for_action >>; if the first element of @@ -899,11 +991,6 @@ to C. sub uri_for { my ( $c, $path, @args ) = @_; - my $base = $c->request->base->clone; - my $basepath = $base->path; - $basepath =~ s/\/$//; - $basepath .= '/'; - my $namespace = $c->namespace || ''; if ( Scalar::Util::blessed($path) ) { # action object my $captures = ( scalar @args && ref $args[0] eq 'ARRAY' @@ -914,32 +1001,53 @@ sub uri_for { $path = '/' if $path eq ''; } - # massage namespace, empty if absolute path - $namespace =~ s/^\/// if $namespace; - $namespace .= '/' if $namespace; - $path ||= ''; - $namespace = '' if $path =~ /^\//; - $path =~ s/^\///; - $path =~ s/\?/%3F/g; + undef($path) if (defined $path && $path eq ''); my $params = ( scalar @args && ref $args[$#args] eq 'HASH' ? pop @args : {} ); - for my $value ( values %$params ) { - next unless defined $value; - for ( ref $value eq 'ARRAY' ? @$value : $value ) { - $_ = "$_"; - utf8::encode( $_ ); + carp "uri_for called with undef argument" if grep { ! defined $_ } @args; + s/([^$URI::uric])/$URI::Escape::escapes{$1}/go for @args; + + unshift(@args, $path); + + unless (defined $path && $path =~ s!^/!!) { # in-place strip + my $namespace = $c->namespace; + if (defined $path) { # cheesy hack to handle path '../foo' + $namespace =~ s{(?:^|/)[^/]+$}{} while $args[0] =~ s{^\.\./}{}; } - }; + unshift(@args, $namespace || ''); + } # join args with '/', or a blank string - my $args = ( scalar @args ? '/' . join( '/', map {s/\?/%3F/g; $_} @args ) : '' ); - $args =~ s/^\/// unless $path; - my $res = - URI->new_abs( URI->new_abs( "$path$args", "$basepath$namespace" ), $base ) - ->canonical; - $res->query_form(%$params); + my $args = join('/', grep { defined($_) } @args); + $args =~ s/\?/%3F/g; # STUPID STUPID SPECIAL CASE + $args =~ s!^/!!; + my $base = $c->req->base; + my $class = ref($base); + $base =~ s{(?{$_}; + s/([;\/?:@&=+,\$\[\]%])/$URI::Escape::escapes{$1}/go; + s/ /+/g; + my $key = $_; + $val = '' unless defined $val; + (map { + $_ = "$_"; + utf8::encode( $_ ) if utf8::is_utf8($_); + # using the URI::Escape pattern here so utf8 chars survive + s/([^A-Za-z0-9\-_.!~*'() ])/$URI::Escape::escapes{$1}/go; + s/ /+/g; + "${key}=$_"; } ( ref $val eq 'ARRAY' ? @$val : $val )); + } @keys); + } + + my $res = bless(\"${base}${args}${query}", $class); $res; } @@ -960,8 +1068,8 @@ sub welcome_message { "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> - - + + $name on Catalyst $VERSION