-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Splitting cached meta saving/loader into separate class
- Loading branch information
1 parent
2c3d13a
commit a128be2
Showing
3 changed files
with
79 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |