Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileModifyTimeStrategy #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/VersionStrategy/CacheAssets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\Asset\VersionStrategy;

final class CacheAssets
{
private bool $debugMode;

private string $tempFile;

/** @var ?array<int> */
private ?array $files = null;

private bool $save = false;


public function __construct(bool $debugMode, string $tempDir)
{
$this->debugMode = $debugMode;
$this->tempFile = $tempDir . '/_assets.php';
}


public function load(string $pathname): int
{
$this->loadCache();
if (isset($this->files[$pathname])) {
return $this->files[$pathname];
}

$this->save = true;
$mtime = filemtime($pathname);

return $this->files[$pathname] = $mtime === false ? 0 : $mtime;
}


private function loadCache(): void
{
if ($this->files !== null) {
return;
} elseif ($this->debugMode === true) {
$this->files = [];
} else {
$this->files = require $this->tempFile;
}
}


/**
* Clear local cache
* @return static
*/
public function clear()
{
$this->files = [];
$this->save = true;

return $this;
}


public function __destruct()
{
if ($this->debugMode === false && $this->save === true) {
file_put_contents($this->tempFile, '<?php return ' . var_export($this->files, true) . ';');
}
}

}
38 changes: 38 additions & 0 deletions src/VersionStrategy/FileModifyTimeStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace SixtyEightPublishers\Asset\VersionStrategy;

use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;

final class FileModifyTimeStrategy implements VersionStrategyInterface
{
private string $wwwDir;

private CacheAssets $cacheAssets;


public function __construct(string $wwwDir, CacheAssets $cacheAssets)
{
$this->wwwDir = $wwwDir;
$this->cacheAssets = $cacheAssets;
}


public function getVersion(string $path): string
{
return (string) $this->cacheAssets->load($this->absolutePath($path));
}


public function applyVersion(string $path): string
{
return "$path?" . $this->getVersion($path);
}


private function absolutePath(string $path): string
{
return "$this->wwwDir/" . ltrim($path, '/');
}

}
8 changes: 8 additions & 0 deletions tests/VersionStrategy/CacheAssetsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace SixtyEightPublishers\Asset\Tests\VersionStrategy;

final class CacheAssetsTest
{

}
48 changes: 48 additions & 0 deletions tests/VersionStrategy/FileModifyTimeStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\Asset\Tests\VersionStrategy;

use SixtyEightPublishers\Asset\VersionStrategy\CacheAssets;
use SixtyEightPublishers\Asset\VersionStrategy\FileModifyTimeStrategy;
use Tester\Assert;
use Tester\TestCase;

require __DIR__ . '/../bootstrap.php';

/**
* @testCase
*/
final class FileModifyTimeStrategyTest extends TestCase
{

public function testVersionLikeMTime(): void
{
$tempDir = sys_get_temp_dir() . '/' . uniqid('68publishers:AssetExtensionTest', true);
$wwwDir = "$tempDir/www";
@mkdir("$wwwDir/js", 0755, true);

$fileA = 'js/a.js';
$pathA = "$wwwDir/$fileA";
$unixtimeA = 1678000000;
touch($pathA, $unixtimeA);

$cacheAssets = new CacheAssets(true, $tempDir);
$fileModifyStrategy = new FileModifyTimeStrategy($wwwDir, $cacheAssets);

Assert::same((string) $unixtimeA, $fileModifyStrategy->getVersion($fileA));
Assert::same("$fileA?$unixtimeA", $fileModifyStrategy->applyVersion($fileA));
Assert::same("/$fileA?$unixtimeA", $fileModifyStrategy->applyVersion("/$fileA"));

$unixtimeB = 1679000000;
touch($pathA, $unixtimeB);
Assert::same((string) $unixtimeA, $fileModifyStrategy->getVersion($fileA));
$cacheAssets->clear();
clearstatcache();
Assert::same((string) $unixtimeB, $fileModifyStrategy->getVersion($fileA));
}

}

(new FileModifyTimeStrategyTest())->run();