Catalyst-View-Template-PHP

 view release on metacpan or  search on metacpan

lib/Catalyst/View/Template/PHP.pm  view on Meta::CPAN


    if (ref($c->req->body) eq 'File::Temp') {
	my $input = join q//, readline($c->req->body);
	if (length($input)) {
	    my $len = length($input);
	    PHP::set_php_input($input);
	    $params->{HTTP_RAW_POST_DATA} = $input;
	    if ($DEBUG) {
		if ($len < 500) {
		    $c->log->debug( "Set \$HTTP_RAW_POST_DATA:", $input );
		} else {
		    $c->log->debug( "Set \$HTTP_RAW_POST_DATA, $len bytes" );
		}
	    }
	}
    }

    $params = $self->preprocess( $c, $params );



    while (my ($param_key, $param_value) = each %$params) {
	PHP::assign_global( $param_key, $param_value );
    }
    if ($DEBUG) {
	use Data::Dumper;
	local $Data::Dumper::Indent = 1;
	local $Data::Dumper::Sortkeys = 1;
	$c->log->debug("Parameters to make into global PHP variables:",
		       Data::Dumper::Dumper($params));
    }
    $c->stash->{php_params} = $params;




    # include path
    my @include_path;
    if ($c->stash->{php_include_path}) {
	@include_path = ($c->stash->{include_path});
    } elsif ($c->config->{'Template::PHP'}{include_path}) {
	@include_path = split /\s+/, 
			    $c->config->{'Template::PHP'}{include_path};
    }
    if (!@include_path) {
	@include_path = ($c->config->{root},
			 $c->config->{root} . '/templates',
			 $c->config->{root} . '/php',
			 # . included by default sometime after v5.1.6?
			 '.');
    }
    PHP::call( 'set_include_path', join (':', @include_path) );
    if ($DEBUG) {
	$c->log->debug("include path for PHP: "
		       . join ' : ', @include_path );
    }




    # set up PHP module callbacks
    my $OUTPUT = '';
    PHP::options( stdout => sub { $OUTPUT .= $_[0] } );
    PHP::options( stderr => sub { $self->handle_warning($c, $_[0]) } );
    PHP::options( header => sub { $self->header_callback($c, $_[0], $_[1]) } );
    $self->{_headers} = [];
    if ($DEBUG) {
	$c->log->debug(scalar localtime . " ... processing '$template' ...");
    }




    # invoke PHP
    eval { PHP::include( $template ) };
    chdir $orig_cwd;
    $self->{_status} = 200;
    if ($@) {
	$self->handle_error( $c, $template, $@ );
    }




    $OUTPUT = $self->postprocess( $c, $OUTPUT );
    if ($DEBUG) {
	$c->log->debug("Output from PHP processing of '$template':",
		       $OUTPUT);
    }



    # handle redirect?
    my @redirect = grep { /^Location:\s/ } map { $_->[0] } @{$self->{_headers}};
    if (@redirect) {
	$c->log->debug("Redirect header(s) received: @redirect") if $DEBUG;
	if ($self->{_status} < 300 || $self->{_status} >= 400) {
	    $self->{_status} = 302;
	}
	my $location = $redirect[0];
	$location =~ s/^\S+:\s+//;
	if ($self->on_redirect($c, $location, $self->{_status})) {

	    foreach my $headerref ( @{$self->{_headers}} ) {
		my ($header, $replace) = @$headerref;
		next if $header =~ /^Location:\s/;
		if ($replace) {
		    $c->response->headers->header(split /:\s+/, $header, 2);
		} else {
		    $c->response->headers->push_header(split(/:\s+/, $header, 2));
		}
	    }


	    if ($DEBUG) {
		$c->log->debug("View::Template::PHP: redirecting to $location");
		use Data::Dumper;
		local $Data::Dumper::Indent = 1;
		local $Data::Dumper::Sortkeys = 1;
		$c->log->debug("RESPONSE HEADERS: " .
			       Data::Dumper::Dumper($c->response->headers));



( run in 0.608 second using v1.01-cache-2.11-cpan-39bf76dae61 )