| 1 | package Catalyst::View::PHP; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base qw/Catalyst::Base/; | ||
| 5 | use PHP::Interpreter; | ||
| 6 | use NEXT; | ||
| 7 | |||
| 8 | our $VERSION = '0.01'; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Catalyst::View::PHP - Template View Class | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | # use the helper | ||
| 17 | myapp_create.pl view PHP PHP | ||
| 18 | |||
| 19 | # lib/MyApp/View/PHP.pm | ||
| 20 | package MyApp::View::PHP; | ||
| 21 | |||
| 22 | use base 'Catalyst::View::PHP'; | ||
| 23 | |||
| 24 | # To set the override the PHP include path set the path in the config. | ||
| 25 | __PACKAGE__->config->{INCLUDE_PATH} = | ||
| 26 | '/usr/local/generic/templates:/usr/local/myapp/templates'; | ||
| 27 | |||
| 28 | 1; | ||
| 29 | |||
| 30 | # Meanwhile, maybe in a private C<end> action | ||
| 31 | $c->forward('MyApp::View::PHP'); | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | This is the Catalyst view class for the L<PHP::Interpreter>. Your | ||
| 36 | application subclass should inherit from this class. This plugin | ||
| 37 | renders the template specified in C<$c-E<gt>stash-E<gt>{template}>, or | ||
| 38 | failing that, C<$c-E<gt>request-E<gt>match>. The template variables | ||
| 39 | are set up from the contents of C<$c-E<gt>stash>, augmented with | ||
| 40 | template variable C<base> set to Catalyst's C<$c-E<gt>req-E<gt>base>, | ||
| 41 | template variable C<c> to Catalyst's C<$c>, and template variable | ||
| 42 | C<name> to Catalyst's C<$c-E<gt>config-E<gt>{name}>. The output is | ||
| 43 | stored in C<$c-E<gt>response-E<gt>output>. | ||
| 44 | |||
| 45 | If you want to override PHP config settings, you can do it in your | ||
| 46 | application's view class by setting | ||
| 47 | C<__PACKAGE__-E<gt>config-E<gt>{OPTION}>, as shown in the Synopsis. | ||
| 48 | See the available options document on the L<PHP::Interpreter> | ||
| 49 | documentation. | ||
| 50 | |||
| 51 | In PHP the variables exported are the requests parameters for C<$_GET> | ||
| 52 | and C<$_POST> depending on the method used to send the request. Also all | ||
| 53 | of the stash is exported just like in TemplateToolkit, and you can | ||
| 54 | access the current context by calling C<$c>. | ||
| 55 | |||
| 56 | For example to read the CGI parameter 'test' you can use: | ||
| 57 | |||
| 58 | C<$_GET['test']> | ||
| 59 | |||
| 60 | or | ||
| 61 | |||
| 62 | C<print $c-E<gt>request-E<gt>parameters['test']> | ||
| 63 | |||
| 64 | Or to get the method of the request try: | ||
| 65 | |||
| 66 | C<print $c-E<gt>request-E<gt>method> | ||
| 67 | |||
| 68 | =head1 BUGS | ||
| 69 | |||
| 70 | There are probably a few as this module is very new along with | ||
| 71 | PHP::Interpreter being very new. Feel free to discuss this module on | ||
| 72 | the Catalyst mailing list catalyst@lists.rawmode.org. | ||
| 73 | |||
| 74 | This module has been tested with PHP 5.1.0RC1 (cli). | ||
| 75 | |||
| 76 | =head1 METHODS | ||
| 77 | |||
| 78 | =over 4 | ||
| 79 | |||
| 80 | =item new | ||
| 81 | |||
| 82 | The constructor for the PHP view. Sets up the template provider, | ||
| 83 | and reads the application config. | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | sub new { | ||
| 88 | my $self = shift; | ||
| 89 | my $c = shift; | ||
| 90 | $self = $self->NEXT::new(@_); | ||
| 91 | my $root = $c->config->{root}; | ||
| 92 | |||
| 93 | return $self; | ||
| 94 | } | ||
| 95 | |||
| 96 | =item process | ||
| 97 | |||
| 98 | Renders the template specified in C<$c-E<gt>stash-E<gt>{template}> or | ||
| 99 | C<$c-E<gt>request-E<gt>match>. Template variables are set up from the | ||
| 100 | contents of C<$c-E<gt>stash>, augmented with C<base> set to | ||
| 101 | C<$c-E<gt>req-E<gt>base>, C<c> to C<$c> and C<name> to | ||
| 102 | C<$c-E<gt>config-E<gt>{name}>. Output is stored in | ||
| 103 | C<$c-E<gt>response-E<gt>output>. | ||
| 104 | |||
| 105 | Being that there is no clear way to reset the interpreter between | ||
| 106 | requests, each request is processed in a new interpreter instance. As | ||
| 107 | L<PHP::Interpreter> matures this may change. | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub process { | ||
| 112 | my ( $self, $c ) = @_; | ||
| 113 | |||
| 114 | my $template = $c->stash->{template} || $c->request->match; | ||
| 115 | |||
| 116 | unless ($template) { | ||
| 117 | $c->log->debug('No template specified for rendering') if $c->debug; | ||
| 118 | return 0; | ||
| 119 | } | ||
| 120 | |||
| 121 | $c->log->debug(qq/Rendering template "$template"/) if $c->debug; | ||
| 122 | |||
| 123 | my $output; | ||
| 124 | |||
| 125 | my $interpreter_params = { | ||
| 126 | c => $c, | ||
| 127 | %{$c->stash}, | ||
| 128 | OUTPUT => \$output, | ||
| 129 | COOKIE => $c->req->cookies, | ||
| 130 | INCLUDE_PATH => $c->config->{root} . ":" . $c->config->{root} . '/base', | ||
| 131 | }; | ||
| 132 | |||
| 133 | |||
| 134 | if ($c->req->method eq 'POST') { | ||
| 135 | $interpreter_params->{POST} = $c->req->parameters; | ||
| 136 | } else { | ||
| 137 | $interpreter_params->{GET} = $c->req->parameters; | ||
| 138 | } | ||
| 139 | |||
| 140 | my $interpreter = PHP::Interpreter->new($interpreter_params); | ||
| 141 | |||
| 142 | eval { | ||
| 143 | $interpreter->include($template); | ||
| 144 | }; | ||
| 145 | if($@) { | ||
| 146 | my $error = qq/Couldn't render template "$@"/; | ||
| 147 | $c->log->error($error); | ||
| 148 | $c->error($error); | ||
| 149 | return 0; | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | unless ( $c->response->content_type ) { | ||
| 154 | $c->response->content_type('text/html; charset=utf-8'); | ||
| 155 | } | ||
| 156 | |||
| 157 | $c->response->body($output); | ||
| 158 | |||
| 159 | return 1; | ||
| 160 | } | ||
| 161 | |||
| 162 | =item config | ||
| 163 | |||
| 164 | This allows your view subclass to pass additional settings to the PHP | ||
| 165 | config hash. | ||
| 166 | |||
| 167 | =back | ||
| 168 | |||
| 169 | =head1 SEE ALSO | ||
| 170 | |||
| 171 | L<Catalyst>, L<PHP::Interpreter>, L<http://www.php.net> | ||
| 172 | |||
| 173 | =head1 AUTHOR | ||
| 174 | |||
| 175 | Rusty Conover, C<rconover@infogears.com> | ||
| 176 | |||
| 177 | Based off of L<Catalyst::View::TT> by: | ||
| 178 | |||
| 179 | Sebastian Riedel, C<sri@cpan.org> | ||
| 180 | Marcus Ramberg, C<mramberg@cpan.org> | ||
| 181 | Jesse Sheidlower, C<jester@panix.com> | ||
| 182 | |||
| 183 | =head1 COPYRIGHT | ||
| 184 | |||
| 185 | Copyright (c) 2005 InfoGears, Inc. All Rights Reserved. L<http://www.infogears.com/> | ||
| 186 | |||
| 187 | This program is free software, you can redistribute it and/or modify it | ||
| 188 | under the same terms as Perl itself. | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | 1; |