laravel-soap
laravel-soap copied to clipboard
Service already exists error when in loop
I'm trying to loop through some data and make a Soap call on each iteration.
The first one always processes without issue. However, after the first I get the following error
Service 'addNewJob' already exists. which is thrown by /vendor/artisaninweb/laravel-soap/src/Artisaninweb/SoapWrapper/SoapWrapper.php
The file contains the following code
public function add($name, Closure $closure)
{
if (!$this->has($name)) {
$service = new Service();
$closure($service);
$this->services[$name] = $service;
return $this;
}
Does it cache the name or something? how do I stop this error?
You need to unregister the service of the SoapWrapper after the execution because the worker instance is unique and in the second iteration tries to register again the service that is already registered. SoapWrapper::remove('addNewJob');
/** * Remove a service from the wrapper * * @param $name * @return $this */ public function remove($name) { if(!empty($this->services[$name])) { unset($this->services[$name]); } return $this; }