1 |
package Frey::PPI; |
2 |
use Moose; |
3 |
|
4 |
extends 'Frey::ClassLoader'; |
5 |
|
6 |
use PPI; |
7 |
use Data::Dump qw/dump/; |
8 |
|
9 |
has 'class' => ( |
10 |
is => 'rw', |
11 |
isa => 'Str', |
12 |
required => 1, |
13 |
documentation => 'Name of class to parse', |
14 |
); |
15 |
|
16 |
has 'doc' => ( |
17 |
is => 'ro', |
18 |
isa => 'PPI::Document', |
19 |
lazy => 1, |
20 |
default => sub { |
21 |
my ( $self ) = @_; |
22 |
warn "doc from ", $self->class; |
23 |
my $doc = PPI::Document->new( $self->class_path( $self->class ) ) || die $!; |
24 |
$doc->prune('PPI::Token::Whitespace'); |
25 |
return $doc; |
26 |
}, |
27 |
); |
28 |
|
29 |
sub find { |
30 |
my ( $self, $coderef ) = @_; |
31 |
|
32 |
my $doc = $self->doc; |
33 |
$doc->find(sub { |
34 |
my ( $doc,$el ) = @_; |
35 |
eval { $coderef->( $doc, $el ) }; |
36 |
warn "ERROR: $@" if $@; |
37 |
}); |
38 |
} |
39 |
|
40 |
sub attribute_order { |
41 |
my ( $self ) = @_; |
42 |
|
43 |
my $attribute_order; |
44 |
$self->find(sub { |
45 |
my ($doc,$el) = @_; |
46 |
return unless ( $el->isa('PPI::Statement') && $el->{children}->[0]->isa('PPI::Token::Word') && $el->{children}->[0]->literal eq 'has' ); |
47 |
|
48 |
warn "## has ",$el->{children}->[1]->literal if $self->debug; |
49 |
push @$attribute_order, $el->{children}->[1]->literal; |
50 |
}); |
51 |
|
52 |
warn "# ", $self->class, " attribute_order ", dump( $attribute_order ) if $self->debug; |
53 |
|
54 |
return $attribute_order; |
55 |
} |
56 |
|
57 |
sub includes { |
58 |
my $self = shift; |
59 |
|
60 |
my $include; |
61 |
$self->find(sub { |
62 |
my ($doc,$el) = @_; |
63 |
return unless $el->isa('PPI::Statement::Include'); |
64 |
|
65 |
warn "## include ",dump( $el->module, $el->type, $el->pragma ) if $self->debug; |
66 |
push @{ $include->{ $el->type } }, $el->module |
67 |
}); |
68 |
warn "# ", $self->class, " include ", dump( $include ) if $self->debug; |
69 |
return $include; |
70 |
} |
71 |
|
72 |
sub data { |
73 |
my $self = shift; |
74 |
return { |
75 |
includes => $self->includes, |
76 |
attribute_order => $self->attribute_order, |
77 |
doc => $self->doc, |
78 |
}; |
79 |
} |
80 |
|
81 |
1; |