Skip to content

Commit

Permalink
Version 4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar authored Dec 21, 2021
1 parent 310c035 commit 76f1e56
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 204 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: '8.1'
coverage: none
tools: php-cs-fixer

Expand All @@ -35,7 +35,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [7.2, 7.3, 7.4, 8.0]
php: ['8.0', '8.1']
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -61,7 +61,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [7.2, 7.3, 7.4, 8.0]
php: ['8.0', '8.1']
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/vendor/
.php_cs.cache
.php-cs-fixer.cache
.phpunit.result.cache
composer.lock
49 changes: 49 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

$date = date('Y');

$header = <<<EOF
This file is part of the toflar/psr6-symfony-http-cache-store package.
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
@copyright Yanick Witschi <yanick.witschi@terminal42.ch>
EOF;

$finder = PhpCsFixer\Finder::create()
->in([__DIR__.'/src', __DIR__.'/tests'])
;

$config = new PhpCsFixer\Config();
$config
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'combine_consecutive_unsets' => true,
'declare_strict_types' => true,
'general_phpdoc_annotation_remove' => true,
'header_comment' => ['header' => $header],
'heredoc_to_nowdoc' => true,
'no_extra_blank_lines' => true,
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'no_superfluous_phpdoc_tags' => true,
'ordered_class_elements' => true,
'ordered_imports' => true,
'php_unit_strict' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_order' => true,
'psr_autoloading' => true,
'strict_comparison' => true,
'strict_param' => true,
'native_function_invocation' => ['include' => ['@compiler_optimized']],
'void_return' => true,
])
->setFinder($finder)
;

return $config;
49 changes: 0 additions & 49 deletions .php_cs.dist

This file was deleted.

23 changes: 0 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,6 @@ $ composer require toflar/psr6-symfony-http-cache-store

## Configuration

For the Symfony 3 standard edition file structure, use the `Psr6Store` by
enabling it in your `AppCache` as follows:

```php
<?php

// app/AppCache.php

/**
* Overwrite constructor to register the Psr6Store.
*/
public function __construct(
HttpKernelInterface $kernel,
SurrogateInterface $surrogate = null,
array $options = []
) {
$store = new Psr6Store(['cache_directory' => $kernel->getCacheDir()]);

parent::__construct($kernel, $store, $surrogate, $options);
}

```

For the Symfony 4/Flex structure, you need to adjust your `index.php` like this:

```php
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
}
],
"require": {
"php": "^7.2 | ^8.0",
"symfony/lock": "^4.4 | ^5.0",
"symfony/cache": "^4.4 | ^5.0",
"symfony/http-foundation": "^4.4 | ^5.0",
"symfony/http-kernel": "^4.4 | ^5.0",
"symfony/options-resolver": "^4.4 | ^5.0"
"php": "^8.0",
"symfony/lock": "^6.0",
"symfony/cache": "^6.0",
"symfony/http-foundation": "^6.0",
"symfony/http-kernel": "^6.0",
"symfony/options-resolver": "^6.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^5.0"
"symfony/phpunit-bridge": "^6.0"
},
"autoload": {
"psr-4": {
Expand Down
99 changes: 15 additions & 84 deletions src/Psr6Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

namespace Toflar\Psr6HttpCacheStore;

use Psr\Cache\CacheItemInterface;
use Psr\Cache\InvalidArgumentException as CacheInvalidArgumentException;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\FilesystemTagAwareAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
Expand All @@ -44,29 +44,18 @@
*/
class Psr6Store implements Psr6StoreInterface, ClearableInterface
{
const NON_VARYING_KEY = 'non-varying';
const COUNTER_KEY = 'write-operations-counter';
const CLEANUP_LOCK_KEY = 'cleanup-lock';
public const NON_VARYING_KEY = 'non-varying';
public const COUNTER_KEY = 'write-operations-counter';
public const CLEANUP_LOCK_KEY = 'cleanup-lock';

/**
* @var array
*/
private $options;

/**
* @var TagAwareAdapterInterface
*/
private $cache;

/**
* @var LockFactory
*/
private $lockFactory;
private array $options;
private AdapterInterface $cache;
private LockFactory $lockFactory;

/**
* @var LockInterface[]
*/
private $locks = [];
private array $locks = [];

/**
* When creating a Psr6Store you can configure a number options.
Expand Down Expand Up @@ -111,17 +100,11 @@ public function __construct(array $options = [])
$this->lockFactory = $this->options['lock_factory'];
}

/**
* Locates a cached Response for the Request provided.
*
* @param Request $request A Request instance
*
* @return Response|null A Response instance, or null if no cache entry was found
*/
public function lookup(Request $request): ?Response
{
$cacheKey = $this->getCacheKey($request);

/** @var CacheItem $item */
$item = $this->cache->getItem($cacheKey);

if (!$item->isHit()) {
Expand Down Expand Up @@ -150,17 +133,6 @@ public function lookup(Request $request): ?Response
return null;
}

/**
* Writes a cache entry to the store for the given Request and Response.
*
* Existing entries are read and any that match the response are removed. This
* method calls write with the new list of cache entries.
*
* @param Request $request A Request instance
* @param Response $response A Response instance
*
* @return string The key under which the response is stored
*/
public function write(Request $request, Response $response): string
{
if (null === $response->getMaxAge()) {
Expand All @@ -174,6 +146,7 @@ public function write(Request $request, Response $response): string
$headers = $response->headers->all();
unset($headers['age']);

/** @var CacheItem $item */
$item = $this->cache->getItem($cacheKey);

if (!$item->isHit()) {
Expand Down Expand Up @@ -220,26 +193,14 @@ public function write(Request $request, Response $response): string
return $cacheKey;
}

/**
* Invalidates all cache entries that match the request.
*
* @param Request $request A Request instance
*/
public function invalidate(Request $request): void
{
$cacheKey = $this->getCacheKey($request);

$this->cache->deleteItem($cacheKey);
}

/**
* Locks the cache for a given Request.
*
* @param Request $request A Request instance
*
* @return bool|string true if the lock is acquired, the path to the current lock otherwise
*/
public function lock(Request $request)
public function lock(Request $request): bool|string
{
$cacheKey = $this->getCacheKey($request);

Expand All @@ -253,13 +214,6 @@ public function lock(Request $request)
return $this->locks[$cacheKey]->acquire();
}

/**
* Releases the lock for the given Request.
*
* @param Request $request A Request instance
*
* @return bool False if the lock file does not exist or cannot be unlocked, true otherwise
*/
public function unlock(Request $request): bool
{
$cacheKey = $this->getCacheKey($request);
Expand All @@ -279,13 +233,6 @@ public function unlock(Request $request): bool
return true;
}

/**
* Returns whether or not a lock exists.
*
* @param Request $request A Request instance
*
* @return bool true if lock exists, false otherwise
*/
public function isLocked(Request $request): bool
{
$cacheKey = $this->getCacheKey($request);
Expand All @@ -297,25 +244,13 @@ public function isLocked(Request $request): bool
return $this->locks[$cacheKey]->isAcquired();
}

/**
* Purges data for the given URL.
*
* @param string $url A URL
*
* @return bool true if the URL exists and has been purged, false otherwise
*/
public function purge($url): bool
public function purge(string $url): bool
{
$cacheKey = $this->getCacheKey(Request::create($url));

return $this->cache->deleteItem($cacheKey);
}

/**
* Release all locks.
*
* {@inheritdoc}
*/
public function cleanup(): void
{
try {
Expand Down Expand Up @@ -485,7 +420,7 @@ private function saveContentDigest(Response $response): void

// Make sure the content-length header is present
if (!$response->headers->has('Transfer-Encoding')) {
$response->headers->set('Content-Length', \strlen((string) $response->getContent()));
$response->headers->set('Content-Length', (string) \strlen((string) $response->getContent()));
}
}

Expand Down Expand Up @@ -526,16 +461,12 @@ private function autoPruneExpiredEntries(): void
$this->cache->saveDeferred($item);
}

/**
* @param int $expiresAfter
* @param array $tags
*/
private function saveDeferred(CacheItemInterface $item, $data, $expiresAfter = null, $tags = []): bool
private function saveDeferred(CacheItem $item, $data, ?int $expiresAfter = null, array $tags = []): bool
{
$item->set($data);
$item->expiresAfter($expiresAfter);

if (0 !== \count($tags) && method_exists($item, 'tag')) {
if (0 !== \count($tags)) {
$item->tag($tags);
}

Expand Down
Loading

0 comments on commit 76f1e56

Please sign in to comment.