| 1 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | package YAML::PP::Schema::Include; | ||||||||||||
| 4 | |||||||||||||
| 5 | 2 | our $VERSION = 'v0.41.1'; # TRIAL VERSION | |||||||||||
| 6 | |||||||||||||
| 7 | 2 | ▶ | use Carp qw/ croak /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use Scalar::Util qw/ weaken /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use File::Basename qw/ dirname /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 4 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||
| 12 | 4 | my ($class, %args) = @_; | |||||||||||
| 13 | |||||||||||||
| 14 | 4 | my $paths = delete $args{paths}; | |||||||||||
| 15 | 4 | ▶ | if (defined $paths) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 16 | 2 | ▶ | unless (ref $paths eq 'ARRAY') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 17 | 2 | $paths = [$paths]; | |||||||||||
| 18 | } | ||||||||||||
| 19 | } | ||||||||||||
| 20 | else { | ||||||||||||
| 21 | 2 | $paths = []; | |||||||||||
| 22 | } | ||||||||||||
| 23 | 4 | ▶ | my $allow_absolute = $args{allow_absolute} || 0; | ||||||||||
|
Condition50%1/2
$args{'allow_absolute'} || 0
MC/DC0%0/1
$args{'allow_absolute'} || 0
$args{'allow_absolute'}
Truth table50%1/2
A
A= $args{'allow_absolute'}
| |||||||||||||
| 24 | 4 | ▶ | my $loader = $args{loader} || \&default_loader; | ||||||||||
|
Condition50%1/2
$args{'loader'} || \&YAML::PP::Schema::Include::default_loader
MC/DC0%0/1
$args{'loader'} || \&YAML::PP::Schema::Include::default_loader
$args{'loader'}
Truth table50%1/2
A
A= $args{'loader'}
| |||||||||||||
| 25 | |||||||||||||
| 26 | 4 | my $self = bless { | |||||||||||
| 27 | paths => $paths, | ||||||||||||
| 28 | allow_absolute => $allow_absolute, | ||||||||||||
| 29 | last_includes => [], | ||||||||||||
| 30 | cached => {}, | ||||||||||||
| 31 | loader => $loader, | ||||||||||||
| 32 | }, $class; | ||||||||||||
| 33 | 4 | return $self; | |||||||||||
| 34 | } | ||||||||||||
| 35 | |||||||||||||
| 36 | 0 | ▶ | sub init { | ||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 37 | 0 | my ($self) = @_; | |||||||||||
| 38 | 0 | $self->{last_includes} = []; | |||||||||||
| 39 | 0 | $self->{cached} = []; | |||||||||||
| 40 | } | ||||||||||||
| 41 | |||||||||||||
| 42 | 11 | ▶ | sub paths { $_[0]->{paths} } | ||||||||||
|
Subroutine
paths: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 43 | 11 | ▶ | sub allow_absolute { $_[0]->{allow_absolute} } | ||||||||||
|
Subroutine
allow_absolute: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 44 | 15 | ▶ | sub yp { | ||||||||||
|
Subroutine
yp: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 45 | 15 | my ($self, $yp) = @_; | |||||||||||
| 46 | 15 | ▶ | if (@_ == 2) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | 4 | $self->{yp} = $yp; | |||||||||||
| 48 | 4 | weaken $self->{yp}; | |||||||||||
| 49 | 4 | return $yp; | |||||||||||
| 50 | } | ||||||||||||
| 51 | 11 | return $self->{yp}; | |||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | 4 | ▶ | sub register { | ||||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 55 | 4 | my ($self, %args) = @_; | |||||||||||
| 56 | 4 | my $schema = $args{schema}; | |||||||||||
| 57 | |||||||||||||
| 58 | $schema->add_resolver( | ||||||||||||
| 59 | tag => '!include', | ||||||||||||
| 60 | 4 | ▶ | match => [ all => sub { $self->include(@_) } ], | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 61 | implicit => 0, | ||||||||||||
| 62 | ); | ||||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | 11 | ▶ | sub include { | ||||||||||
|
Subroutine
include: called CC=13 SCAR=25.7
Pod
undocumented
| |||||||||||||
| 66 | 11 | my ($self, $constructor, $event) = @_; | |||||||||||
| 67 | 11 | my $yp = $self->yp; | |||||||||||
| 68 | 11 | my $search_paths = $self->paths; | |||||||||||
| 69 | 11 | my $allow_absolute = $self->allow_absolute; | |||||||||||
| 70 | |||||||||||||
| 71 | 11 | my $relative = not @$search_paths; | |||||||||||
| 72 | 11 | ▶ | if ($relative) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 73 | 6 | my $last_includes = $self->{last_includes}; | |||||||||||
| 74 | 6 | ▶ | if (@$last_includes) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 3 | $search_paths = [ $last_includes->[-1] ]; | |||||||||||
| 76 | } | ||||||||||||
| 77 | else { | ||||||||||||
| 78 | # we are in the top-level file and need to look into | ||||||||||||
| 79 | # the original YAML::PP instance | ||||||||||||
| 80 | 3 | my $filename = $yp->loader->filename; | |||||||||||
| 81 | 3 | $search_paths = [dirname $filename]; | |||||||||||
| 82 | } | ||||||||||||
| 83 | } | ||||||||||||
| 84 | 11 | my $filename = $event->{value}; | |||||||||||
| 85 | |||||||||||||
| 86 | 11 | my $fullpath; | |||||||||||
| 87 | 11 | ▶ | if (File::Spec->file_name_is_absolute($filename)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 1 | ▶ | unless ($allow_absolute) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | 1 | croak "Absolute filenames not allowed"; | |||||||||||
| 90 | } | ||||||||||||
| 91 | 0 | $fullpath = $filename; | |||||||||||
| 92 | } | ||||||||||||
| 93 | else { | ||||||||||||
| 94 | 10 | my @paths = File::Spec->splitdir($filename); | |||||||||||
| 95 | 10 | ▶ | unless ($allow_absolute) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | # if absolute paths are not allowed, we also may not use upwards .. | ||||||||||||
| 97 | 10 | @paths = File::Spec->no_upwards(@paths); | |||||||||||
| 98 | } | ||||||||||||
| 99 | 10 | for my $candidate (@$search_paths) { | |||||||||||
| 100 | 10 | my $test = File::Spec->catfile( $candidate, @paths ); | |||||||||||
| 101 | 10 | ▶ | if (-e $test) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | 9 | $fullpath = $test; | |||||||||||
| 103 | 9 | last; | |||||||||||
| 104 | } | ||||||||||||
| 105 | } | ||||||||||||
| 106 | 10 | ▶ | croak "File '$filename' not found" unless defined $fullpath; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | } | ||||||||||||
| 108 | |||||||||||||
| 109 | 9 | ▶ | if ($self->{cached}->{ $fullpath }++) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | 1 | croak "Circular include '$fullpath'"; | |||||||||||
| 111 | } | ||||||||||||
| 112 | 8 | ▶ | if ($relative) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | 5 | push @{ $self->{last_includes} }, dirname $fullpath; | |||||||||||
| 114 | } | ||||||||||||
| 115 | |||||||||||||
| 116 | # We need a new object because we are still in the parsing and | ||||||||||||
| 117 | # constructing process | ||||||||||||
| 118 | 8 | my $clone = $yp->clone; | |||||||||||
| 119 | 8 | my ($data) = $self->loader->($clone, $fullpath); | |||||||||||
| 120 | |||||||||||||
| 121 | 6 | ▶ | if ($relative) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | 3 | pop @{ $self->{last_includes} }; | |||||||||||
| 123 | } | ||||||||||||
| 124 | 6 | ▶ | unless (--$self->{cached}->{ $fullpath }) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | 6 | delete $self->{cached}->{ $fullpath }; | |||||||||||
| 126 | } | ||||||||||||
| 127 | 6 | return $data; | |||||||||||
| 128 | } | ||||||||||||
| 129 | |||||||||||||
| 130 | 8 | ▶ | sub loader { | ||||||||||
|
Subroutine
loader: called CC=2 SCAR=8.4
Pod
undocumented
| |||||||||||||
| 131 | 8 | my ($self, $code) = @_; | |||||||||||
| 132 | 8 | ▶ | if (@_ == 2) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 133 | 0 | $self->{loader} = $code; | |||||||||||
| 134 | 0 | return $code; | |||||||||||
| 135 | } | ||||||||||||
| 136 | 8 | return $self->{loader}; | |||||||||||
| 137 | } | ||||||||||||
| 138 | 8 | ▶ | sub default_loader { | ||||||||||
|
Subroutine
default_loader: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 139 | 8 | my ($yp, $filename) = @_; | |||||||||||
| 140 | 8 | $yp->load_file($filename); | |||||||||||
| 141 | } | ||||||||||||
| 142 | |||||||||||||
| 143 | 2 | 1; | |||||||||||
| 144 | |||||||||||||
| 145 | __END__ | ||||||||||||