X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst.pm;h=f2688c9697e08a534c99da588bea9307a5dbd0ea;hb=12e4ddc404a4d6252fa01b69f5b75bf3e42db463;hp=13218e389ee6d79c13eb6a566289be5ba79ec986;hpb=6977fbffdeb690f6a372e52e42fc6453a201d066;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 13218e3..f2688c9 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -26,7 +26,7 @@ use Tree::Simple qw/use_weak_refs/; use Tree::Simple::Visitor::FindByUID; use attributes; use utf8; -use Carp qw/croak carp/; +use Carp qw/croak carp shortmess/; BEGIN { require 5.008001; } @@ -49,6 +49,7 @@ 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 @@ -63,7 +64,7 @@ __PACKAGE__->stats_class('Catalyst::Stats'); # Remember to update this in Catalyst::Runtime as well! -our $VERSION = '5.7014'; +our $VERSION = '5.71000'; sub import { my ( $class, @arguments ) = @_; @@ -97,7 +98,7 @@ documentation and tutorials. catalyst.pl MyApp # add models, views, controllers - script/myapp_create.pl model MyDatabase DBIC::Schema create=dynamic dbi:SQLite:/path/to/db + script/myapp_create.pl model MyDatabase DBIC::Schema create=static dbi:SQLite:/path/to/db script/myapp_create.pl view MyTemplate TT script/myapp_create.pl controller Search @@ -327,6 +328,47 @@ When called with no arguments it escapes the processing chain entirely. sub detach { my $c = shift; $c->dispatcher->detach( $c, @_ ) } +=head2 $c->visit( $action [, \@arguments ] ) + +=head2 $c->visit( $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 are called, just like a new request. + +In addition both C<< $c->action >> and C<< $c->namespace >> are localized. +This means, for example, that $c->action methods such as C, C and +C return information for the visited action when they are invoked +within the visited action. This is different from the behavior of C +which continues to use the $c->action object from the caller action even when +invoked from the callee. + +C<$c-Estash> is kept unchanged. + +In effect, C allows you to "wrap" another action, just as it +would have been called by dispatching from a URL, while the analogous +C allows you to transfer control to another action as if it had +been reached directly from a URL. + +=cut + +sub visit { my $c = shift; $c->dispatcher->visit( $c, @_ ) } + +=head2 $c->go( $action [, \@arguments ] ) + +=head2 $c->go( $class, $method, [, \@arguments ] ) + +Almost the same as C, but does a full dispatch like C, +instead of just calling the new C<$action> / +C<$class-E$method>. This means that C, C and the +method you visit are 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 @@ -437,12 +479,33 @@ sub _comp_search_prefixes { # regexp fallback $query = qr/$name/i; - @result = grep { $eligible{ $_ } =~ m{$query} } keys %eligible; + @result = map { $c->components->{ $_ } } grep { $eligible{ $_ } =~ m{$query} } keys %eligible; + + # no results? try against full names + if( !@result ) { + @result = map { $c->components->{ $_ } } grep { m{$query} } keys %eligible; + } # don't warn if we didn't find any results, it just might not exist if( @result ) { - $c->log->warn( 'Relying on the regexp fallback behavior for component resolution' ); - $c->log->warn( 'is unreliable and unsafe. You have been warned' ); + my $msg = "Used regexp fallback for \$c->model('${name}'), which found '" . + (join '", "', @result) . "'. Relying on regexp fallback behavior for " . + "component resolution is unreliable and unsafe."; + my $short = $result[0]; + $short =~ s/.*?Model:://; + my $shortmess = Carp::shortmess(''); + if ($shortmess =~ m#Catalyst/Plugin#) { + $msg .= " You probably need to set '$short' instead of '${name}' in this " . + "plugin's config"; + } elsif ($shortmess =~ m#Catalyst/lib/(View|Controller)#) { + $msg .= " You probably need to set '$short' instead of '${name}' in this " . + "component's config"; + } else { + $msg .= " You probably meant \$c->model('$short') instead of \$c->model{'${name}'}, " . + "but if you really wanted to search, pass in a regexp as the argument " . + "like so: \$c->model(qr/${name}/)"; + } + $c->log->warn( "${msg}$shortmess" ); } return @result; @@ -481,6 +544,12 @@ 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 { @@ -504,11 +573,16 @@ Gets a L instance by name. 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 { @@ -532,10 +606,11 @@ sub model { my( $comp, $rest ) = $c->_comp_search_prefixes( undef, qw/Model M/); if( $rest ) { - $c->log->warn( 'Calling $c->model() will return a random model unless you specify one of:' ); + $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.80, the "random" behavior will not work at all.' ); } return $c->_filter_component( $comp ); @@ -551,11 +626,16 @@ Gets a L instance by name. 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 { @@ -583,6 +663,7 @@ sub view { $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 ); @@ -631,6 +712,9 @@ unless you want to get a specific component by full 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 { @@ -641,11 +725,13 @@ sub component { if( !ref $name ) { # is it the exact name? - return $comps->{ $name } if exists $comps->{ $name }; + return $c->_filter_component( $comps->{ $name }, @args ) + if exists $comps->{ $name }; # perhaps we just omitted "MyApp"? my $composed = ( ref $c || $c ) . "::${name}"; - return $comps->{ $composed } if exists $comps->{ $composed }; + return $c->_filter_component( $comps->{ $composed }, @args ) + if exists $comps->{ $composed }; # search all of the models, views and controllers my( $comp ) = $c->_comp_search_prefixes( $name, qw/Model M Controller C View V/ ); @@ -656,8 +742,14 @@ sub component { my $query = ref $name ? $name : qr{$name}i; my @result = grep { m{$query} } keys %{ $c->components }; - return @result if ref $name; - return $result[ 0 ] if $result[ 0 ]; + return map { $c->_filter_component( $_, @args ) } @result if ref $name; + + if( $result[ 0 ] ) { + $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 $c->_filter_component( $result[ 0 ], @args ); + } # I would expect to return an empty list here, but that breaks back-compat } @@ -864,7 +956,8 @@ EOF my @plugins = map { "$_ " . ( $_->VERSION || '' ) } $class->registered_plugins; if (@plugins) { - my $t = Text::SimpleTable->new(74); + my $column_width = Catalyst::Utils::term_width() - 6; + my $t = Text::SimpleTable->new($column_width); $t->row($_) for @plugins; $class->log->debug( "Loaded plugins:\n" . $t->draw . "\n" ); } @@ -896,7 +989,8 @@ EOF $class->setup_components; if ( $class->debug ) { - my $t = Text::SimpleTable->new( [ 63, 'Class' ], [ 8, 'Type' ] ); + 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 ); @@ -919,23 +1013,75 @@ EOF $class->setup_finished(1); } +=head2 $c->uri_for( $action, \@captures?, @args?, \%query_values? ) + =head2 $c->uri_for( $path, @args?, \%query_values? ) -Merges path with C<< $c->request->base >> for absolute URIs and with -C<< $c->namespace >> for relative URIs, then returns a normalized L -object. If any args are passed, they are added at the end of the path. -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. +=over -Note that uri_for is destructive to the passed hashref. Subsequent calls -with the same hashref may have unintended results. +=item $action -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 -C<@args> is an arrayref it is treated as a list of captures to be passed -to C. +A Catalyst::Action object representing the Catalyst action you want to +create a URI for. To get one for an action in the current controller, +use C<< $c->action('someactionname') >>. To get one from different +controller, fetch the controller using C<< $c->controller() >>, then +call C on it. + +This method must be used to create URIs for +L actions. + +=item $path + +The actual path you wish to create a URI for, this is a public path, +not a private action path. + +=item \@captures + +If provided, this argument is used to insert values into a I +action in the parts where the definitions contain I. If +not needed, leave out this argument. + +=item @args + +If provided, this is used as a list of further path sections to append +to the URI. In a I action these are the equivalent to the +endpoint L. + +=item \%query_values + +If provided, the query_values hashref is used to add query parameters +to the URI, with the keys as the names, and the values as the values. + +=back + +Returns a L object. + + ## Ex 1: a path with args and a query parameter + $c->uri_for('user/list', 'short', { page => 2}); + ## -> ($c->req->base is 'http://localhost:3000/' + URI->new('http://localhost:3000/user/list/short?page=2) + + ## Ex 2: a chained view action that captures the user id + ## In controller: + sub user : Chained('/'): PathPart('myuser'): CaptureArgs(1) {} + sub viewuser : Chained('user'): PathPart('view') {} + + ## In uri creating code: + my $uaction = $c->controller('Users')->action_for('viewuser'); + $c->uri_for($uaction, [ 42 ]); + ## outputs: + URI->new('http://localhost:3000/myuser/42/view') + +Creates a URI object using C<< $c->request->base >> and a path. If an +Action object is given instead of a path, the path is constructed +using C<< $c->dispatcher->uri_for_action >> and passing it the +@captures array, if supplied. + +If any query parameters are passed they are added to the end of the +URI in the usual way. + +Note that uri_for is destructive to the passed query values hashref. +Subsequent calls with the same hashref may have unintended results. =cut @@ -1120,7 +1266,7 @@ sub welcome_message { they can save you a lot of work.

