1package Bread::Board::Dumper;
2our $AUTHORITY = 'cpan:STEVAN';
3# ABSTRACT: Pretty printer for visualizing the layout of your Bread::Board
4$Bread::Board::Dumper::VERSION = '0.37';
5use Moose;
6
7sub dump {
8    my ($self, $thing, $indent) = @_;
9
10    $indent = defined $indent ? $indent . '  ' : '';
11
12    my $output = '';
13
14    if ($thing->isa('Bread::Board::Dependency')) {
15        $output .= join('', $indent, "depends_on: ", $thing->service_path || $thing->service->name, "\n");
16    }
17    elsif ($thing->does('Bread::Board::Service')) {
18        $output .= join('', $indent, "service: ", $thing->name, "\n" );
19
20        if ($thing->does('Bread::Board::Service::WithDependencies')) {
21            my $deps = $thing->dependencies;
22            for my $key (sort keys %{$deps}) {
23                $output .= $self->dump($deps->{$key}, $indent);
24            }
25        }
26    }
27    elsif ($thing->isa('Bread::Board::Container')) {
28        $output = join('', $indent, "container: ", $thing->name, "\n" );
29
30        $output .= $self->_dump_container($thing, $indent);
31    }
32    elsif ($thing->isa('Bread::Board::Container::Parameterized')) {
33        my $params = join ', ', @{ $thing->allowed_parameter_names };
34        $output = join('', $indent, "container: ", $thing->name, " [$params]\n" );
35        $output .= $self->_dump_container($thing, $indent);
36    }
37
38    return $output;
39}
40
41sub _dump_container {
42    my ($self, $c, $indent) = @_;
43
44    my $output = '';
45
46    my $subs = $c->sub_containers;
47    for my $key (sort keys %{$subs}) {
48        $output .= $self->dump($subs->{$key}, $indent);
49    }
50
51    my $services = $c->services;
52    for my $key (sort keys %{$services}) {
53        $output .= $self->dump($services->{$key}, $indent);
54    }
55
56    return $output;
57}
58
59__PACKAGE__->meta->make_immutable;
60
61no Moose; 1;
62
63__END__
64
65=pod
66
67=encoding UTF-8
68
69=head1 NAME
70
71Bread::Board::Dumper - Pretty printer for visualizing the layout of your Bread::Board
72
73=head1 VERSION
74
75version 0.37
76
77=head1 SYNOPSIS
78
79  use Bread::Board::Dumper;
80
81  print Bread::Board::Dumper->new->dump($container);
82
83  # container: Application
84  #   container: Controller
85  #   container: View
86  #     service: TT
87  #       depends_on: include_path
88  #   container: Model
89  #     service: dsn
90  #     service: schema
91  #       depends_on: pass
92  #       depends_on: ../dsn
93  #       depends_on: user
94
95=head1 DESCRIPTION
96
97This is a useful utility for dumping a clean view of a Bread::Board
98container.
99
100=head1 AUTHOR (actual)
101
102Daisuke Maki
103
104=head1 AUTHOR
105
106Stevan Little <stevan@iinteractive.com>
107
108=head1 BUGS
109
110Please report any bugs or feature requests on the bugtracker website
111https://github.com/stevan/BreadBoard/issues
112
113When submitting a bug or request, please include a test-file or a
114patch to an existing test-file that illustrates the bug or desired
115feature.
116
117=head1 COPYRIGHT AND LICENSE
118
119This software is copyright (c) 2019, 2017, 2016, 2015, 2014, 2013, 2011, 2009 by Infinity Interactive.
120
121This is free software; you can redistribute it and/or modify it under
122the same terms as the Perl 5 programming language system itself.
123
124=cut
125