Skip to content

Commit

Permalink
Merge pull request #3 from PhantPHP/version-3
Browse files Browse the repository at this point in the history
Version 3
  • Loading branch information
lennyrouanet authored Apr 29, 2022
2 parents 84364e5 + 6afff76 commit 1fc8e76
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 53 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Dependencies
composer.lock
vendor/*

# Dev
.DS_Store
.nova/*
27 changes: 2 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,15 @@ And this code when calling your assets :

## Exemple with a cache manager


Exemple with Symfony FilesystemAdapter :

```php
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Phant\AssetsVersions\AssetsVersions;


// cache

$cache = new FilesystemAdapter( 'my-app-cache' );


// assets versions

$callbackLoadCache = function() use ( $cache ): ?array {
return $cache->getItem( $item )->get( 'assets-versions' );
};

$callbackSaveCache = function( ?array $assetVersionCollection ) use ( $cache ) {
$cacheItem = $cache->getItem( 'assets-versions' );
$cacheItem->set( $assetVersionCollection );
$cacheItem->expiresAfter( 30 * 86400 ); // 30 days
$cache->save( $cacheItem );
};
use Phant\Cache\SimpleCache;

$assetsVersions = new AssetsVersions(
'public/', // path to be processed
[ 'css', 'js' ], // extensions to be processed
[ 'node_modules/' ], // path to be ignored in path to be processed
$callbackLoadCache, //callback load cache
$callbackSaveCache //callback save cache
new SimpleCache( '/my-cache-path/', 'my-app-cache', 30 * 86400 ) // cache adapter
);
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,32 @@

namespace Phant\AssetsVersions;

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

final class AssetsVersions
class AssetsVersions
{
private string $pathToBeProcessed;
private array $extensionsToBeProcessed;
private ?array $pathsToBeIgnored;
protected $callbackLoadCache;
protected $callbackSaveCache;
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, ?callable $callbackLoadCache = null, ?callable $callbackSaveCache = null )
public function __construct(
string $pathToBeProcessed,
array $extensionsToBeProcessed = [ 'css', 'js' ],
?array $pathsToBeIgnored = null,
?CacheInterface $cacheAdapter = null
)
{
if ( !is_dir( $pathToBeProcessed ) ) {
throw new PathToBeProcessedDoesNotExist;
Expand All @@ -30,8 +37,7 @@ public function __construct( string $pathToBeProcessed, array $extensionsToBePro
$this->pathToBeProcessed = $pathToBeProcessed;
$this->extensionsToBeProcessed = array_map( 'strtolower', $extensionsToBeProcessed );
$this->pathsToBeIgnored = $pathsToBeIgnored;
$this->callbackLoadCache = $callbackLoadCache;
$this->callbackSaveCache = $callbackSaveCache;
$this->cacheAdapter = $cacheAdapter;

$this->assetVersionCollection = new AssetVersionCollection();

Expand All @@ -57,7 +63,7 @@ public function of( string $assetPath ): string
return $assetPath;
}

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

Expand All @@ -69,24 +75,24 @@ public function generate()
$this->saveToCache();
}

private function loadFromCache()
private function loadFromCache(): void
{
if ( is_callable( $this->callbackLoadCache ) ) {
$assetVersionCollection = ( $this->callbackLoadCache )();

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

$this->assetVersionCollection = $assetVersionCollection;
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()
private function saveToCache(): void
{
if ( is_callable( $this->callbackSaveCache ) ) {
( $this->callbackSaveCache )( $this->assetVersionCollection );
}
if (!$this->cacheAdapter) return;

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

private function getFromPath( string $assetPath ): ?AssetVersion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace Phant\AssetsVersions\ValueObject;
namespace Phant\AssetsVersions\DataStructure;

final class AssetVersion
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
declare(strict_types=1);

namespace Phant\AssetsVersions\ValueObject;
namespace Phant\AssetsVersions\DataStructure;

use Phant\AssetsVersions\DataStructure\AssetVersion;

final class AssetVersionCollection
{
Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@
}
],
"require": {
"php": "^8.0"
"php": ">=8.0",
"phant/cache": "1.*",
"psr/simple-cache": "^3.0"
},
"require-dev": {
"phpstan/phpstan": "^1.4"
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse component --memory-limit=4G"
},
"autoload": {
"psr-4": {
"Phant\\AssetsVersions\\": "src/AssetsVersions/"
"Phant\\AssetsVersions\\": "component/"
}
}
}

0 comments on commit 1fc8e76

Please sign in to comment.