Skip to content

Commit

Permalink
Splitting cached meta saving/loader into separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverryan committed Feb 2, 2019
1 parent 2c3d13a commit a128be2
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 33 deletions.
41 changes: 8 additions & 33 deletions lib/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@
use Doctrine\RST\Event\PreBuildParseEvent;
use Doctrine\RST\Event\PreBuildRenderEvent;
use Doctrine\RST\Event\PreBuildScanEvent;
use Doctrine\RST\Meta\CachedMetasLoader;
use Doctrine\RST\Meta\Metas;
use Symfony\Component\Filesystem\Filesystem;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function is_dir;
use function serialize;
use function unserialize;

class Builder
{
Expand All @@ -39,6 +35,9 @@ class Builder
/** @var Metas */
private $metas;

/** @var CachedMetasLoader */
private $cachedMetasLoader;

/** @var Documents */
private $documents;

Expand All @@ -60,6 +59,8 @@ public function __construct(?Kernel $kernel = null)

$this->metas = new Metas();

$this->cachedMetasLoader = new CachedMetasLoader();

$this->documents = new Builder\Documents(
$this->filesystem,
$this->metas
Expand Down Expand Up @@ -117,7 +118,7 @@ public function build(
}

if ($this->configuration->getUseCachedMetas()) {
$this->loadCachedMetas($targetDirectory);
$this->cachedMetasLoader->loadCachedMetaEntries($targetDirectory, $this->metas);
}

$parseQueue = $this->scan($directory, $targetDirectory);
Expand All @@ -126,7 +127,7 @@ public function build(

$this->render($directory, $targetDirectory);

$this->saveMetas($targetDirectory);
$this->cachedMetasLoader->cacheMetaEntries($targetDirectory, $this->metas);
}

public function copy(string $source, ?string $destination = null) : self
Expand Down Expand Up @@ -196,30 +197,4 @@ private function render(string $directory, string $targetDirectory) : void
new PostBuildRenderEvent($this, $directory, $targetDirectory)
);
}

private function loadCachedMetas(string $targetDirectory) : void
{
$metaCachePath = $this->getMetaCachePath($targetDirectory);
if (! file_exists($metaCachePath)) {
return;
}

$contents = file_get_contents($metaCachePath);

if ($contents === false) {
return;
}

$this->metas->setMetaEntries(unserialize($contents));
}

private function saveMetas(string $targetDirectory) : void
{
file_put_contents($this->getMetaCachePath($targetDirectory), serialize($this->metas->getAll()));
}

private function getMetaCachePath(string $targetDirectory) : string
{
return $targetDirectory . '/metas.php';
}
}
42 changes: 42 additions & 0 deletions lib/Meta/CachedMetasLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Doctrine\RST\Meta;

use LogicException;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function serialize;
use function sprintf;
use function unserialize;

final class CachedMetasLoader
{
public function loadCachedMetaEntries(string $targetDirectory, Metas $metas) : void
{
$metaCachePath = $this->getMetaCachePath($targetDirectory);
if (! file_exists($metaCachePath)) {
return;
}

$contents = file_get_contents($metaCachePath);

if ($contents === false) {
throw new LogicException(sprintf('Could not load file "%s"', $contents));
}

$metas->setMetaEntries(unserialize($contents));
}

public function cacheMetaEntries(string $targetDirectory, Metas $metas) : void
{
file_put_contents($this->getMetaCachePath($targetDirectory), serialize($metas->getAll()));
}

private function getMetaCachePath(string $targetDirectory) : string
{
return $targetDirectory . '/metas.php';
}
}
29 changes: 29 additions & 0 deletions tests/Meta/CachedMetasLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\RST\Meta;

use Doctrine\RST\Meta\CachedMetasLoader;
use Doctrine\RST\Meta\MetaEntry;
use Doctrine\RST\Meta\Metas;
use PHPUnit\Framework\TestCase;
use function sys_get_temp_dir;

class CachedMetasLoaderTest extends TestCase
{
public function testSaveAndLoadCachedMetaEntries() : void
{
$targetDir = sys_get_temp_dir();
$meta1 = new MetaEntry('file1', 'url1', 'title1', [], [], [], [], 0);
$meta2 = new MetaEntry('file2', 'url2', 'title2', [], [], [], [], 0);

$metasBefore = new Metas([$meta1, $meta2]);
$metasAfter = new Metas();

$loader = new CachedMetasLoader();
$loader->cacheMetaEntries($targetDir, $metasBefore);
$loader->loadCachedMetaEntries($targetDir, $metasAfter);
self::assertEquals([$meta1, $meta2], $metasAfter->getAll());
}
}

0 comments on commit a128be2

Please sign in to comment.