Skip to content

Commit ec262c3

Browse files
committed
- Added \Phpfastcache\Event\Event class for centralizing event name with reusable constants
- `\Psr\Cache\CacheItemInterface::set` will also no longer accepts resource object anymore as method unique parameter
1 parent e697602 commit ec262c3

File tree

12 files changed

+81
-100
lines changed

12 files changed

+81
-100
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 9.0.1
2+
##### 07 november 2021
3+
- __Core__
4+
- Added `\Phpfastcache\Event\Event` class for centralizing event name with reusable constants.
5+
- __Item__
6+
- `\Psr\Cache\CacheItemInterface::set` will also no longer accepts resource object anymore as method unique parameter
17
## 9.0.0
28
##### 31 october 2021
39
- __Migration guide__

docs/migration/MigratingFromV8ToV9.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Because the V9 is **relatively** not backward compatible with the V8, here's a g
44
As of the V9 the mandatory minimum php version has been increased to 8.0+.
55
Once released, the php version 8.1 will be unit-tested
66

7-
### Embedded autoload has been removed (and therefore, embedded dependencies)
7+
### Embedded autoload has been removed (and therefore, embedded dependencies too)
88
Use [Composer](https://getcomposer.org/doc/03-cli.md#require) to include Phpfastcache in your project
99

1010
### Removed magics methods from CacheManager `CacheManager::DriverName()`
@@ -24,9 +24,9 @@ Finally, the config name you try to set MUST be recognized or an exception will
2424
Use `\Phpfastcache\CacheContract` instead. See [Wiki](https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV9%CB%96%5D-Cache-contract).
2525

2626
### Removed `Couchbase` driver (SDK 2 support dropped)
27-
It is now replaced by `Couchbasev3` driver (SDK 3), the configuration options are all the same plus `scopeName` and `collectionName` that are now configurable.
27+
It is now replaced by `Couchbasev3` driver (SDK 3), the configuration options remains the same plus `scopeName` and `collectionName` that are now configurable.
2828

29-
### Updated EventManager instances
29+
### Updated EventManager callback parameters
3030
- Updated argument type #2 (`$items`) of `onCacheSaveMultipleItems()` event from `ExtendedCacheItemInterface[]` to `EventReferenceParameter($items)`
3131
- Updated argument type #2 (`$items`) of `onCacheCommitItem()` event from `ExtendedCacheItemInterface[]` to `EventReferenceParameter($items)`
3232
- Updated argument type #2 (`$value`) of `onCacheItemSet()` event from `mixed` to `EventReferenceParameter(mixed $value)`

lib/Phpfastcache/Cluster/ClusterAggregator.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Exception;
1919
use Phpfastcache\CacheManager;
2020
use Phpfastcache\Config\ConfigurationOption;
21+
use Phpfastcache\Event\Event;
2122
use Phpfastcache\EventManager;
2223
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
2324
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
@@ -149,12 +150,7 @@ public function getCluster(int $strategy = AggregatorInterface::STRATEGY_FULL_RE
149150
...\array_values($this->driverPools)
150151
);
151152

152-
/**
153-
* @eventName CacheClusterBuilt
154-
* @param $clusterAggregator AggregatorInterface
155-
* @param $cluster ClusterPoolInterface
156-
*/
157-
$this->cluster->getEventManager()->dispatch('CacheClusterBuilt', $this, $this->cluster);
153+
$this->cluster->getEventManager()->dispatch(Event::CACHE_CLUSTER_BUILT, $this, $this->cluster);
158154
}
159155
} else {
160156
throw new PhpfastcacheInvalidArgumentException('Unknown cluster strategy');

lib/Phpfastcache/Cluster/Drivers/MasterSlaveReplication/Driver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Phpfastcache\Cluster\ClusterPoolAbstract;
1919
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
2020
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
21+
use Phpfastcache\Event\Event;
2122
use Phpfastcache\EventManager;
2223
use Phpfastcache\Exceptions\PhpfastcacheCoreException;
2324
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
@@ -77,7 +78,7 @@ protected function makeOperation(callable $operation)
7778
} catch (PhpfastcacheExceptionInterface $e) {
7879
try {
7980
$this->eventManager->dispatch(
80-
'CacheReplicationSlaveFallback',
81+
Event::CACHE_REPLICATION_SLAVE_FALLBACK,
8182
$this,
8283
\debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']
8384
);

lib/Phpfastcache/Cluster/Drivers/RandomReplication/Driver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use Phpfastcache\Cluster\Drivers\MasterSlaveReplication\Driver as MasterSlaveReplicationDriver;
1919
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
20+
use Phpfastcache\Event\Event;
2021
use Phpfastcache\Event\EventManagerInterface;
2122
use ReflectionException;
2223
use ReflectionMethod;
@@ -36,7 +37,7 @@ public function __construct(string $clusterName, EventManagerInterface $em, Exte
3637
$randomPool = $driverPools[\random_int(0, \count($driverPools) - 1)];
3738

3839
$this->eventManager->dispatch(
39-
'CacheReplicationRandomPoolChosen',
40+
Event::CACHE_REPLICATION_RANDOM_POOL_CHOSEN,
4041
$this,
4142
$randomPool
4243
);

lib/Phpfastcache/Core/Item/CacheItemTrait.php

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use DateInterval;
1919
use DateTime;
2020
use DateTimeInterface;
21+
use Phpfastcache\Event\Event;
2122
use Phpfastcache\Event\EventManagerDispatcherTrait;
2223
use Phpfastcache\Event\EventReferenceParameter;
2324
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
@@ -63,13 +64,11 @@ public function set(mixed $value): static
6364
throw new PhpfastcacheInvalidArgumentException('The value set cannot be an instance of \\Closure.');
6465
}
6566

66-
/**
67-
* @eventName CacheSaveDeferredItem
68-
* @param ExtendedCacheItemInterface $this
69-
* @param mixed $value
70-
*
71-
*/
72-
$this->eventManager->dispatch('CacheItemSet', $this, new EventReferenceParameter($value, true));
67+
if (\is_resource($value)) {
68+
throw new PhpfastcacheInvalidArgumentException('The value set cannot be a resource');
69+
}
70+
71+
$this->eventManager->dispatch(Event::CACHE_ITEM_SET, $this, new EventReferenceParameter($value, true));
7372

7473
$this->data = $value;
7574

@@ -101,12 +100,7 @@ public function setHit(bool $isHit): ExtendedCacheItemInterface
101100
public function expiresAt(?\DateTimeInterface $expiration): static
102101
{
103102
if ($expiration instanceof DateTimeInterface) {
104-
/**
105-
* @eventName CacheItemExpireAt
106-
* @param ExtendedCacheItemInterface $this
107-
* @param DateTimeInterface $expiration
108-
*/
109-
$this->eventManager->dispatch('CacheItemExpireAt', $this, $expiration);
103+
$this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AT, $this, $expiration);
110104
$this->expirationDate = $expiration;
111105
} else {
112106
throw new PhpfastcacheInvalidArgumentException('$expiration must be an object implementing the DateTimeInterface got: ' . \gettype($expiration));
@@ -131,21 +125,11 @@ public function expiresAfter(int|\DateInterval|null $time): static
131125
$time = 30 * 24 * 3600 * 5;
132126
}
133127

134-
/**
135-
* @eventName CacheItemExpireAt
136-
* @param ExtendedCacheItemInterface $this
137-
* @param DateTimeInterface $expiration
138-
*/
139-
$this->eventManager->dispatch('CacheItemExpireAfter', $this, $time);
128+
$this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AFTER, $this, $time);
140129

141130
$this->expirationDate = (new DateTime())->add(new DateInterval(\sprintf('PT%dS', $time)));
142131
} elseif ($time instanceof DateInterval) {
143-
/**
144-
* @eventName CacheItemExpireAt
145-
* @param ExtendedCacheItemInterface $this
146-
* @param DateTimeInterface $expiration
147-
*/
148-
$this->eventManager->dispatch('CacheItemExpireAfter', $this, $time);
132+
$this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AFTER, $this, $time);
149133

