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