Skip to content

Commit

Permalink
performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Dec 10, 2017
1 parent 203ccf3 commit 24e3f0a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to `spatie/crawler` will be documented in this file.

## 2.6.1 - 2017-12-10
- performance improvements

## 2.6.0 - 2017-10-16
- add `CrawlSubdomains` profile

Expand Down
30 changes: 20 additions & 10 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ class Url
/** @var null|string */
public $query;

/** @var null|string */
protected $toString;
/** @var bool */
public $shouldRerenderString = true;

/** @var string */
protected $renderedString = '';

/**
* @param string $url
Expand Down Expand Up @@ -66,7 +69,8 @@ public function hasCrawlableScheme(): bool
public function setScheme(string $scheme)
{
$this->scheme = $scheme;
$this->toString = null;

$this->shouldRerenderString = true;

return $this;
}
Expand All @@ -79,7 +83,8 @@ public function setScheme(string $scheme)
public function setHost(string $host)
{
$this->host = $host;
$this->toString = null;

$this->shouldRerenderString = true;

return $this;
}
Expand All @@ -92,7 +97,8 @@ public function setHost(string $host)
public function setPort(int $port)
{
$this->port = $port;
$this->toString = null;

$this->shouldRerenderString = true;

return $this;
}
Expand All @@ -103,7 +109,8 @@ public function setPort(int $port)
public function removeFragment()
{
$this->path = explode('#', $this->path)[0];
$this->toString = null;

$this->shouldRerenderString = true;

return $this;
}
Expand All @@ -116,7 +123,8 @@ public function removeFragment()
public function setPath(string $path)
{
$this->path = $path;
$this->toString = null;

$this->shouldRerenderString = true;

return $this;
}
Expand Down Expand Up @@ -189,17 +197,19 @@ public function isEqual(Url $otherUrl): bool
*/
public function __toString()
{
if ($this->toString === null) {
if ($this->shouldRerenderString) {
$path = $this->startsWith($this->path, '/') ? substr($this->path, 1) : $this->path;

$port = ($this->port === 80 ? '' : ":{$this->port}");

$queryString = (is_null($this->query) ? '' : "?{$this->query}");

$this->toString = "{$this->scheme}://{$this->host}{$port}/{$path}{$queryString}";
$this->renderedString = "{$this->scheme}://{$this->host}{$port}/{$path}{$queryString}";

$this->shouldRerenderString = false;
}

return $this->toString;
return $this->renderedString;
}

/**
Expand Down

0 comments on commit 24e3f0a

Please sign in to comment.