script/${prefix}_create.pl -help

Also, be sure to check out the vast and growing - collection of plugins for Catalyst on CPAN; + collection of plugins for Catalyst on CPAN; you are likely to find what you need there.

@@ -1234,7 +1380,12 @@ sub execute { my $last = pop( @{ $c->stack } ); if ( my $error = $@ ) { - if ( !ref($error) and $error eq $DETACH ) { die $DETACH if $c->depth > 1 } + if ( !ref($error) and $error eq $DETACH ) { + die $DETACH if($c->depth > 1); + } + elsif ( !ref($error) and $error eq $GO ) { + die $GO if($c->depth > 0); + } else { unless ( ref $error ) { no warnings 'uninitialized'; @@ -1860,6 +2011,11 @@ search paths, specify a key named C as an array reference. Items in the array beginning with C<::> will have the application class name prepended to them. +All components found will also have any +L loaded and set up as components. +Note, that modules which are B an I of the main +file namespace loaded will not be instantiated as components. + =cut sub setup_components { @@ -2365,13 +2521,15 @@ Wiki: =head2 L - The test suite. -=head1 CREDITS +=head1 PROJECT FOUNDER -Andy Grundman +sri: Sebastian Riedel -Andy Wardley +=head1 CONTRIBUTORS -Andreas Marienborg +abw: Andy Wardley + +acme: Leon Brocard Andrew Bramble @@ -2379,65 +2537,73 @@ Andrew Ford Andrew Ruthven -Arthur Bergman - -Autrijus Tang +andyg: Andy Grundman -Brian Cassidy +audreyt: Audrey Tang -Carl Franks +bricas: Brian Cassidy -Christian Hansen +chansen: Christian Hansen -Christopher Hicks +chicks: Christopher Hicks -Dan Sully +dkubb: Dan Kubb -Danijel Milicevic +Drew Taylor -David Kamholz +esskar: Sascha Kiefer -David Naughton +fireartist: Carl Franks -Drew Taylor +gabb: Danijel Milicevic Gary Ashton Jones Geoff Richards -Jesse Sheidlower +jcamacho: Juan Camacho -Jesse Vincent +jhannah: Jay Hannah Jody Belka Johan Lindstrom -Juan Camacho +jon: Jon Schutz -Leon Brocard +marcus: Marcus Ramberg -Marcus Ramberg +miyagawa: Tatsuhiko Miyagawa -Matt S Trout +mst: Matt S. Trout -Robert Sedlacek +mugwump: Sam Vilain -Sam Vilain +naughton: David Naughton -Sascha Kiefer +ningu: David Kamholz -Sebastian Willert +nothingmuch: Yuval Kogman -Tatsuhiko Miyagawa +numa: Dan Sully -Ulf Edvinsson +obra: Jesse Vincent + +omega: Andreas Marienborg + +Oleg Kostyuk + +phaylon: Robert Sedlacek -Yuval Kogman +sky: Arthur Bergman + +the_jester: Jesse Sheidlower + +Ulf Edvinsson -=head1 AUTHOR +willert: Sebastian Willert -Sebastian Riedel, C +batman: Jan Henning Thorsen =head1 LICENSE