From: Sebastian Riedel Date: Thu, 14 Apr 2005 13:44:09 +0000 (+0000) Subject: some code cleanup X-Git-Tag: 5.7099_04~1538 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=99fe1710ff4af72d62979c1b64092c0777660a4d;p=catagits%2FCatalyst-Runtime.git some code cleanup --- diff --git a/lib/Catalyst.pm b/lib/Catalyst.pm index 08037d2..2f4aa1f 100644 --- a/lib/Catalyst.pm +++ b/lib/Catalyst.pm @@ -126,6 +126,7 @@ sub import { my ( $self, @options ) = @_; my $caller = caller(0); + # Prepare inheritance unless ( $caller->isa($self) ) { no strict 'refs'; push @{"$caller\::ISA"}, $self; @@ -139,6 +140,7 @@ sub import { $caller->log( Catalyst::Log->new ); } + # Debug? if ( $ENV{CATALYST_DEBUG} || $ENV{ uc($caller) . '_DEBUG' } ) { no strict 'refs'; *{"$caller\::debug"} = sub { 1 }; @@ -148,6 +150,7 @@ sub import { my $engine = 'Catalyst::Engine::CGI'; my $dispatcher = 'Catalyst::Dispatcher'; + # Detect mod_perl if ( $ENV{MOD_PERL} ) { require mod_perl; @@ -160,19 +163,24 @@ sub import { } } + # Process options my @plugins; foreach (@options) { + if (/^\-Debug$/) { next if $caller->debug; no strict 'refs'; *{"$caller\::debug"} = sub { 1 }; $caller->log->debug('Debug messages enabled'); } + elsif (/^-Dispatcher=(.*)$/) { $dispatcher = "Catalyst::Dispatcher::$1"; } + elsif (/^-Engine=(.*)$/) { $engine = "Catalyst::Engine::$1" } elsif (/^-.*$/) { $caller->log->error(qq/Unknown flag "$_"/) } + else { my $plugin = "Catalyst::Plugin::$_"; @@ -187,7 +195,10 @@ sub import { push @{"$caller\::ISA"}, $plugin; } } + } + + # Plugin table my $t = Text::ASCIITable->new( { hide_HeadRow => 1, hide_HeadLine => 1 } ); $t->setCols('Class'); $t->setColWidth( 'Class', 75, 1 ); diff --git a/lib/Catalyst/Dispatcher.pm b/lib/Catalyst/Dispatcher.pm index 76ab83c..01f4bed 100644 --- a/lib/Catalyst/Dispatcher.pm +++ b/lib/Catalyst/Dispatcher.pm @@ -9,9 +9,12 @@ use Tree::Simple::Visitor::FindByPath; __PACKAGE__->mk_classdata($_) for qw/actions tree/; +# These are the core structures __PACKAGE__->actions( { plain => {}, private => {}, regex => {}, compiled => [], reverse => {} } ); + +# We use a tree __PACKAGE__->tree( Tree::Simple->new( 0, Tree::Simple->ROOT ) ); memoize('_class2prefix'); @@ -42,14 +45,17 @@ sub dispatch { my $namespace = ''; $namespace = ( join( '/', @{ $c->req->args } ) || '/' ) if $action eq 'default'; + unless ($namespace) { if ( my $result = $c->get_action($action) ) { $namespace = _class2prefix( $result->[0]->[0]->[0] ); } } + my $default = $action eq 'default' ? $namespace : undef; my $results = $c->get_action( $action, $default ); $namespace ||= '/'; + if ( @{$results} ) { # Execute last begin @@ -82,6 +88,7 @@ sub dispatch { return if scalar @{ $c->error }; } } + else { my $path = $c->req->path; my $error = $path @@ -107,33 +114,44 @@ If you define a class without method it will default to process(). sub forward { my $c = shift; my $command = shift; + unless ($command) { $c->log->debug('Nothing to forward to') if $c->debug; return 0; } + my $caller = caller(0); my $namespace = '/'; + if ( $command =~ /^\// ) { $command =~ /^\/(.*)\/(\w+)$/; $namespace = $1 || '/'; $command = $2 || $command; $command =~ s/^\///; + warn "NAMESPACE: $namespace COMMAND: $command"; } + else { $namespace = _class2prefix($caller) || '/' } + my $results = $c->get_action( $command, $namespace ); + unless ( @{$results} ) { my $class = $command || ''; + if ( $class =~ /[^\w\:]/ ) { my $error = qq/Couldn't forward to "$class"/; $c->error($error); $c->log->debug($error) if $c->debug; return 0; } + my $method = shift || 'process'; + if ( my $code = $class->can($method) ) { $c->actions->{reverse}->{"$code"} = "$class->$method"; $results = [ [ [ $class, $code ] ] ]; } + else { my $error = qq/Couldn't forward to "$class"/; $c->error($error); @@ -141,12 +159,15 @@ sub forward { if $c->debug; return 0; } + } + for my $result ( @{$results} ) { $c->execute( @{ $result->[0] } ); return if scalar @{ $c->error }; last unless $c->state; } + return $c->state; } @@ -160,15 +181,18 @@ sub get_action { my ( $c, $action, $namespace ) = @_; return [] unless $action; $namespace ||= ''; + if ($namespace) { $namespace = '' if $namespace eq '/'; my $parent = $c->tree; my @results; my %allowed = ( begin => 1, auto => 1, default => 1, end => 1 ); + if ( $allowed{$action} ) { 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); @@ -178,8 +202,11 @@ sub get_action { push @results, [$match] if $match; $parent = $child if $child; } + } + else { + if ($namespace) { my $visitor = Tree::Simple::Visitor::FindByPath->new; $visitor->setSearchPath( split '/', $namespace ); @@ -190,20 +217,26 @@ sub get_action { if $uid; push @results, [$match] if $match; } + else { my $result = $c->actions->{private}->{ $parent->getUID }->{$action}; push @results, [$result] if $result; } + } return \@results; } + elsif ( my $p = $c->actions->{plain}->{$action} ) { return [ [$p] ] } elsif ( my $r = $c->actions->{regex}->{$action} ) { return [ [$r] ] } + else { + for my $i ( 0 .. $#{ $c->actions->{compiled} } ) { my $name = $c->actions->{compiled}->[$i]->[0]; my $regex = $c->actions->{compiled}->[$i]->[1]; + if ( $action =~ $regex ) { my @snippets; for my $i ( 1 .. 9 ) { @@ -213,6 +246,7 @@ sub get_action { } return [ [ $c->actions->{regex}->{$name}, $name, \@snippets ] ]; } + } } return []; @@ -249,18 +283,22 @@ sub set_action { my $parent = $c->tree; my $visitor = Tree::Simple::Visitor::FindByPath->new; + 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}->{$method} = [ $namespace, $code ]; my $forward = $prefix ? "$prefix/$method" : $method; @@ -271,6 +309,7 @@ sub set_action { if ( $flags{path} =~ /^'(.*)'$/ ) { $flags{path} = $1 } if ( $flags{path} =~ /^"(.*)"$/ ) { $flags{path} = $1 } } + if ( $flags{regex} ) { $flags{regex} =~ s/^\w+//; $flags{regex} =~ s/\w+$//; @@ -281,16 +320,19 @@ sub set_action { my $reverse = $prefix ? "$prefix/$method" : $method; if ( $flags{local} || $flags{global} || $flags{path} ) { - my $path = $flags{path} || $method; + my $path = $flags{path} || $method; my $absolute = 0; + if ( $path =~ /^\/(.+)/ ) { $path = $1; $absolute = 1; } + $absolute = 1 if $flags{global}; my $name = $absolute ? $path : $prefix ? "$prefix/$path" : $path; $c->actions->{plain}->{$name} = [ $namespace, $code ]; } + if ( my $regex = $flags{regex} ) { push @{ $c->actions->{compiled} }, [ $regex, qr#$regex# ]; $c->actions->{regex}->{$regex} = [ $namespace, $code ]; @@ -307,14 +349,17 @@ Setup actions for a component. sub setup_actions { my ( $self, $comps ) = @_; + for my $comp (@$comps) { $comp = ref $comp || $comp; + for my $action ( @{ $comp->_cache } ) { my ( $code, $attrs ) = @{$action}; my $name = ''; no strict 'refs'; my @cache = ( $comp, @{"$comp\::ISA"} ); my %namespaces; + while ( my $namespace = shift @cache ) { $namespaces{$namespace}++; for my $isa ( @{"$comp\::ISA"} ) { @@ -323,58 +368,77 @@ sub setup_actions { $namespaces{$isa}++; } } + for my $namespace ( keys %namespaces ) { + for my $sym ( values %{ $namespace . '::' } ) { + if ( *{$sym}{CODE} && *{$sym}{CODE} == $code ) { + $name = *{$sym}{NAME}; $self->set_action( $name, $code, $comp, $attrs ); last; } + } + } + } + } + my $actions = $self->actions; my $privates = Text::ASCIITable->new; $privates->setCols( 'Private', 'Class' ); $privates->setColWidth( 'Private', 36, 1 ); $privates->setColWidth( 'Class', 37, 1 ); + my $walker = sub { my ( $walker, $parent, $prefix ) = @_; $prefix .= $parent->getNodeValue || ''; $prefix .= '/' unless $prefix =~ /\/$/; my $uid = $parent->getUID; + for my $action ( keys %{ $actions->{private}->{$uid} } ) { my ( $class, $code ) = @{ $actions->{private}->{$uid}->{$action} }; $privates->addRow( "$prefix$action", $class ); } + $walker->( $walker, $_, $prefix ) for $parent->getAllChildren; }; + $walker->( $walker, $self->tree, '' ); $self->log->debug( 'Loaded private actions', $privates->draw ) if ( @{ $privates->{tbl_rows} } && $self->debug ); + my $publics = Text::ASCIITable->new; $publics->setCols( 'Public', 'Private' ); $publics->setColWidth( 'Public', 36, 1 ); $publics->setColWidth( 'Private', 37, 1 ); + for my $plain ( sort keys %{ $actions->{plain} } ) { my ( $class, $code ) = @{ $actions->{plain}->{$plain} }; my $reverse = $self->actions->{reverse}->{$code}; $reverse = $reverse ? "/$reverse" : $code; $publics->addRow( "/$plain", $reverse ); } + $self->log->debug( 'Loaded public actions', $publics->draw ) if ( @{ $publics->{tbl_rows} } && $self->debug ); + my $regexes = Text::ASCIITable->new; $regexes->setCols( 'Regex', 'Private' ); $regexes->setColWidth( 'Regex', 36, 1 ); $regexes->setColWidth( 'Private', 37, 1 ); + for my $regex ( sort keys %{ $actions->{regex} } ) { my ( $class, $code ) = @{ $actions->{regex}->{$regex} }; my $reverse = $self->actions->{reverse}->{$code}; $reverse = $reverse ? "/$reverse" : $code; $regexes->addRow( $regex, $reverse ); } + $self->log->debug( 'Loaded regex actions', $regexes->draw ) if ( @{ $regexes->{tbl_rows} } && $self->debug ); } diff --git a/lib/Catalyst/Engine.pm b/lib/Catalyst/Engine.pm index 641cc6d..a7f2e7b 100644 --- a/lib/Catalyst/Engine.pm +++ b/lib/Catalyst/Engine.pm @@ -15,6 +15,7 @@ use Catalyst::Response; require Module::Pluggable::Fast; +# For pretty dumps $Data::Dumper::Terse = 1; __PACKAGE__->mk_classdata('components'); @@ -24,6 +25,7 @@ __PACKAGE__->mk_accessors(qw/request response state/); *req = \&request; *res = \&response; +# For statistics our $COUNT = 1; our $START = time; @@ -75,14 +77,17 @@ Regex search for a component. sub component { my ( $c, $name ) = @_; + if ( my $component = $c->components->{$name} ) { return $component; } + else { for my $component ( keys %{ $c->components } ) { return $c->components->{$component} if $component =~ /$name/i; } } + } =item $c->error @@ -120,6 +125,7 @@ sub execute { $class = $c->comp($class) || $class; $c->state(0); my $callsub = ( caller(1) )[3]; + eval { if ( $c->debug ) { @@ -133,6 +139,7 @@ sub execute { } else { $c->state( &$code( $class, $c, @{ $c->req->args } ) ) } }; + if ( my $error = $@ ) { unless ( ref $error ) { @@ -337,12 +344,14 @@ sub handler { my $status = -1; eval { my @stats = (); + my $handler = sub { my $c = $class->prepare($engine); $c->{stats} = \@stats; $c->dispatch; return $c->finalize; }; + if ( $class->debug ) { my $elapsed; ( $elapsed, $status ) = $class->benchmark($handler); @@ -358,11 +367,14 @@ sub handler { $t->draw ); } else { $status = &$handler } + }; + if ( my $error = $@ ) { chomp $error; $class->log->error(qq/Caught exception in engine "$error"/); } + $COUNT++; return $status; } @@ -376,6 +388,7 @@ into a Catalyst context . sub prepare { my ( $class, $r ) = @_; + my $c = bless { request => Catalyst::Request->new( { @@ -393,6 +406,7 @@ sub prepare { stash => {}, state => 0 }, $class; + if ( $c->debug ) { my $secs = time - $START || 1; my $av = sprintf '%.3f', $COUNT / $secs; @@ -401,17 +415,20 @@ sub prepare { $c->log->debug('**********************************'); $c->res->headers->header( 'X-Catalyst' => $Catalyst::VERSION ); } + $c->prepare_request($r); $c->prepare_path; $c->prepare_headers; $c->prepare_cookies; $c->prepare_connection; + my $method = $c->req->method || ''; my $path = $c->req->path || ''; my $hostname = $c->req->hostname || ''; my $address = $c->req->address || ''; $c->log->debug(qq/"$method" request for "$path" from $hostname($address)/) if $c->debug; + $c->prepare_action; $c->prepare_parameters; @@ -426,6 +443,7 @@ sub prepare { } $c->log->debug( 'Parameters are', $t->draw ); } + $c->prepare_uploads; return $c; } @@ -441,6 +459,7 @@ sub prepare_action { my $path = $c->req->path; my @path = split /\//, $c->req->path; $c->req->args( \my @args ); + while (@path) { $path = join '/', @path; if ( my $result = ${ $c->get_action($path) }[0] ) { @@ -458,19 +477,23 @@ sub prepare_action { $c->req->action($match); $c->req->snippets( \@snippets ); } + else { $c->req->action($path); $c->log->debug(qq/Requested action is "$path"/) if $c->debug; } + $c->req->match($path); last; } unshift @args, pop @path; } + unless ( $c->req->action ) { $c->req->action('default'); $c->req->match(''); } + $c->log->debug( 'Arguments are "' . join( '/', @args ) . '"' ) if ( $c->debug && @args ); } @@ -604,18 +627,21 @@ sub setup_components { $self->log->error( qq/Couldn't initialize "Module::Pluggable::Fast", "$error"/); } + $self->components( {} ); my @comps; for my $comp ( $self->_components($self) ) { $self->components->{ ref $comp } = $comp; push @comps, $comp; } + my $t = Text::ASCIITable->new( { hide_HeadRow => 1, hide_HeadLine => 1 } ); $t->setCols('Class'); $t->setColWidth( 'Class', 75, 1 ); $t->addRow($_) for keys %{ $self->components }; $self->log->debug( 'Loaded components', $t->draw ) if ( @{ $t->{tbl_rows} } && $self->debug ); + $self->setup_actions( [ $self, @comps ] ); }