Skip to content

Commit

Permalink
Merge pull request #4 from PhantPHP/upgrade
Browse files Browse the repository at this point in the history
Upgrade to PHP 8.1
  • Loading branch information
lennyrouanet authored Nov 10, 2022
2 parents 1fc8e76 + 1b38f86 commit 8c09915
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 209 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ vendor/*
# Dev
.DS_Store
.nova/*
.php-cs-fixer.cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Requirments

PHP >= 8.0
PHP >= 8.1


## Install
Expand Down
320 changes: 157 additions & 163 deletions component/AssetsVersions.php
Original file line number Diff line number Diff line change
@@ -1,175 +1,169 @@
<?php

declare(strict_types=1);

namespace Phant\AssetsVersions;

use Psr\SimpleCache\CacheInterface;
use Phant\AssetsVersions\Exception\{
PathToBeProcessedDoesNotExist,
};
use Phant\AssetsVersions\Exception\PathToBeProcessedDoesNotExist;
use Phant\AssetsVersions\DataStructure\{
AssetVersion,
AssetVersionCollection,
AssetVersion,
AssetVersionCollection,
};

class AssetsVersions
{
const CACHE_ITEM = 'assets-versions';

protected string $pathToBeProcessed;
protected array $extensionsToBeProcessed;
protected ?array $pathsToBeIgnored;
protected ?CacheInterface $cacheAdapter;

private AssetVersionCollection $assetVersionCollection;

public function __construct(
string $pathToBeProcessed,
array $extensionsToBeProcessed = [ 'css', 'js' ],
?array $pathsToBeIgnored = null,
?CacheInterface $cacheAdapter = null
)
{
if ( !is_dir( $pathToBeProcessed ) ) {
throw new PathToBeProcessedDoesNotExist;
}

$this->pathToBeProcessed = $pathToBeProcessed;
$this->extensionsToBeProcessed = array_map( 'strtolower', $extensionsToBeProcessed );
$this->pathsToBeIgnored = $pathsToBeIgnored;
$this->cacheAdapter = $cacheAdapter;

$this->assetVersionCollection = new AssetVersionCollection();

$this->loadFromCache();
}

public function of( string $assetPath ): string
{
$assetVersion = $this->assetVersionCollection->findFromAssetPath( $assetPath );

if ($assetVersion) {
return (string) $assetVersion;
}

$assetVersion = $this->getFromPath( $assetPath );

if ($assetVersion) {
$this->assetVersionCollection->add( $assetVersion );
$this->saveToCache();
return (string) $assetVersion;
}

return $assetPath;
}

public function generate(): void
{
$assetPathList = $this->getAssetPathList();

foreach ( $assetPathList as $assetPath ) {
$assetVersion = $this->getFromPath( $assetPath );
$this->assetVersionCollection->add( $assetVersion );
}

$this->saveToCache();
}

private function loadFromCache(): void
{
if ( !$this->cacheAdapter ) return;

$assetVersionCollection = $this->cacheAdapter->get( self::CACHE_ITEM );

if ( !is_a( $assetVersionCollection, get_class( $this->assetVersionCollection ) ) ) {
return;
}

$this->assetVersionCollection = $assetVersionCollection;
}

private function saveToCache(): void
{
if (!$this->cacheAdapter) return;

$this->cacheAdapter->set( self::CACHE_ITEM , $this->assetVersionCollection );
}

private function getFromPath( string $assetPath ): ?AssetVersion
{
$assetVersion = $this->getAssetVersionFromGitRevisions( $assetPath );

if ( $assetVersion ) {
return $assetVersion;
}

$assetVersion = $this->getAssetVersionFromFileUpdateTime( $assetPath );

if ( $assetVersion ) {
return $assetVersion;
}

return null;
}

private function getAssetVersionFromGitRevisions( string $assetPath ): ?AssetVersion
{
$command = sprintf( 'git log --oneline %s | wc -l', escapeshellarg( $this->pathToBeProcessed . $assetPath ) );
$revision = exec( $command );

if ( !$revision ) {
return null;
}

return new AssetVersion( $assetPath, AssetVersion::TYPE_VERSION, (int) $revision );
}

private function getAssetVersionFromFileUpdateTime( string $assetPath ): ?AssetVersion
{
$modificationTime = filemtime( $this->pathToBeProcessed . $assetPath );

if ( !$modificationTime ) {
return null;
}

return new AssetVersion( $assetPath, AssetVersion::TYPE_MODIFICATION_TIME, (int) $modificationTime );
}

private function getAssetPathList( string $subpath = '' ): array
{
$assetPathList = [];

$dh = opendir( $this->pathToBeProcessed . $subpath );

while ( $entry = readdir( $dh ) ) {

if ( $entry == '.' || $entry == '..' ) {
continue;
}

$entryPath = $subpath . $entry;

if ( is_dir( $entryPath )) {
$entryPath.= '/';

if (in_array( $entryPath, $this->pathsToBeIgnored ) ) {
continue;
}

$assetPathList = array_merge( $assetPathList, $this->getAssetPathList( $entryPath ) );

continue;
}

if ( !empty( $this->extensionsToBeProcessed ) && !in_array( strtolower( pathinfo( $entryPath, PATHINFO_EXTENSION ) ), $this->extensionsToBeProcessed ) ) {
continue;
}

$assetPathList[] = $entryPath;
}

closedir($dh);

return $assetPathList;
}
public const CACHE_ITEM = 'assets-versions';

protected array $extensionsToBeProcessed;
private AssetVersionCollection $assetVersionCollection;

public function __construct(
protected readonly string $pathToBeProcessed,
array $extensionsToBeProcessed = [ 'css', 'js' ],
protected readonly ?array $pathsToBeIgnored = null,
protected readonly ?CacheInterface $cacheAdapter = null
) {
if (!is_dir($pathToBeProcessed)) {
throw new PathToBeProcessedDoesNotExist();
}

$this->extensionsToBeProcessed = array_map('strtolower', $extensionsToBeProcessed);

$this->assetVersionCollection = new AssetVersionCollection();

$this->loadFromCache();
}

public function of(string $assetPath): string
{
$assetVersion = $this->assetVersionCollection->findFromAssetPath($assetPath);

if ($assetVersion) {
return (string) $assetVersion;
}

$assetVersion = $this->getFromPath($assetPath);

if ($assetVersion) {
$this->assetVersionCollection->add($assetVersion);
$this->saveToCache();
return (string) $assetVersion;
}

return $assetPath;
}

public function generate(): void
{
$assetPathList = $this->getAssetPathList();

foreach ($assetPathList as $assetPath) {
$assetVersion = $this->getFromPath($assetPath);
$this->assetVersionCollection->add($assetVersion);
}

$this->saveToCache();
}

private function loadFromCache(): void
{
if (!$this->cacheAdapter) {
return;
}

$assetVersionCollection = $this->cacheAdapter->get(self::CACHE_ITEM);

if (!is_a($assetVersionCollection, get_class($this->assetVersionCollection))) {
return;
}

$this->assetVersionCollection = $assetVersionCollection;
}

private function saveToCache(): void
{
if (!$this->cacheAdapter) {
return;
}

$this->cacheAdapter->set(self::CACHE_ITEM, $this->assetVersionCollection);
}

private function getFromPath(string $assetPath): ?AssetVersion
{
$assetVersion = $this->getAssetVersionFromGitRevisions($assetPath);

if ($assetVersion) {
return $assetVersion;
}

$assetVersion = $this->getAssetVersionFromFileUpdateTime($assetPath);

if ($assetVersion) {
return $assetVersion;
}

return null;
}

private function getAssetVersionFromGitRevisions(string $assetPath): ?AssetVersion
{
$command = sprintf('git log --oneline %s | wc -l', escapeshellarg($this->pathToBeProcessed . $assetPath));
$revision = exec($command);

if (!$revision) {
return null;
}

return new AssetVersion($assetPath, AssetVersion::TYPE_VERSION, (int) $revision);
}

private function getAssetVersionFromFileUpdateTime(string $assetPath): ?AssetVersion
{
$modificationTime = filemtime($this->pathToBeProcessed . $assetPath);

if (!$modificationTime) {
return null;
}

return new AssetVersion($assetPath, AssetVersion::TYPE_MODIFICATION_TIME, (int) $modificationTime);
}

private function getAssetPathList(string $subpath = ''): array
{
$assetPathList = [];

$dh = opendir($this->pathToBeProcessed . $subpath);

while ($entry = readdir($dh)) {
if ($entry == '.' || $entry == '..') {
continue;
}

$entryPath = $subpath . $entry;

if (is_dir($entryPath)) {
$entryPath.= '/';

if (in_array($entryPath, $this->pathsToBeIgnored)) {
continue;
}

$assetPathList = array_merge($assetPathList, $this->getAssetPathList($entryPath));

continue;
}

if (!empty($this->extensionsToBeProcessed) && !in_array(strtolower(pathinfo($entryPath, PATHINFO_EXTENSION)), $this->extensionsToBeProcessed)) {
continue;
}

$assetPathList[] = $entryPath;
}

closedir($dh);

return $assetPathList;
}
}
37 changes: 17 additions & 20 deletions component/DataStructure/AssetVersion.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
<?php

declare(strict_types=1);

namespace Phant\AssetsVersions\DataStructure;

final class AssetVersion
{
const TYPE_VERSION = 'ver';
const TYPE_MODIFICATION_TIME = 'lmod';

public string $assetPath;
public string $type;
public int $value;

public function __construct( string $assetPath, string $type, int $value )
{
$this->assetPath = $assetPath;
$this->type = $type;
$this->value = $value;
}

public function __toString()
{
return $this->assetPath . '?' . http_build_query([
$this->type => $this->value
]);
}
public const TYPE_VERSION = 'ver';
public const TYPE_MODIFICATION_TIME = 'lmod';

public function __construct(
public readonly string $assetPath,
public readonly string $type,
public readonly int $value
) {
}

public function __toString(): string
{
return $this->assetPath . '?' . http_build_query([
$this->type => $this->value
]);
}
}
Loading

0 comments on commit 8c09915

Please sign in to comment.