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