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

[#16606] - feat: added events and events manager to storage and cache classes #16607

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ autom4te.cache/
.zephir/
.temp/

# JetBrains IDE
*.iml
.idea/

# Zephir compiler
zephir.phar
zephir-*.phar
Expand All @@ -75,3 +79,6 @@ tests/_output/*
.php_cs.cache
.env
zephir

# Docker
docker-compose.override.yml
14 changes: 14 additions & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
- Changed the `Phalcon\Mvc\Router\Group::getHostname()` to return `null` also. [#16601](https://github.com/phalcon/cphalcon/issues/16601)

### Added

- Added events and `Phalcon\Events\Manager` for `Phalcon\Storage\Adapter\Apcu`,
`Phalcon\Storage\Adapter\Redis`,
`Phalcon\Storage\Adapter\Memory`,
`Phalcon\Storage\Adapter\Libmemcached`,
`Phalcon\Storage\Adapter\Stream`,
`Phalcon\Storage\Adapter\Weak`,
`Phalcon\Cache\Adapter\Apcu`,
`Phalcon\Cache\Adapter\Redis`,
`Phalcon\Cache\Adapter\Memory`,
`Phalcon\Cache\Adapter\Libmemcached`,
`Phalcon\Cache\Adapter\Stream`,
`Phalcon\Cache\Adapter\Weak`
`Phalcon\Cache\AbstractCache`. [#16606](https://github.com/phalcon/cphalcon/issues/16606)

### Fixed

Expand Down
21 changes: 18 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,23 @@
"test-db-mysql": "php -d extension=ext/modules/phalcon.so ./vendor/bin/codecept run --ext DotReporter database --env mysql -g mysql",
"test-db-pgsql": "php -d extension=ext/modules/phalcon.so ./vendor/bin/codecept run --ext DotReporter database --env pgsql -g pgsql",
"test-db-sqlite": "php -d extension=ext/modules/phalcon.so ./vendor/bin/codecept run --ext DotReporter database --env sqlite -g sqlite",
"test-db": "composer test-db-common && composer test-db-mysql && composer test-db-sqlite",
"test-all": "composer test-unit && composer test-cli && composer test-integration && composer test-db",
"analyze": "php -d extension=ext/modules/phalcon.so ./vendor/bin/psalm"
"test-db": [
"@test-db-common",
"@test-db-mysql",
"@test-db-sqlite"
],
"test-all": [
"@test-unit",
"@test-cli",
"@test-integration",
"@test-db"
],
"analyze": "php -d extension=ext/modules/phalcon.so ./vendor/bin/psalm --no-cache",
"clean-build-stubs": [
"./vendor/bin/zephir fullclean",
"Composer\\Config::disableProcessTimeout",
"./vendor/bin/zephir build",
"./vendor/bin/zephir stubs"
]
}
}
93 changes: 88 additions & 5 deletions phalcon/Cache/AbstractCache.zep
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ namespace Phalcon\Cache;
use DateInterval;
use Phalcon\Cache\Adapter\AdapterInterface;
use Phalcon\Cache\Exception\InvalidArgumentException;
use Phalcon\Events\EventsAwareInterface;
use Phalcon\Events\ManagerInterface;
use Traversable;

/**
* This component offers caching capabilities for your application.
*/
abstract class AbstractCache implements CacheInterface
abstract class AbstractCache implements CacheInterface, EventsAwareInterface
{
/**
* The adapter
Expand All @@ -27,6 +29,13 @@ abstract class AbstractCache implements CacheInterface
*/
protected adapter;

/**
* Event Manager
*
* @var ManagerInterface|null
*/
protected eventsManager = null;

/**
* Constructor.
*
Expand All @@ -47,6 +56,22 @@ abstract class AbstractCache implements CacheInterface
return this->adapter;
}

/**
* Sets the event manager
*/
public function setEventsManager(<ManagerInterface> eventsManager) -> void
{
let this->eventsManager = eventsManager;
}

/**
* Get the event manager
*/
public function getEventsManager() -> <ManagerInterface> | null
{
return this->eventsManager;
}

/**
* Checks the key. If it contains invalid characters an exception is thrown
*
Expand Down Expand Up @@ -108,9 +133,17 @@ abstract class AbstractCache implements CacheInterface
*/
protected function doDelete(string key) -> bool
{
var result;

this->fire("cache:beforeDelete");
niden marked this conversation as resolved.
Show resolved Hide resolved

this->checkKey(key);

return this->adapter->delete(key);
let result = this->adapter->delete(key);

this->fire("cache:afterDelete");

return result;
}

/**
Expand All @@ -122,13 +155,17 @@ abstract class AbstractCache implements CacheInterface

this->checkKeys(keys);

this->fire("cache:beforeDeleteMultiple");
niden marked this conversation as resolved.
Show resolved Hide resolved

let result = true;
for key in keys {
if (true !== this->adapter->delete(key)) {
let result = false;
}
}

this->fire("cache:afterDeleteMultiple");

return result;
}

Expand All @@ -146,9 +183,17 @@ abstract class AbstractCache implements CacheInterface
*/
protected function doGet(string key, var defaultValue = null) -> var
{
var result;

this->checkKey(key);

return this->adapter->get(key, defaultValue);
this->fire("cache:beforeGet");

let result = this->adapter->get(key, defaultValue);

this->fire("cache:afterGet");

return result;
}

/**
Expand All @@ -160,11 +205,15 @@ abstract class AbstractCache implements CacheInterface

this->checkKeys(keys);

this->fire("cache:beforeGetMultiple");

let results = [];
for element in keys {
let results[element] = this->get(element, defaultValue);
}

this->fire("cache:afterGetMultiple");

return results;
}

Expand All @@ -180,9 +229,17 @@ abstract class AbstractCache implements CacheInterface
*/
protected function doHas(string key) -> bool
{
var result;

this->checkKey(key);

return this->adapter->has(key);
this->fire("cache:beforeHas");

let result = this->adapter->has(key);

this->fire("cache:afterHas");

return result;
}

/**
Expand All @@ -205,9 +262,17 @@ abstract class AbstractCache implements CacheInterface
*/
protected function doSet(string key, var value, var ttl = null) -> bool
{
var result;

this->checkKey(key);

return this->adapter->set(key, value, ttl);
this->fire("cache:beforeSet");

let result = this->adapter->set(key, value, ttl);

this->fire("cache:afterSet");

return result;
}

/**
Expand All @@ -219,16 +284,34 @@ abstract class AbstractCache implements CacheInterface

this->checkKeys(values);

this->fire("cache:beforeSetMultiple");

let result = true;
for key, value in values {
if (true !== this->set(key, value, ttl)) {
let result = false;
}
}

this->fire("cache:afterSetMultiple");

return result;
}

/**
* Trigger an event for the eventsManager.
*
* @var string $eventName
*/
protected function fire(string eventName) -> void
{
if (this->eventsManager === null) {
return;
}

this->eventsManager->fire(eventName, this);
}

/**
* Returns the exception class that will be used for exceptions thrown
*
Expand Down
1 change: 1 addition & 0 deletions phalcon/Cache/Adapter/Apcu.zep
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Apcu as StorageApcu;
*/
class Apcu extends StorageApcu implements CacheAdapterInterface
{
protected eventType = "cache";
}
1 change: 1 addition & 0 deletions phalcon/Cache/Adapter/Libmemcached.zep
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Libmemcached as StorageLibmemcached;
*/
class Libmemcached extends StorageLibmemcached implements CacheAdapterInterface
{
protected eventType = "cache";
}
1 change: 1 addition & 0 deletions phalcon/Cache/Adapter/Memory.zep
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Memory as StorageMemory;
*/
class Memory extends StorageMemory implements CacheAdapterInterface
{
protected eventType = "cache";
}
1 change: 1 addition & 0 deletions phalcon/Cache/Adapter/Redis.zep
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Redis as StorageRedis;
*/
class Redis extends StorageRedis implements CacheAdapterInterface
{
protected eventType = "cache";
}
1 change: 1 addition & 0 deletions phalcon/Cache/Adapter/Stream.zep
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Stream as StorageStream;
*/
class Stream extends StorageStream implements CacheAdapterInterface
{
protected eventType = "cache";
}
1 change: 1 addition & 0 deletions phalcon/Cache/Adapter/Weak.zep
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Weak as StorageWeak;
*/
class Weak extends StorageWeak implements CacheAdapterInterface
{
protected eventType = "cache";
}
60 changes: 57 additions & 3 deletions phalcon/Storage/Adapter/AbstractAdapter.zep
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Phalcon\Storage\Adapter;
use DateInterval;
use DateTime;
use Exception;
use Phalcon\Events\EventsAwareInterface;
use Phalcon\Events\ManagerInterface;
use Phalcon\Storage\Serializer\SerializerInterface;
use Phalcon\Storage\SerializerFactory;
use Phalcon\Support\Exception as SupportException;
Expand All @@ -30,7 +32,7 @@ use Phalcon\Support\Exception as SupportException;
* @property SerializerInterface $serializer
* @property SerializerFactory $serializerFactory
*/
abstract class AbstractAdapter implements AdapterInterface
abstract class AbstractAdapter implements AdapterInterface, EventsAwareInterface
{
/**
* @var mixed
Expand Down Expand Up @@ -75,6 +77,20 @@ abstract class AbstractAdapter implements AdapterInterface
*/
protected serializerFactory;

/**
* Event Manager
*
* @var ManagerInterface|null
*/
protected eventsManager = null;

/**
* EventType prefix.
*
* @var string
*/
protected eventType = "storage";

/**
* AbstractAdapter constructor.
*
Expand Down Expand Up @@ -143,15 +159,23 @@ abstract class AbstractAdapter implements AdapterInterface
*/
public function get(string key, defaultValue = null) -> var
{
var content;
var content, result;

this->fire(this->eventType . ":beforeGet");

if (true !== this->has(key)) {
this->fire(this->eventType . ":afterGet");

return defaultValue;
}

let content = this->doGet(key);

return this->getUnserializedData(content, defaultValue);
let result = this->getUnserializedData(content, defaultValue);

this->fire(this->eventType . ":afterGet");

return result;
}

/**
Expand Down Expand Up @@ -384,4 +408,34 @@ abstract class AbstractAdapter implements AdapterInterface

return value;
}

/**
* Sets the event manager
*/
public function setEventsManager(<ManagerInterface> eventsManager) -> void
{
let this->eventsManager = eventsManager;
}

/**
* Get the event manager
*/
public function getEventsManager() -> <ManagerInterface> | null
{
return this->eventsManager;
}

/**
* Trigger an event for the eventsManager.
*
* @var string $eventName
*/
protected function fire(string eventName) -> void
{
if (this->eventsManager === null) {
return;
}

this->eventsManager->fire(eventName, this);
}
}
Loading
Loading