150134
$this->expirationDate = (new DateTime())->add($time);
151135
} else {

lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
2020
use Phpfastcache\Entities\DriverIO;
2121
use Phpfastcache\Entities\ItemBatch;
22+
use Phpfastcache\Event\Event;
2223
use Phpfastcache\Event\EventManagerDispatcherTrait;
2324
use Phpfastcache\Event\EventReferenceParameter;
2425
use Phpfastcache\Exceptions\PhpfastcacheCoreException;
@@ -151,18 +152,13 @@ public function getItem(string $key): ExtendedCacheItemInterface
151152
* The timeout has been reached
152153
* Consider that the batch has
153154
* failed and serve an empty item
154-
* to avoid to get stuck with a
155+
* to avoid get stuck with a
155156
* batch item stored in driver
156157
*/
157158
goto getItemDriverExpired;
158159
}
159-
/**
160-
* @eventName CacheGetItem
161-
* @param $this ExtendedCacheItemPoolInterface
162-
* @param $driverData ItemBatch
163-
* @param $cacheSlamsSpendSeconds int
164-
*/
165-
$this->eventManager->dispatch('CacheGetItemInSlamBatch', $this, $driverData, $cacheSlamsSpendSeconds);
160+
161+
$this->eventManager->dispatch(Event::CACHE_GET_ITEM_IN_SLAM_BATCH, $this, $driverData, $cacheSlamsSpendSeconds);
166162

