| 1 | package XML::CompileX::Schema::Loader; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use Modern::Perl '2010'; ## no critic (Modules::ProhibitUseQuotedVersion) | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 2 | our $VERSION = '0.006'; # VERSION | |||||||||||
| 6 | 2 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use MooX::Types::MooseLike::Base qw(ArrayRef HashRef InstanceOf); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use HTTP::Exception; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use List::MoreUtils 'uniq'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 2 | ▶ | use LWP::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 2 | ▶ | use URI; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 2 | ▶ | use XML::Compile::WSDL11; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 2 | ▶ | use XML::Compile::Util 'SCHEMA2001'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 2 | ▶ | use XML::Compile::SOAP::Util 'WSDL11'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 2 | ▶ | use XML::LibXML; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | has uris => ( | ||||||||||||
| 19 | is => 'rwp', | ||||||||||||
| 20 | isa => ArrayRef [ InstanceOf ['URI'] ], | ||||||||||||
| 21 | required => 1, | ||||||||||||
| 22 | 2 | ▶ | coerce => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 23 | 'ARRAY' eq ref $_[0] | ||||||||||||
| 24 | 2 | ▶ | ? [ map { URI->new($_) } @{ $_[0] } ] | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | : [ URI->new( $_[0] ) ]; | ||||||||||||
| 26 | }, | ||||||||||||
| 27 | 2 | ); | |||||||||||
| 28 | |||||||||||||
| 29 | has user_agent => ( | ||||||||||||
| 30 | is => 'lazy', | ||||||||||||
| 31 | isa => InstanceOf ['LWP::UserAgent'], | ||||||||||||
| 32 | 0 | ▶ | default => sub { LWP::UserAgent->new }, | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 33 | 2 | ); | |||||||||||
| 34 | |||||||||||||
| 35 | 2 | has wsdl => ( is => 'lazy', isa => InstanceOf ['XML::Compile::WSDL11'] ); | |||||||||||
| 36 | |||||||||||||
| 37 | 0 | ▶ | sub _build_wsdl { | ||||||||||
|
Subroutine
_build_wsdl: not called CC=2 SCAR=17.9
| |||||||||||||
| 38 | 0 | my $self = shift; | |||||||||||
| 39 | 0 | my @uri = @{ $self->uris }; | |||||||||||
| 40 | |||||||||||||
| 41 | 0 | my $wsdl = XML::Compile::WSDL11->new( | |||||||||||
| 42 | $self->_get_uri_content_ref( shift @uri ) ); | ||||||||||||
| 43 | 0 | for (@uri) { $wsdl->addWSDL( $self->_get_uri_content_ref($_) ) } | |||||||||||
| 44 | |||||||||||||
| 45 | 0 | return $wsdl; | |||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | 1 | ▶ | sub collect_imports { | ||||||||||
|
Subroutine
collect_imports: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 49 | 1 | my ( $self, @uri ) = @_; | |||||||||||
| 50 | 1 | my $wsdl = $self->wsdl; | |||||||||||
| 51 | $self->_set_uris( | ||||||||||||
| 52 | 1 | [ @uri = uniq @uri, map { $_->as_string } @{ $self->uris } ] ); | |||||||||||
| 53 | 1 | for my $uri ( @{ $self->uris } ) { | |||||||||||
| 54 | 1 | $wsdl->addWSDL( $self->_get_uri_content_ref($uri) ); | |||||||||||
| 55 | 1 | $wsdl = $self->_do_imports( $wsdl, $uri ); | |||||||||||
| 56 | } | ||||||||||||
| 57 | 1 | $wsdl->importDefinitions( [ values %{ $self->_imports } ] ); | |||||||||||
| 58 | 1 | return $wsdl; | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | 1 | ▶ | has _imports => ( is => 'rw', isa => HashRef, default => sub { {} } ); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 62 | |||||||||||||
| 63 | 3 | ▶ | sub _do_imports { | ||||||||||
|
Subroutine
_do_imports: called CC=4 SCAR=13.9
| |||||||||||||
| 64 | 3 | my ( $self, $wsdl, @locations ) = @_; | |||||||||||
| 65 | |||||||||||||
| 66 | 3 | for my $uri ( grep { not exists $self->_imports->{ $_->as_string } } | |||||||||||
| 67 | @locations ) | ||||||||||||
| 68 | { | ||||||||||||
| 69 | 3 | my $content_ref = $self->_get_uri_content_ref($uri); | |||||||||||
| 70 | 3 | my $doc = XML::LibXML->load_xml( string => $content_ref ); | |||||||||||
| 71 | $self->_imports( | ||||||||||||
| 72 | 3 | +{ %{ $self->_imports }, $uri->as_string => $content_ref } ); | |||||||||||
| 73 | |||||||||||||
| 74 | 3 | ▶ | if ( 'definitions' eq $doc->documentElement->getName ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 2 | $wsdl->addWSDL($content_ref); | |||||||||||
| 76 | } | ||||||||||||
| 77 | 3 | $wsdl->importDefinitions($content_ref); | |||||||||||
| 78 | |||||||||||||
| 79 | my @imports = ( | ||||||||||||
| 80 | _collect( 'location', $uri, $doc, WSDL11, 'import' ), | ||||||||||||
| 81 | 3 | map { _collect( 'schemaLocation', $uri, $doc, SCHEMA2001, $_ ) } | |||||||||||
| 82 | qw(import include), | ||||||||||||
| 83 | ); | ||||||||||||
| 84 | 3 | ▶ | if (@imports) { $wsdl = $self->_do_imports( $wsdl, @imports ) } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 3 | undef $doc; | |||||||||||
| 86 | } | ||||||||||||
| 87 | 3 | return $wsdl; | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 9 | ▶ | sub _collect { | ||||||||||
|
Subroutine
_collect: called CC=1 SCAR=0.0
| |||||||||||||
| 91 | 9 | my ( $attr, $uri, $document, $ns, $element ) = @_; | |||||||||||
| 92 | return | ||||||||||||
| 93 | 9 | map { URI->new_abs( $_->getAttribute($attr), $uri ) } | |||||||||||
| 94 | $document->getElementsByTagNameNS( $ns => $element ); | ||||||||||||
| 95 | } | ||||||||||||
| 96 | |||||||||||||
| 97 | 4 | ▶ | sub _get_uri_content_ref { | ||||||||||
|
Subroutine
_get_uri_content_ref: called CC=3 SCAR=13.3
| |||||||||||||
| 98 | 4 | my ( $self, $uri ) = @_; | |||||||||||
| 99 | 4 | my $response = $self->user_agent->get($uri); | |||||||||||
| 100 | 4 | ▶ | if ( $response->is_error ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 101 | 0 | ▶ | HTTP::Exception->throw( $response->code, | ||||||||||
|
Condition0%0/2
$response->message // ""
MC/DC0%0/1
$response->message // ""
$response->message
Truth table0%0/2
A
A= $response->message
| |||||||||||||
| 102 | status_message => sprintf '"%s": %s' => | ||||||||||||
| 103 | ( $uri->as_string, $response->message // q{} ) ); | ||||||||||||
| 104 | } | ||||||||||||
| 105 | 4 | return $response->decoded_content( ref => 1, raise_error => 1 ); | |||||||||||
| 106 | } | ||||||||||||
| 107 | |||||||||||||
| 108 | 2 | 1; | |||||||||||
| 109 | |||||||||||||
| 110 | # ABSTRACT: Load a web service and its dependencies for XML::Compile::WSDL11 | ||||||||||||
| 111 | |||||||||||||
| 112 | __END__ | ||||||||||||