| 1 | package Net::ISC::DHCPd::Config::Include; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Net::ISC::DHCPd::Config::Include - Hold content of included file | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | See L<Net::ISC::DHCPd::Config::Role> for methods and attributes without | ||||||||||||||||||||||||||||
| 10 | documentation. | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | An instance from this class, comes from / will produce: | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | include "$file_attribute_value"; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Example: | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | my $include = $config->includes->[0]; | ||||||||||||||||||||||||||||
| 19 | $include->parse; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | See L<Net::ISC::DHCPd::Config/SYNOPSIS>. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 27 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 27 | ▶ | use Path::Class::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 27 | ▶ | use Net::ISC::DHCPd::Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 27 | with 'Net::ISC::DHCPd::Config::Root'; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head2 children | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | See L<Net::ISC::DHCPd::Config::Role/children>. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 37 | ▶ | sub children { | ||||||||||||||||||||||||||
|
Subroutine
children: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 37 | return Net::ISC::DHCPd::Config::children(); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 27 | __PACKAGE__->create_children(__PACKAGE__->children()); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head2 generate_with_include | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | This attribute holds a boolean value. L</generate> will result in | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | include "path/from/file/attribute"; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | when false, and the included config if true. This attribute is false | ||||||||||||||||||||||||||||
| 54 | by default. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | Example: | ||||||||||||||||||||||||||||
| 57 | $include->generate_with_include(1); | ||||||||||||||||||||||||||||
| 58 | $include->generate; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 27 | has generate_with_include => ( | |||||||||||||||||||||||||||
| 63 | is => 'rw', | ||||||||||||||||||||||||||||
| 64 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 65 | default => 0, | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head2 regex | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | See L<Net::ISC::DHCPd::Config::Role/regex>. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||
| 73 | 27 | our $regex = qr{^\s* include \s+ "([^"]+)" ;}x; | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub _build_root { shift->parent } | ||||||||||||||||||||||||||
|
Subroutine
_build_root: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 5 | ▶ | sub _build__filehandle { | ||||||||||||||||||||||||||
|
Subroutine
_build__filehandle: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 77 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 78 | 5 | my $file = $self->file; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 5 | ▶ | if ($self->root->filename_callback) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 1 | $file = Path::Class::File->new(&{$self->root->filename_callback}($file)); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 5 | ▶ | if($file->is_relative and !-e $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$file->is_relative and not -e $file
MC/DC50%1/2
$file->is_relative and not -e $file
$file->is_relative
not -e $file
Truth table66%2/3
A and not(B)
A= $file->is_relativeB= not -e $file
| |||||||||||||||||||||||||||||
| 85 | 4 | $file = Path::Class::File->new($self->root->file->dir, $file); | |||||||||||||||||||||||||||
| 86 | 4 | $self->file($file); # needed so dir stays updated with recursive includes | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 5 | return $file->openr; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 METHODS | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head2 parse | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | This around method modifier will stop the parser when parsing | ||||||||||||||||||||||||||||
| 97 | recursively, which will require the user to manually parse the | ||||||||||||||||||||||||||||
| 98 | included files from the config. Reason for this is that the | ||||||||||||||||||||||||||||
| 99 | C<parse()> method returns the number of lines in a single file. | ||||||||||||||||||||||||||||
| 100 | and counting lines from included files will break this behaviour. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | See also L<Net::ISC::DHCPd::Config::Role/parse> and | ||||||||||||||||||||||||||||
| 103 | L<Net::ISC::DHCPd::Config/SYNOPSIS>. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 10 | ▶ | around parse => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 108 | 10 | my $next = shift; | |||||||||||||||||||||||||||
| 109 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 10 | ▶ | if($_[0] and $_[0] eq 'recursive') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[0] and $_[0] eq "recursive"
MC/DC50%1/2
$_[0] and $_[0] eq "recursive"
$_[0]
$_[0] eq "recursive"
Truth table66%2/3
A and B
A= $_[0]B= $_[0] eq "recursive"
| |||||||||||||||||||||||||||||
| 112 | 5 | return '0e0'; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 5 | return $self->$next(@_); | |||||||||||||||||||||||||||
| 116 | 27 | }; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 captured_to_args | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | See L<Net::ISC::DHCPd::Config::Role/captured_to_args>. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 5 | ▶ | sub captured_to_args { | ||||||||||||||||||||||||||
|
Subroutine
captured_to_args: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 5 | return { file => $_[0] }; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head2 generate | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | This method can either result in C<include ...;> line or the whole | ||||||||||||||||||||||||||||
| 131 | config of the included file. See L</generate_with_include> for how | ||||||||||||||||||||||||||||
| 132 | to control the behaviour. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | See also L<Net::ISC::DHCPd::Config::Role/generate>. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =cut | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 4 | ▶ | sub generate { | ||||||||||||||||||||||||||
|
Subroutine
generate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 4 | ▶ | if($self->generate_with_include) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 2 | my $text = $self->generate_config_from_children; | |||||||||||||||||||||||||||
| 143 | 2 | ▶ | return $text ? $text : "# forgot to parse " .$self->file ."?"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | else { | ||||||||||||||||||||||||||||
| 147 | 2 | return qq(include ") .$self->file .qq(";); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | See L<Net::ISC::DHCPd>. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||
| 158 | 27 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 159 | 27 | 1; | |||||||||||||||||||||||||||