| 1 | # | ||
| 2 | # parse and transform PHP source files | ||
| 3 | # | ||
| 4 | package PHP::Decode; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use PHP::Decode::Parser; | ||
| 9 | use PHP::Decode::Transformer; | ||
| 10 | |||
| 11 | our $VERSION = '0.302'; | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | my ($class, %args) = @_; | ||
| 15 | |||
| 16 | my $parser = PHP::Decode::Parser->new(strmap => {}, | ||
| 17 | exists $args{inscript} ? (inscript => $args{inscript}) : (), | ||
| 18 | exists $args{filename} ? (filename => $args{filename}) : (), | ||
| 19 | exists $args{max_strlen} ? (max_strlen => $args{max_strlen}) : (), | ||
| 20 | exists $args{log} ? (log => $args{log}) : (), | ||
| 21 | exists $args{debug} ? (debug => $args{debug}) : (), | ||
| 22 | exists $args{warn} ? (warn => $args{warn}) : ()); | ||
| 23 | |||
| 24 | my $ctx = PHP::Decode::Transformer->new(parser => $parser, | ||
| 25 | exists $args{skip} ? (skip => $args{skip}) : (), | ||
| 26 | exists $args{with} ? (with => $args{with}) : (), | ||
| 27 | exists $args{max_loop} ? (max_loop => $args{max_loop}) : (), | ||
| 28 | exists $args{log} ? (log => \&_log_cb) : (), | ||
| 29 | exists $args{warn} ? (warn => \&_warn_cb) : ()); | ||
| 30 | |||
| 31 | return bless { | ||
| 32 | ctx => $ctx, | ||
| 33 | }, $class; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _log_cb { | ||
| 37 | my ($ctx, $action, $stmt, $fmt) = (shift, shift, shift, shift); | ||
| 38 | |||
| 39 | my $parser = $ctx->{parser}; | ||
| 40 | my $flg = $ctx->{skipundef} ? '! ' : ''; | ||
| 41 | if ($ctx->{incall}) { | ||
| 42 | $parser->{log}->($flg . "[$ctx->{infunction}] $action $stmt", $fmt, @_); | ||
| 43 | } elsif ($ctx->{infunction}) { | ||
| 44 | $parser->{log}->("<$ctx->{infunction}> $action $stmt", $fmt, @_); | ||
| 45 | } else { | ||
| 46 | $parser->{log}->($flg . "$action $stmt", $fmt, @_); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _warn_cb { | ||
| 51 | my ($ctx, $action, $stmt, $fmt) = (shift, shift, shift, shift); | ||
| 52 | |||
| 53 | my $parser = $ctx->{parser}; | ||
| 54 | my $flg = $ctx->{skipundef} ? '! ' : ''; | ||
| 55 | if ($ctx->{incall}) { | ||
| 56 | $parser->{warn}->($flg . "[$ctx->{infunction}] $action $stmt", $fmt, @_); | ||
| 57 | } elsif ($ctx->{infunction}) { | ||
| 58 | $parser->{warn}->("<$ctx->{infunction}> $action $stmt", $fmt, @_); | ||
| 59 | } else { | ||
| 60 | $parser->{warn}->($flg . "$action $stmt", $fmt, @_); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | sub parse { | ||
| 65 | my ($self, $line) = @_; | ||
| 66 | my $parser = $self->{ctx}{parser}; | ||
| 67 | my $str = $parser->setstr($line); | ||
| 68 | |||
| 69 | return $self->{ctx}->parse_eval($str); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub exec { | ||
| 73 | my ($self, $stmt) = @_; | ||
| 74 | |||
| 75 | return $self->{ctx}->exec_eval($stmt); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub eval { | ||
| 79 | my ($self, $line) = @_; | ||
| 80 | my $parser = $self->{ctx}{parser}; | ||
| 81 | my $str = $parser->setstr($line); | ||
| 82 | |||
| 83 | my $stmt = $self->{ctx}->parse_eval($str); | ||
| 84 | return $self->{ctx}->exec_eval($stmt); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub format { | ||
| 88 | my ($self, $stmt, $fmt) = @_; | ||
| 89 | my $parser = $self->{ctx}{parser}; | ||
| 90 | |||
| 91 | return $parser->format_stmt($stmt, $fmt); | ||
| 92 | } | ||
| 93 | |||
| 94 | 1; | ||
| 95 | |||
| 96 | __END__ |