167163
/**
168164
* Wait for a second before
@@ -231,12 +227,7 @@ public function getItem(string $key): ExtendedCacheItemInterface
231227

232228

233229
if ($item !== null) {
234-
/**
235-
* @eventName CacheGetItem
236-
* @param $this ExtendedCacheItemPoolInterface
237-
* @param $this ExtendedCacheItemInterface
238-
*/
239-
$this->eventManager->dispatch('CacheGetItem', $this, $item);
230+
$this->eventManager->dispatch(Event::CACHE_GET_ITEM, $this, $item);
240231

241232
$item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss();
242233

@@ -267,12 +258,7 @@ public function hasItem(string $key): bool
267258
*/
268259
public function clear(): bool
269260
{
270-
/**
271-
* @eventName CacheClearItem
272-
* @param $this ExtendedCacheItemPoolInterface
273-
* @param $itemInstances ExtendedCacheItemInterface[]
274-
*/
275-
$this->eventManager->dispatch('CacheClearItem', $this, $this->itemInstances);
261+
$this->eventManager->dispatch(Event::CACHE_CLEAR_ITEM, $this, $this->itemInstances);
276262

277263
$this->getIO()->incWriteHit();
278264
// Faster than detachAllItems()
@@ -317,12 +303,7 @@ public function deleteItem(string $key): bool
317303
$item->setHit(false);
318304
$this->getIO()->incWriteHit();
319305

320-
/**
321-
* @eventName CacheCommitItem
322-
* @param $this ExtendedCacheItemPoolInterface
323-
* @param $item ExtendedCacheItemInterface
324-
*/
325-
$this->eventManager->dispatch('CacheDeleteItem', $this, $item);
306+
$this->eventManager->dispatch(Event::CACHE_DELETE_ITEM, $this, $item);
326307

327308
/**
328309
* De-register the item instance
@@ -355,12 +336,7 @@ public function saveDeferred(CacheItemInterface $item): bool
355336
throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.');
356337
}
357338

358-
/**
359-
* @eventName CacheSaveDeferredItem
360-
* @param $this ExtendedCacheItemPoolInterface
361-
* @param $this ExtendedCacheItemInterface
362-
*/
363-
$this->eventManager->dispatch('CacheSaveDeferredItem', $this, $item);
339+
$this->eventManager->dispatch(Event::CACHE_SAVE_DEFERRED_ITEM, $this, $item);
364340
$this->deferredList[$item->getKey()] = $item;
365341

366342
return true;
@@ -372,16 +348,10 @@ public function saveDeferred(CacheItemInterface $item): bool
372348
* @throws PhpfastcacheDriverException
373349
* @throws PhpfastcacheInvalidArgumentException
374350
* @throws PhpfastcacheLogicException
375-
* @throws \ReflectionException
376351
*/
377352
public function commit(): bool
378353
{
379-
/**
380-
* @eventName CacheCommitItem
381-
* @param $this ExtendedCacheItemPoolInterface
382-
* @param $deferredList ExtendedCacheItemInterface[]
383-
*/
384-
$this->eventManager->dispatch('CacheCommitItem', $this, new EventReferenceParameter($this->deferredList));
354+
$this->eventManager->dispatch(Event::CACHE_COMMIT_ITEM, $this, new EventReferenceParameter($this->deferredList));
385355

386356
if (\count($this->deferredList)) {
387357
$return = true;
@@ -424,13 +394,7 @@ public function save(CacheItemInterface $item): bool
424394
throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.');
425395
}
426396

427-
/**
428-
* @eventName CacheSaveItem
429-
* @param $this ExtendedCacheItemPoolInterface
430-
* @param $this ExtendedCacheItemInterface
431-
*/
432-
$this->eventManager->dispatch('CacheSaveItem', $this, $item);
433-
397+
$this->eventManager->dispatch(Event::CACHE_SAVE_ITEM, $this, $item);
434398

435399
if ($this->getConfig()->isPreventCacheSlams()) {
436400
/**

lib/Phpfastcache/Core/Pool/ExtendedCacheItemPoolTrait.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace Phpfastcache\Core\Pool;
1717

1818
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
19+
use Phpfastcache\Event\Event;
1920
use Phpfastcache\Event\EventReferenceParameter;
2021
use Phpfastcache\Exceptions\PhpfastcacheCoreException;
2122
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
@@ -119,16 +120,10 @@ public function isAttached(CacheItemInterface $item): bool
119120
* @throws PhpfastcacheDriverException
120121
* @throws PhpfastcacheInvalidArgumentException
121122
* @throws PhpfastcacheLogicException
122-
* @throws \ReflectionException
123123
*/
124124
public function saveMultiple(ExtendedCacheItemInterface...$items): bool
125125
{
126-
/**
127-
* @eventName CacheSaveItem
128-
* @param $this ExtendedCacheItemPoolInterface
129-
* @param $this ExtendedCacheItemInterface
130-
*/
131-
$this->eventManager->dispatch('CacheSaveMultipleItems', $this, new EventReferenceParameter($items));
126+
$this->eventManager->dispatch(Event::CACHE_SAVE_MULTIPLE_ITEMS, $this, new EventReferenceParameter($items));
132127

133128
if (\count($items)) {
134129
foreach ($items as $item) {

lib/Phpfastcache/Core/Pool/IO/IOHelperTrait.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
1919
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
2020
use Phpfastcache\Entities\DriverStatistic;
21+
use Phpfastcache\Event\Event;
2122
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
2223
use Phpfastcache\Exceptions\PhpfastcacheIOException;
2324
use Phpfastcache\Util\Directory;
@@ -271,14 +272,7 @@ protected function readFile(string $file): string
271272
*/
272273
protected function writeFile(string $file, string $data, bool $secureFileManipulation = false): bool
273274
{
274-
/**
275-
* @eventName CacheWriteFileOnDisk
276-
* @param ExtendedCacheItemPoolInterface $this
277-
* @param string $file
278-
* @param bool $secureFileManipulation
279-
*
280-
*/
281-
$this->eventManager->dispatch('CacheWriteFileOnDisk', $this, $file, $secureFileManipulation);
275+
$this->eventManager->dispatch(Event::CACHE_WRITE_FILE_ON_DISK, $this, $file, $secureFileManipulation);
282276

283277
if ($secureFileManipulation) {
284278
$tmpFilename = Directory::getAbsolutePath(

lib/Phpfastcache/Drivers/Arangodb/Driver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
3131
use Phpfastcache\Core\Pool\TaggableCacheItemPoolTrait;
3232
use Phpfastcache\Entities\DriverStatistic;
33+
use Phpfastcache\Event\Event;
3334
use Phpfastcache\Event\EventReferenceParameter;
3435
use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException;
3536
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
@@ -101,7 +102,7 @@ protected function driverConnect(): bool
101102
$connectionOptions[ArangoConnectionOptions::OPTION_CIPHERS] = $this->getConfig()->getCiphers();
102103
}
103104

104-
$this->eventManager->dispatch('ArangodbConnection', $this, new EventReferenceParameter($connectionOptions));
105+
$this->eventManager->dispatch(Event::ARANGODB_CONNECTION, $this, new EventReferenceParameter($connectionOptions));
105106

106107
$this->instance = new ArangoConnection($connectionOptions);
107108
$this->documentHandler = new ArangoDocumentHandler($this->instance);
@@ -214,7 +215,7 @@ protected function createCollection($collectionName): bool
214215
'waitForSync' => false
215216
];
216217

217-
$this->eventManager->dispatch('ArangodbCollectionParams', $this, new EventReferenceParameter($params));
218+
$this->eventManager->dispatch(Event::ARANGODB_COLLECTION_PARAMS, $this, new EventReferenceParameter($params));
218219

219220
$this->collectionHandler->create($collection, $params);
220221

0 commit comments

Comments
 (0)