| 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 | use Moose; | ||
| 28 | use Path::Class::File; | ||
| 29 | use Net::ISC::DHCPd::Config; | ||
| 30 | |||
| 31 | 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 | sub children { | ||
| 40 | return Net::ISC::DHCPd::Config::children(); | ||
| 41 | } | ||
| 42 | |||
| 43 | __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 | 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 | our $regex = qr{^\s* include \s+ "([^"]+)" ;}x; | ||
| 74 | sub _build_root { shift->parent } | ||
| 75 | |||
| 76 | sub _build__filehandle { | ||
| 77 | my $self = shift; | ||
| 78 | my $file = $self->file; | ||
| 79 | |||
| 80 | if ($self->root->filename_callback) { | ||
| 81 | $file = Path::Class::File->new(&{$self->root->filename_callback}($file)); | ||
| 82 | } | ||
| 83 | |||
| 84 | if($file->is_relative and !-e $file) { | ||
| 85 | $file = Path::Class::File->new($self->root->file->dir, $file); | ||
| 86 | $self->file($file); # needed so dir stays updated with recursive includes | ||
| 87 | } | ||
| 88 | |||
| 89 | 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 | around parse => sub { | ||
| 108 | my $next = shift; | ||
| 109 | my $self = shift; | ||
| 110 | |||
| 111 | if($_[0] and $_[0] eq 'recursive') { | ||
| 112 | return '0e0'; | ||
| 113 | } | ||
| 114 | |||
| 115 | return $self->$next(@_); | ||
| 116 | }; | ||
| 117 | |||
| 118 | =head2 captured_to_args | ||
| 119 | |||
| 120 | See L<Net::ISC::DHCPd::Config::Role/captured_to_args>. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub captured_to_args { | ||
| 125 | 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 | sub generate { | ||
| 139 | my $self = shift; | ||
| 140 | |||
| 141 | if($self->generate_with_include) { | ||
| 142 | my $text = $self->generate_config_from_children; | ||
| 143 | return $text ? $text : "# forgot to parse " .$self->file ."?"; | ||
| 144 | |||
| 145 | } | ||
| 146 | else { | ||
| 147 | 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 | __PACKAGE__->meta->make_immutable; | ||
| 159 | 1; |