implement setSource method to update value properly

This commit is contained in:
yggverse 2025-11-11 13:35:35 +02:00
parent a98fb2ef5a
commit 3dd8a5256e

View file

@ -49,12 +49,12 @@ class Cli
);
}
// Appends valid address to crawler queue
// Appends address in crawler queue
public function addSource(
string $url
): bool
{
// Validate given value and check it is unique in pool
// Validate given value and check it is unique in the pool
if ($this->_source($url) && !in_array($url, $this->source))
{
$this->source[] = $url;
@ -65,6 +65,23 @@ class Cli
return false;
}
// Updates address in crawler queue
public function setSource(
int $offset,
string $url
): bool
{
// Validate given value and check it is unique in the pool
if (isset($this->source[$offset]) && $this->_source($url) && $this->source[$offset] != $url)
{
$this->source[$offset] = $url;
return true;
}
return false;
}
// Begin crawler task
public function start(
int $offset = 0
@ -187,12 +204,29 @@ class Cli
if (filter_var($response->getMeta(), FILTER_VALIDATE_URL)) // @TODO resolve relative locations
{
// Apply redirection target to the current destination
$this->source[$offset] = $response->getMeta();
if ($this->setSource($offset, trim($response->getMeta())))
{
// Rescan current destination using updated location
$this->start(
$offset
);
}
else
{
print(
Message::red(
sprintf(
_("\tdestination could not be updated due to the conditions or it has already been indexed: `%s`"),
$response->getMeta()
)
)
);
// Rescan current destination using updated location
$this->start(
$offset
);
// Continue next location
$this->start(
$offset + 1
);
}
}
else
{