X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FEngine.pm;h=4259d7c45fed4bc5dc1a38b99b385c391d5dcc5f;hb=a554cc3b4596813069727afe60e8e6cb2e701eeb;hp=41dc691276fea074b257173b5ee3db6ca24979f3;hpb=d6f060b780b7ff804165b1ff635a0244315b10b2;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 41dc691..4259d7c 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -68,96 +68,12 @@ sub action { $_[1] ? ( $action = {@_} ) : ( $action = shift ); if ( ref $action eq 'HASH' ) { while ( my ( $name, $code ) = each %$action ) { - my $prefix = ''; - my $class = caller(0); - if ( $name =~ /^\?(.*)$/ ) { - my $prefix = $1 || ''; - $name = $2; - $name = $prefix . _prefix( $class, $name ); - $self->actions->{plain}->{$name} = [ $class, $code ]; - } - if ( $name =~ /^\/(.*)\/$/ ) { - my $regex = $1; - $self->actions->{compiled}->{qr#$regex#} = $name; - $self->actions->{regex}->{$name} = [ $class, $code ]; - } - elsif ( $name =~ /^\!(.*)$/ ) { - $name = $1; - my $parent = $self->tree; - my $visitor = Tree::Simple::Visitor::FindByPath->new; - $prefix = _class2prefix($class); - for my $part ( split '/', $prefix ) { - $visitor->setSearchPath($part); - $parent->accept($visitor); - my $child = $visitor->getResult; - unless ($child) { - $child = $parent->addChild( Tree::Simple->new($part) ); - $visitor->setSearchPath($part); - $parent->accept($visitor); - $child = $visitor->getResult; - } - $parent = $child; - } - my $uid = $parent->getUID; - $self->actions->{private}->{$uid}->{$name} = [ $class, $code ]; - $name = "!$name"; - } - else { $self->actions->{plain}->{$name} = [ $class, $code ] } - my $reverse = $prefix ? "$name ($prefix)" : $name; - $self->actions->{reverse}->{"$code"} = $reverse; - $self->log->debug(qq/"$class" defined "$name" as "$code"/) - if $self->debug; + $self->set_action( $name, $code, caller(0) ); } } return 1; } -=item $c->find_action( $name, $namespace ) - -Find an action in a given namespace. - -=cut - -sub find_action { - my ( $c, $action, $namespace ) = @_; - $namespace ||= ''; - if ( $action =~ /^\!(.*)/ ) { - $action = $1; - my $parent = $c->tree; - my @results; - my $result = $c->actions->{private}->{ $parent->getUID }->{$action}; - push @results, [$result] if $result; - my $visitor = Tree::Simple::Visitor::FindByPath->new; - for my $part ( split '/', $namespace ) { - $visitor->setSearchPath($part); - $parent->accept($visitor); - my $child = $visitor->getResult; - my $uid = $child->getUID if $child; - my $match = $c->actions->{private}->{$uid}->{$action} if $uid; - push @results, [$match] if $match; - $parent = $child if $child; - } - return \@results; - } - elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] } - elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] } - else { - for my $regex ( keys %{ $c->actions->{compiled} } ) { - my $name = $c->actions->{compiled}->{$regex}; - if ( $action =~ $regex ) { - my @snippets; - for my $i ( 1 .. 9 ) { - no strict 'refs'; - last unless ${$i}; - push @snippets, ${$i}; - } - return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ]; - } - } - } - return []; -} - =item $c->benchmark($coderef) Takes a coderef with arguments and returns elapsed time as float. @@ -202,27 +118,27 @@ sub component { } } -=item $c->errors +=item $c->error -=item $c->errors($error, ...) +=item $c->error($error, ...) -=item $c->errors($arrayref) +=item $c->error($arrayref) -Returns an arrayref containing errors messages. +Returns an arrayref containing error messages. - my @errors = @{ $c->errors }; + my @error = @{ $c->error }; Add a new error. - $c->errors('Something bad happened'); + $c->error('Something bad happened'); =cut -sub errors { +sub error { my $c = shift; - my $errors = ref $_[0] eq 'ARRAY' ? $_[0] : [@_]; - push @{ $c->{errors} }, @$errors; - return $c->{errors}; + my $error = ref $_[0] eq 'ARRAY' ? $_[0] : [@_]; + push @{ $c->{error} }, @$error; + return $c->{error}; } =item $c->finalize @@ -240,13 +156,13 @@ sub finalize { $c->res->status(302); } - if ( !$c->res->output || $#{ $c->errors } >= 0 ) { + if ( !$c->res->output || $#{ $c->error } >= 0 ) { $c->res->headers->content_type('text/html'); my $name = $c->config->{name} || 'Catalyst Application'; - my ( $title, $errors, $infos ); + my ( $title, $error, $infos ); if ( $c->debug ) { - $errors = join '
', @{ $c->errors }; - $errors ||= 'No output'; + $error = join '
', @{ $c->error }; + $error ||= 'No output'; $title = $name = "$name on Catalyst $Catalyst::VERSION"; my $req = encode_entities Dumper $c->req; my $res = encode_entities Dumper $c->res; @@ -262,9 +178,9 @@ sub finalize { } else { - $title = $name; - $errors = ''; - $infos = <<""; + $title = $name; + $error = ''; + $infos = <<"";
 (en) Please come back later
 (de) Bitte versuchen sie es spaeter nocheinmal
@@ -298,7 +214,7 @@ sub finalize {
                 margin: 10px;
                 -moz-border-radius: 10px;
             }
-            div.errors {
+            div.error {
                 background-color: #977;
                 border: 1px solid #755;
                 padding: 8px;
@@ -325,7 +241,7 @@ sub finalize {
     
     
         
-
$errors
+
$error
$infos
$name
@@ -383,23 +299,15 @@ sub forward { if ( $command =~ /^\!/ ) { $namespace = _class2prefix($caller); } - if ( my $results = $c->find_action( $command, $namespace ) ) { - if ( $command =~ /^\!/ ) { - for my $result ( @{$results} ) { - my ( $class, $code ) = @{ $result->[0] }; - $c->state( $c->process( $class, $code ) ); - } - } - else { - return 0 unless my $result = $results->[0]; - if ( $result->[2] ) { + my $results = $c->get_action( $command, $namespace ); + if ( @{$results} ) { + unless ( $command =~ /^\!/ ) { + $results = [ pop @{$results} ]; + if ( $results->[0]->[2] ) { $c->log->debug(qq/Couldn't forward "$command" to regex action/) if $c->debug; return 0; } - my ( $class, $code ) = @{ $result->[0] }; - $class = $c->components->{$class} || $class; - $c->state( $c->process( $class, $code ) ); } } else { @@ -411,7 +319,7 @@ sub forward { my $method = shift || 'process'; if ( my $code = $class->can($method) ) { $c->actions->{reverse}->{"$code"} = "$class->$method"; - $c->state( $c->process( $class, $code ) ); + $results = [ [ [ $class, $code ] ] ]; } else { $c->log->debug(qq/Couldn't forward to "$class->$method"/) @@ -419,16 +327,70 @@ sub forward { return 0; } } + for my $result ( @{$results} ) { + $c->state( $c->process( @{ $result->[0] } ) ); + } return $c->state; } -=item $c->handler($r) +=item $c->get_action( $action, $namespace ) + +Get an action in a given namespace. + +=cut + +sub get_action { + my ( $c, $action, $namespace ) = @_; + $namespace ||= ''; + if ( $action =~ /^\!(.*)/ ) { + $action = $1; + my $parent = $c->tree; + my @results; + my $result = $c->actions->{private}->{ $parent->getUID }->{$action}; + push @results, [$result] if $result; + my $visitor = Tree::Simple::Visitor::FindByPath->new; + my $local; + for my $part ( split '/', $namespace ) { + $local = undef; + $visitor->setSearchPath($part); + $parent->accept($visitor); + my $child = $visitor->getResult; + my $uid = $child->getUID if $child; + my $match = $c->actions->{private}->{$uid}->{$action} if $uid; + return [ [$match] ] if ( $match && $match =~ /^?.*/ ); + $local = $c->actions->{private}->{$uid}->{"?$action"} if $uid; + push @results, [$match] if $match; + $parent = $child if $child; + } + return [ [$local] ] if $local; + return \@results; + } + elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] } + elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] } + else { + for my $regex ( keys %{ $c->actions->{compiled} } ) { + my $name = $c->actions->{compiled}->{$regex}; + if ( $action =~ $regex ) { + my @snippets; + for my $i ( 1 .. 9 ) { + no strict 'refs'; + last unless ${$i}; + push @snippets, ${$i}; + } + return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ]; + } + } + } + return []; +} + +=item $c->handler( $class, $r ) Handles the request. =cut -sub handler { +sub handler ($$) { my ( $class, $r ) = @_; # Always expect worst case! @@ -440,19 +402,20 @@ sub handler { my $namespace = ''; $namespace = join '/', @{ $c->req->args } if $action eq '!default'; unless ($namespace) { - if ( my $result = $c->find_action($action) ) { + if ( my $result = $c->get_action($action) ) { $namespace = _class2prefix( $result->[0]->[0]->[0] ); } } - my $results = $c->find_action( $action, $namespace ); + my $results = $c->get_action( $action, $namespace ); if ( @{$results} ) { - for my $begin ( @{ $c->find_action( '!begin', $namespace ) } ) { + for my $begin ( @{ $c->get_action( '!begin', $namespace ) } ) { $c->state( $c->process( @{ $begin->[0] } ) ); } - for my $result ( @{ $c->find_action( $action, $namespace ) } ) { + for my $result ( @{ $c->get_action( $action, $namespace ) } ) { $c->state( $c->process( @{ $result->[0] } ) ); + last unless $action =~ /^\!.*/; } - for my $end ( @{ $c->find_action( '!end', $namespace ) } ) { + for my $end ( @{ $c->get_action( '!end', $namespace ) } ) { $c->state( $c->process( @{ $end->[0] } ) ); } } @@ -462,7 +425,7 @@ sub handler { ? qq/Unknown resource "$path"/ : "No default action defined"; $c->log->error($error) if $c->debug; - $c->errors($error); + $c->error($error); } return $c->finalize; }; @@ -485,7 +448,8 @@ sub handler { =item $c->prepare($r) -Turns the engine-specific request (Apache, CGI...) into a Catalyst context. +Turns the engine-specific request( Apache, CGI ... ) +into a Catalyst context . =cut @@ -555,7 +519,7 @@ sub prepare_action { $c->req->args( \my @args ); while (@path) { $path = join '/', @path; - if ( my $result = ${ $c->find_action($path) }[0] ) { + if ( my $result = ${ $c->get_action($path) }[0] ) { # It's a regex if ($#$result) { @@ -586,7 +550,7 @@ sub prepare_action { if ( $c->debug && @args ); } -=item $c->prepare_connection; +=item $c->prepare_connection Prepare connection. @@ -594,7 +558,7 @@ Prepare connection. sub prepare_connection { } -=item $c->prepare_cookies; +=item $c->prepare_cookies Prepare cookies. @@ -645,35 +609,44 @@ sub prepare_uploads { } =item $c->process($class, $coderef) Process a coderef in given class and catch exceptions. -Errors are available via $c->errors. +Errors are available via $c->error. =cut sub process { my ( $c, $class, $code ) = @_; - my $status; + $class = $c->comp($class) || $class; + $c->state(0); eval { if ( $c->debug ) { my $action = $c->actions->{reverse}->{"$code"} || "$code"; - my $elapsed; - ( $elapsed, $status ) = + my ( $elapsed, @state ) = $c->benchmark( $code, $class, $c, @{ $c->req->args } ); $c->log->info( sprintf qq/Processing "$action" took %fs/, $elapsed ) if $c->debug; + $c->state(@state); } - else { $status = &$code( $class, $c, @{ $c->req->args } ) } + else { $c->state( &$code( $class, $c, @{ $c->req->args } ) ) } }; if ( my $error = $@ ) { chomp $error; $error = qq/Caught exception "$error"/; $c->log->error($error); - $c->errors($error) if $c->debug; - return 0; + $c->error($error) if $c->debug; + $c->state(0); } - return $status; + return $c->state; } +=item $c->run + +Starts the engine. + +=cut + +sub run { } + =item $c->request =item $c->req @@ -690,6 +663,54 @@ Returns a C object. my $res = $c->res; +=item $c->set_action( $action, $code, $namespace ) + +Set an action in a given namespace. + +=cut + +sub set_action { + my ( $c, $action, $code, $namespace ) = @_; + my $prefix = ''; + if ( $action =~ /^\?(.*)$/ ) { + my $prefix = $1 || ''; + $action = $2; + $action = $prefix . _prefix( $namespace, $action ); + $c->actions->{plain}->{$action} = [ $namespace, $code ]; + } + elsif ( $action =~ /^\/(.*)\/$/ ) { + my $regex = $1; + $c->actions->{compiled}->{qr#$regex#} = $action; + $c->actions->{regex}->{$action} = [ $namespace, $code ]; + } + elsif ( $action =~ /^\!(.*)$/ ) { + $action = $1; + my $parent = $c->tree; + my $visitor = Tree::Simple::Visitor::FindByPath->new; + $prefix = _class2prefix($namespace); + for my $part ( split '/', $prefix ) { + $visitor->setSearchPath($part); + $parent->accept($visitor); + my $child = $visitor->getResult; + unless ($child) { + $child = $parent->addChild( Tree::Simple->new($part) ); + $visitor->setSearchPath($part); + $parent->accept($visitor); + $child = $visitor->getResult; + } + $parent = $child; + } + my $uid = $parent->getUID; + $c->actions->{private}->{$uid}->{$action} = [ $namespace, $code ]; + $action = "!$action"; + } + else { $c->actions->{plain}->{$action} = [ $namespace, $code ] } + my $reverse = $prefix ? "$action ($prefix)" : $action; + $c->actions->{reverse}->{"$code"} = $reverse; + $c->log->debug(qq/"$namespace" defined "$action" as "$code"/) + if $c->debug; +} + =item $class->setup Setup. @@ -766,6 +787,7 @@ sub stash { sub _prefix { my ( $class, $name ) = @_; my $prefix = _class2prefix($class); + warn "$class - $name - $prefix"; $name = "$prefix/$name" if $prefix; return $name; }