summary refs log tree commit diff
path: root/lib/CatalystX/Dispatcher/AsGraph.pm
blob: 9f6bfd525f68f79771197eae06bfd15f8d4d6600 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use MooseX::Declare;
use Graph::Easy;
use UNIVERSAL::require;

our $VERSION = '0.02';

class CatalystX::Dispatcher::AsGraph {

    with 'MooseX::Getopt';

    has [qw/appname output/] => ( is => 'ro', isa => 'Str', required => 1 );
    has 'graph' => ( is => 'ro', default => sub { Graph::Easy->new } );

    method run{
        my $class  = $self->appname;
        $class->require or die $@;
        my $app    = $class->new;
        my $routes = $app->dispatcher->_tree;
        $self->_new_node($routes, '');
    }

    method _new_node($parent, $prefix) {
        my $name = $prefix . $parent->getNodeValue || '';
        my $node = $self->graph->add_node($name);

        my $actions = $parent->getNodeValue->actions;
        for my $action ( keys %{$actions} ) {
            next if ( ( $action =~ /^_.*/ ) );
            $self->graph->add_edge( $node, "[action] " . $action);
        }
        for my $child ( $parent->getAllChildren ) {
            my $child_node = $self->_new_node( $child, $name . ' -> ' );
            $self->graph->add_edge( $node, $child_node );
        }
        return $node;
    }
}


__END__

=head1 NAME

CatalystX::Dispatcher::AsGraph - Create a graph from Catalyst dispatcher

=head1 SYNOPSIS

    use CatalystX::Dispatcher::AsGraph;
    my $graph = CatalystX::Dispatcher::AsGraph->new_with_options();
    $graph->graph;

=head1 DESCRIPTION

CatalystX::Dispatcher::AsGraph create a graph for a Catalyst application
using his dispatcher.

At the time, only private actions are graphed.

=head1 AUTHOR

Franck Cuny E<lt>franck@lumberjaph.netE<gt>

=head1 SEE ALSO

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.