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

add phpdoc #35

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
53 changes: 50 additions & 3 deletions src/Client/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public function __construct(protected ?\Redis $redis = null)
$this->redis = $redis ?? new \Redis(array_key_exists('REDIS_HOST', $_SERVER) ? ['host' => $_SERVER['REDIS_HOST']] : null);
}

/**
* @inheritdoc
*/
public function createPersistentConnection(?string $host = null, ?int $port = null, ?int $timeout = 0): void
{
$this->redis->pconnect(
Expand All @@ -25,13 +28,19 @@ public function createPersistentConnection(?string $host = null, ?int $port = nu
);
}

/**
* @inheritdoc
*/
public function hMSet(string $key, array $data): void
{
if (!$this->redis->hMSet(RedisClient::convertPrefix($key), $data)) {
$this->handleError(__METHOD__, $this->redis->getLastError());
}
}

/**
* @inheritdoc
*/
public function hget(string $key, string $property): string
{
$result = $this->redis->hget(RedisClient::convertPrefix($key), $property);
Expand All @@ -43,6 +52,9 @@ public function hget(string $key, string $property): string
return $result;
}

/**
* @inheritdoc
*/
public function hGetAll(string $key): array
{
$result = $this->redis->hGetAll(RedisClient::convertPrefix($key));
Expand All @@ -54,13 +66,19 @@ public function hGetAll(string $key): array
return $result;
}

/**
* @inheritdoc
*/
public function del(string $key): void
{
if (!$this->redis->del(RedisClient::convertPrefix($key))) {
$this->handleError(__METHOD__, $this->redis->getLastError());
}
}

/**
* @inheritdoc
*/
public function jsonGet(string $key): ?string
{
$result = $this->redis->rawCommand(RedisCommands::JSON_GET->value, static::convertPrefix($key));
Expand All @@ -72,6 +90,9 @@ public function jsonGet(string $key): ?string
return $result;
}

/**
* @inheritdoc
*/
public function jsonGetProperty(string $key, string $property): ?string
{
$result = $this->redis->rawCommand(RedisCommands::JSON_GET->value, static::convertPrefix($key), "$.$property");
Expand All @@ -83,6 +104,9 @@ public function jsonGetProperty(string $key, string $property): ?string
return $result;
}

/**
* @inheritdoc
*/
public function jsonSet(string $key, ?string $path = '$', ?string $value = '{}'): void
{
if (!$this->redis->rawCommand(RedisCommands::JSON_SET->value, static::convertPrefix($key), $path, $value)) {
Expand All @@ -91,8 +115,7 @@ public function jsonSet(string $key, ?string $path = '$', ?string $value = '{}')
}

/**
* Should provide a set of parameters systematically composed of a key / path / value for each JSON object to be persisted
* @param mixed ...$params
* @inheritdoc
*/
public function jsonMSet(...$params): void
{
Expand All @@ -114,6 +137,9 @@ public function jsonMSet(...$params): void
}
}

/**
* @inheritdoc
*/
public function jsonDel(string $key, ?string $path = '$'): void
{
if (!$this->redis->rawCommand(RedisCommands::JSON_DELETE->value, static::convertPrefix($key), $path)) {
Expand All @@ -122,7 +148,7 @@ public function jsonDel(string $key, ?string $path = '$'): void
}

/**
* @param \ReflectionProperty[] $properties
* @inheritdoc
*/
public function createIndex(string $prefixKey, ?string $format = 'HASH', ?array $properties = []): void
{
Expand Down Expand Up @@ -173,6 +199,9 @@ public function createIndex(string $prefixKey, ?string $format = 'HASH', ?array
}
}

/**
* @inheritdoc
*/
public function dropIndex(string $prefixKey): bool
{
try {
Expand All @@ -185,6 +214,9 @@ public function dropIndex(string $prefixKey): bool
return true;
}

/**
* @inheritdoc
*/
public function count(string $prefixKey, array $criterias = []): int
{
$arguments = [RedisCommands::SEARCH->value, static::convertPrefix($prefixKey)];
Expand All @@ -202,6 +234,9 @@ public function count(string $prefixKey, array $criterias = []): int
return (int) $rawResult[0];
}

/**
* @inheritdoc
*/
public function scanKeys(string $prefixKey): array
{
$keys = [];
Expand All @@ -216,18 +251,27 @@ public function scanKeys(string $prefixKey): array
return $keys;
}

/**
* @inheritdoc
*/
public function flushAll(): void
{
if (!$this->redis->flushAll()) {
$this->handleError(__METHOD__, $this->redis->getLastError());
}
}

/**
* @inheritdoc
*/
public function keys(string $pattern): array
{
return $this->redis->keys($pattern);
}

/**
* @inheritdoc
*/
public function search(string $prefixKey, array $search, array $orderBy, ?string $format = RedisFormat::HASH->value, ?int $numberOfResults = null): array
{
$arguments = [RedisCommands::SEARCH->value, self::convertPrefix($prefixKey)];
Expand Down Expand Up @@ -266,6 +310,9 @@ public function search(string $prefixKey, array $search, array $orderBy, ?string
return $this->extractRedisData($result, $format, $numberOfResults);
}

/**
* @inheritdoc
*/
public function searchLike(string $prefixKey, string $search, ?string $format = RedisFormat::HASH->value, ?int $numberOfResults = null): array
{
$arguments = [RedisCommands::SEARCH->value, static::convertPrefix($prefixKey)];
Expand Down
68 changes: 68 additions & 0 deletions src/Client/RedisClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,89 @@

interface RedisClientInterface
{
/**
* Create a persistent connection to the Redis server.
*/
public function createPersistentConnection(?string $host = null, ?int $port = null, ?int $timeout = 0): void;

/**
* Persist multiple hash objects to the Redis datastore.
*/
public function hMSet(string $key, array $data): void;

/**
* Get all properties of a hash object from the Redis datastore by given key.
*/
public function hGetAll(string $key): array;

/**
* Get a specific property of a hash object from the Redis datastore by given key.
*/
public function hget(string $key, string $property): string;

/**
* Remove an entry from the Redis datastore by given key.
*/
public function del(string $key): void;

/**
* Get a JSON object from the Redis datastore by given key.
*/
public function jsonGet(string $key): ?string;

/**
* Get a specific property of a JSON object from the Redis datastore by given key.
*/
public function jsonGetProperty(string $key, string $property): ?string;

/**
* Set a JSON object to the Redis datastore by given key.
*/
public function jsonSet(string $key, ?string $path = '$', ?string $value = '{}'): void;

/**
* Set multiple JSON objects to the Redis datastore.
*/
public function jsonMSet(...$params): void;

/**
* Remove a JSON object from the Redis datastore by given key.
* Should provide a set of parameters systematically composed of a key / path / value for each JSON object to be persisted
*/
public function jsonDel(string $key, ?string $path = '$'): void;

/**
* Create index for objects by properties.
*/
public function createIndex(string $prefixKey, ?string $format = 'HASH', ?array $properties = []): void;

/**
* Remove all index for given prefix key.
*/
public function dropIndex(string $prefixKey): bool;

/**
* Count all objects by given prefix key and criterias.
*/
public function count(string $prefixKey, array $criterias = []): int;

/**
* Search objects by given prefix key and criterias.
*/
public function search(string $prefixKey, array $search, array $orderBy, ?string $format = RedisFormat::HASH->value, ?int $numberOfResults = null): array;

/**
* Search objects by given prefix key and full text.
*/
public function searchLike(string $prefixKey, string $search, ?string $format = RedisFormat::HASH->value, ?int $numberOfResults = null): array;

/**
* Retrieve all keys by given prefix key, use * for all keys (do not use in production).
*/
public function scanKeys(string $prefixKey): array;

/**
* Remove all keys from the Redis datastore. Do not use in production.
*/
public function flushAll(): void;
}
30 changes: 30 additions & 0 deletions src/Om/RedisObjectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public function remove(object $object): void
$this->objectsToFlush[$objectToRemove->persisterClass][$objectToRemove->operation][$objectToRemove->redisKey] = $objectToRemove;
}

/**
* @inheritdoc
*/
public function flush(): void
{
foreach ($this->objectsToFlush as $persisterClassName => $objectsByOperation) {
Expand All @@ -64,18 +67,27 @@ public function flush(): void
}
}

/**
* @inheritdoc
*/
public function find(string $className, $id): ?object
{
$objectMapper = $this->getEntityMapper($className);

return $objectMapper->repository->find((string) $id);
}

/**
* @inheritdoc
*/
public function clear(): void
{
$this->objectsToFlush = [];
}

/**
* @inheritdoc
*/
public function detach(object $object): void
{
$identifier = $this->keyGenerator->getIdentifier(new \ReflectionClass($object));
Expand All @@ -92,6 +104,9 @@ public function detach(object $object): void
}
}

/**
* @inheritdoc
*/
public function refresh(object $object): object
{
$objectMapper = $this->getEntityMapper($object);
Expand All @@ -100,28 +115,43 @@ public function refresh(object $object): object
return $objectMapper->repository->find($objectMapper->prefix.':'.$object->{$identifierProperty->getName()});
}

/**
* @inheritdoc
*/
public function getRepository(string $className): RepositoryInterface
{
$objectMapper = $this->getEntityMapper($className);

return $objectMapper->repository;
}

/**
* @inheritdoc
*/
public function getClassMetadata(string $className): ClassMetadata
{
return (new MetadataFactory())->createClassMetadata($className);
}

/**
* @inheritdoc
*/
public function getMetadataFactory()
{
return new MetadataFactory();
}

/**
* @inheritdoc
*/
public function initializeObject(object $obj)
{
return new $obj();
}

/**
* @inheritdoc
*/
public function contains(object $object): bool
{
$identifier = $this->keyGenerator->getIdentifier(new \ReflectionClass($object));
Expand Down
Loading