Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #42 SoapClient->_stream_context is private in php 8 #46

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 18 additions & 30 deletions src/AbstractSoapClientBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ abstract class AbstractSoapClientBase implements SoapClientInterface
*/
private ?SoapClient $soapClient = null;

/**
* The SoapClient's stream context.
* Stored separately since SoapClient->_stream_context is private in php 8.
* @var resource|null
*/
private $streamContext = null;

/**
* Contains Soap call result
* @var mixed
Expand Down Expand Up @@ -45,11 +52,6 @@ public function getSoapClient(): ?SoapClient
return $this->soapClient;
}

public function setSoapClient(SoapClient $soapClient): SoapClient
{
return ($this->soapClient = $soapClient);
}

public function initSoapClient(array $options): void
{
$wsdlOptions = [];
Expand All @@ -67,8 +69,10 @@ public function initSoapClient(array $options): void
$wsdlUrl = $wsdlOptions[str_replace(self::OPTION_PREFIX, '', self::WSDL_URL)];
unset($wsdlOptions[str_replace(self::OPTION_PREFIX, '', self::WSDL_URL)]);
}
$this->streamContext = stream_context_create();
$wsdlOptions['stream_context'] = $this->streamContext;
$soapClientClassName = $this->getSoapClientClassName();
$this->setSoapClient(new $soapClientClassName($wsdlUrl, $wsdlOptions));
$this->soapClient = new $soapClientClassName($wsdlUrl, $wsdlOptions);
}
}

Expand Down Expand Up @@ -302,20 +306,14 @@ public function setSoapHeader(string $namespace, string $name, $data, bool $must
public function setHttpHeader(string $headerName, $headerValue): bool
{
$state = false;
if ($this->getSoapClient() && !empty($headerName)) {
$streamContext = $this->getStreamContext();
if ($streamContext === null) {
$options = [];
$streamContext = $this->getStreamContext();
if ($this->getSoapClient() && $streamContext && !empty($headerName)) {
$options = stream_context_get_options($streamContext);
if (!array_key_exists('http', $options) || !is_array($options['http'])) {
$options['http'] = [];
$options['http']['header'] = '';
} else {
$options = stream_context_get_options($streamContext);
if (!array_key_exists('http', $options) || !is_array($options['http'])) {
$options['http'] = [];
$options['http']['header'] = '';
} elseif (!array_key_exists('header', $options['http'])) {
$options['http']['header'] = '';
}
} elseif (!array_key_exists('header', $options['http'])) {
$options['http']['header'] = '';
}
if (count($options) && array_key_exists('http', $options) && is_array($options['http']) && array_key_exists('header', $options['http']) && is_string($options['http']['header'])) {
$lines = explode("\r\n", $options['http']['header']);
Expand All @@ -336,17 +334,7 @@ public function setHttpHeader(string $headerName, $headerValue): bool
* Set the context http header option
*/
$options['http']['header'] = implode("\r\n", $newLines);
/**
* Create context if it does not exist
*/
if ($streamContext === null) {
$state = is_resource($this->getSoapClient()->_stream_context = stream_context_create($options));
} else {
/**
* Set the new context http header option
*/
$state = stream_context_set_option($this->getSoapClient()->_stream_context, 'http', 'header', $options['http']['header']);
}
$state = stream_context_set_option($this->streamContext, 'http', 'header', $options['http']['header']);
}
}

Expand All @@ -359,7 +347,7 @@ public function setHttpHeader(string $headerName, $headerValue): bool
*/
public function getStreamContext()
{
return ($this->getSoapClient() && isset($this->getSoapClient()->_stream_context) && is_resource($this->getSoapClient()->_stream_context)) ? $this->getSoapClient()->_stream_context : null;
return $this->streamContext;
}

/**
Expand Down