Skip to content

Commit

Permalink
Merge pull request #93 from peter-gribanov/writer_state
Browse files Browse the repository at this point in the history
Optimize check state in Writers
  • Loading branch information
peter-gribanov authored Jul 7, 2020
2 parents e5d1c2d + f002503 commit 83604f7
Show file tree
Hide file tree
Showing 33 changed files with 284 additions and 269 deletions.
8 changes: 8 additions & 0 deletions src/Limiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ final class Limiter
*/
private $used_bytes = 0;

/**
* @throws LinksOverflowException
*/
public function tryAddUrl(): void
{
if ($this->added_urls + 1 > self::LINKS_LIMIT) {
Expand All @@ -63,6 +66,9 @@ public function howManyUrlsAvailableToAdd(): int
return self::LINKS_LIMIT - $this->added_urls;
}

/**
* @throws SitemapsOverflowException
*/
public function tryAddSitemap(): void
{
if ($this->added_sitemaps + 1 > self::SITEMAPS_LIMIT) {
Expand All @@ -82,6 +88,8 @@ public function howManySitemapsAvailableToAdd(): int

/**
* @param int $used_bytes
*
* @throws SizeOverflowException
*/
public function tryUseBytes(int $used_bytes): void
{
Expand Down
4 changes: 2 additions & 2 deletions src/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ final class Location
private $location;

/**
* @throws InvalidLocationException
*
* @param string $location
*
* @throws InvalidLocationException
*/
public function __construct(string $location)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Sitemap/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Sitemap
/**
* @param Location|string $location
* @param \DateTimeInterface|null $last_modify
*
* @throws InvalidLastModifyException
*/
public function __construct($location, ?\DateTimeInterface $last_modify = null)
{
Expand Down
9 changes: 9 additions & 0 deletions src/Stream/IndexStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@
namespace GpsLab\Component\Sitemap\Stream;

use GpsLab\Component\Sitemap\Sitemap\Sitemap;
use GpsLab\Component\Sitemap\Stream\Exception\StreamStateException;

interface IndexStream
{
/**
* @throws StreamStateException
*/
public function open(): void;

/**
* @throws StreamStateException
*/
public function close(): void;

/**
* @param Sitemap $sitemap
*
* @throws StreamStateException
*/
public function pushSitemap(Sitemap $sitemap): void;
}
3 changes: 3 additions & 0 deletions src/Stream/LoggerStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace GpsLab\Component\Sitemap\Stream;

use GpsLab\Component\Sitemap\Stream\Exception\StreamStateException;
use GpsLab\Component\Sitemap\Url\Url;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -40,6 +41,8 @@ public function close(): void

/**
* @param Url $url
*
* @throws StreamStateException
*/
public function push(Url $url): void
{
Expand Down
9 changes: 9 additions & 0 deletions src/Stream/MultiStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace GpsLab\Component\Sitemap\Stream;

use GpsLab\Component\Sitemap\Stream\Exception\StreamStateException;
use GpsLab\Component\Sitemap\Url\Url;

final class MultiStream implements Stream
Expand All @@ -27,13 +28,19 @@ public function __construct(Stream ...$streams)
$this->streams = $streams;
}

/**
* @throws StreamStateException
*/
public function open(): void
{
foreach ($this->streams as $stream) {
$stream->open();
}
}

/**
* @throws StreamStateException
*/
public function close(): void
{
foreach ($this->streams as $stream) {
Expand All @@ -43,6 +50,8 @@ public function close(): void

/**
* @param Url $url
*
* @throws StreamStateException
*/
public function push(Url $url): void
{
Expand Down
8 changes: 8 additions & 0 deletions src/Stream/OutputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public function __construct(SitemapRender $render)
$this->limiter = new Limiter();
}

/**
* @throws StreamStateException
*/
public function open(): void
{
$this->state->open();
Expand All @@ -58,6 +61,9 @@ public function open(): void
$this->limiter->tryUseBytes(mb_strlen($this->end_string, '8bit'));
}

/**
* @throws StreamStateException
*/
public function close(): void
{
$this->state->close();
Expand All @@ -67,6 +73,8 @@ public function close(): void

/**
* @param Url $url
*
* @throws StreamStateException
*/
public function push(Url $url): void
{
Expand Down
6 changes: 6 additions & 0 deletions src/Stream/State/StreamState.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ final class StreamState
*/
private $state = self::STATE_CREATED;

/**
* @throws StreamStateException
*/
public function open(): void
{
if ($this->state === self::STATE_READY) {
Expand All @@ -37,6 +40,9 @@ public function open(): void
$this->state = self::STATE_READY;
}

/**
* @throws StreamStateException
*/
public function close(): void
{
if ($this->state === self::STATE_CLOSED) {
Expand Down
9 changes: 9 additions & 0 deletions src/Stream/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,25 @@

namespace GpsLab\Component\Sitemap\Stream;

use GpsLab\Component\Sitemap\Stream\Exception\StreamStateException;
use GpsLab\Component\Sitemap\Url\Url;

interface Stream
{
/**
* @throws StreamStateException
*/
public function open(): void;

/**
* @throws StreamStateException
*/
public function close(): void;

/**
* @param Url $url
*
* @throws StreamStateException
*/
public function push(Url $url): void;
}
8 changes: 8 additions & 0 deletions src/Stream/WritingIndexStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,19 @@ public function __construct(SitemapIndexRender $render, Writer $writer, string $
$this->limiter = new Limiter();
}

/**
* @throws StreamStateException
*/
public function open(): void
{
$this->state->open();
$this->writer->start($this->filename);
$this->writer->append($this->render->start());
}

/**
* @throws StreamStateException
*/
public function close(): void
{
$this->state->close();
Expand All @@ -75,6 +81,8 @@ public function close(): void

/**
* @param Sitemap $sitemap
*
* @throws StreamStateException
*/
public function pushSitemap(Sitemap $sitemap): void
{
Expand Down
12 changes: 12 additions & 0 deletions src/Stream/WritingSplitIndexStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ final class WritingSplitIndexStream implements Stream, IndexStream
* @param string $index_filename
* @param string $part_filename_pattern
* @param string $part_web_path_pattern
*
* @throws SplitIndexException
*/
public function __construct(
SitemapIndexRender $index_render,
Expand Down Expand Up @@ -151,6 +153,9 @@ public function __construct(
$this->part_limiter = new Limiter();
}

/**
* @throws StreamStateException
*/
public function open(): void
{
$this->state->open();
Expand All @@ -159,6 +164,9 @@ public function open(): void
$this->index_writer->append($this->index_render->start());
}

/**
* @throws StreamStateException
*/
public function close(): void
{
$this->state->close();
Expand All @@ -182,6 +190,8 @@ public function close(): void

/**
* @param Url $url
*
* @throws StreamStateException
*/
public function push(Url $url): void
{
Expand All @@ -204,6 +214,8 @@ public function push(Url $url): void

/**
* @param Sitemap $sitemap
*
* @throws StreamStateException
*/
public function pushSitemap(Sitemap $sitemap): void
{
Expand Down
10 changes: 10 additions & 0 deletions src/Stream/WritingSplitStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ final class WritingSplitStream implements SplitStream
* @param Writer $writer
* @param string $filename_pattern
* @param string $web_path_pattern
*
* @throws SplitIndexException
*/
public function __construct(
SitemapRender $render,
Expand Down Expand Up @@ -109,12 +111,18 @@ public function __construct(
$this->limiter = new Limiter();
}

/**
* @throws StreamStateException
*/
public function open(): void
{
$this->state->open();
$this->openPart();
}

/**
* @throws StreamStateException
*/
public function close(): void
{
$this->state->close();
Expand All @@ -130,6 +138,8 @@ public function close(): void

/**
* @param Url $url
*
* @throws StreamStateException
*/
public function push(Url $url): void
{
Expand Down
8 changes: 8 additions & 0 deletions src/Stream/WritingStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public function __construct(SitemapRender $render, Writer $writer, string $filen
$this->limiter = new Limiter();
}

/**
* @throws StreamStateException
*/
public function open(): void
{
$this->state->open();
Expand All @@ -74,6 +77,9 @@ public function open(): void
$this->limiter->tryUseBytes(mb_strlen($this->end_string, '8bit'));
}

/**
* @throws StreamStateException
*/
public function close(): void
{
$this->state->close();
Expand All @@ -84,6 +90,8 @@ public function close(): void

/**
* @param Url $url
*
* @throws StreamStateException
*/
public function push(Url $url): void
{
Expand Down
2 changes: 2 additions & 0 deletions src/Url/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ final class Language
/**
* @param string $language
* @param string $location
*
* @throws InvalidLanguageException
*/
public function __construct(string $language, string $location)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Url/Priority.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ private static function safeCreate(string $priority): self
/**
* @param string|float|int $priority
*
* @throws InvalidPriorityException
*
* @return self
*/
public static function create($priority): self
Expand Down
2 changes: 2 additions & 0 deletions src/Url/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Url
* @param ChangeFrequency|string|null $change_frequency
* @param Priority|string|float|int|null $priority
* @param array<string, string> $languages
*
* @throws InvalidLastModifyException
*/
public function __construct(
$location,
Expand Down
Loading

0 comments on commit 83604f7

Please sign in to comment.