Skip to content

Commit

Permalink
PHP 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammetsafak committed Dec 16, 2023
1 parent bd579d4 commit dce9624
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

## Requirements

- PHP 7.4 or later
- PHP 8.0 or later

**Note :** Adapters used may have different dependencies.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
],
"minimum-stability": "stable",
"require": {
"php": ">=7.4"
"php": ">=8.0"
}
}
14 changes: 7 additions & 7 deletions src/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,41 @@ abstract class AbstractAdapter implements Interfaces\AdapterInterface
/**
* @inheritDoc
*/
public function close()
public function close(): bool
{
return true;
}

/**
* @inheritDoc
*/
abstract public function destroy($id);
abstract public function destroy(string $id): bool;

/**
* @inheritDoc
*/
public function gc($max_lifetime)
public function gc(int $max_lifetime): int|false
{
return true;
return $max_lifetime;
}

/**
* @inheritDoc
*/
public function open($path, $name)
public function open(string $path, string $name): bool
{
return true;
}

/**
* @inheritDoc
*/
abstract public function read($id);
abstract public function read(string $id): string|false;

/**
* @inheritDoc
*/
abstract public function write($id, $data);
abstract public function write(string $id, string $data): bool;


}
17 changes: 7 additions & 10 deletions src/Adapters/CookieAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,26 @@ public function __construct(array $options)
/**
* @inheritDoc
*/
public function destroy($id)
public function destroy(string $id): bool
{
setcookie($this->name, '', (time() - 86400));
return setcookie($this->name, '', (time() - 86400)) !== false;
}

/**
* @inheritDoc
*/
public function gc($max_lifetime)
public function gc(int $max_lifetime): int|false
{
if (!is_int($max_lifetime) || $max_lifetime < 0) {
throw new SessionException();
}
if ($this->ttl != $max_lifetime) {
$this->ttl = $max_lifetime;
}
return true;
return $max_lifetime;
}

/**
* @inheritDoc
*/
public function read($id)
public function read(string $id): string|false
{
if(!isset($this->data)){
$this->_decode();
Expand All @@ -92,11 +89,11 @@ public function read($id)
/**
* @inheritDoc
*/
public function write($id, $data)
public function write(string $id, string $data): bool
{
$this->data = $data;
$value = base64_encode($this->encrypt->encrypt($data));
return setcookie($this->name, $value, (time() + $this->ttl));
return setcookie($this->name, $value, (time() + $this->ttl)) !== false;
}

private function _decode(): void
Expand Down
10 changes: 5 additions & 5 deletions src/Adapters/FileAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ public function __construct(array $options)
}


public function read($id)
public function read(string $id): string|false
{
$id = $this->prefix . $id;

return (string) @file_get_contents("{$this->path}/$id");
}

public function write($id, $data)
public function write(string $id, string $data): bool
{
$id = $this->prefix . $id;

return file_put_contents("{$this->path}/$id", $data) !== false;
}

public function destroy($id)
public function destroy(string $id): bool
{
$id = $this->prefix . $id;

Expand All @@ -60,7 +60,7 @@ public function destroy($id)
return true;
}

public function gc($max_lifetime)
public function gc(int $max_lifetime): int|false
{
$files = glob("{$this->path}/{$this->prefix}*");
$currentTime = time();
Expand All @@ -71,7 +71,7 @@ public function gc($max_lifetime)
}
}

return true;
return $max_lifetime;
}

}
10 changes: 5 additions & 5 deletions src/Adapters/MemCacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public function __destruct()
/**
* @inheritDoc
*/
public function destroy($id)
public function destroy(string $id): bool
{
try {
$session = $this->credentials['prefix'] . $id;

return $this->getMemcache()->delete($session) !== FALSE;
return $this->getMemcache()->delete($session) !== false;
} catch (\Exception $e) {
return false;
}
Expand All @@ -92,7 +92,7 @@ public function destroy($id)
/**
* @inheritDoc
*/
public function read($id)
public function read(string $id): string|false
{
try {
$session = $this->credentials['prefix'] . $id;
Expand All @@ -114,7 +114,7 @@ public function read($id)
/**
* @inheritDoc
*/
public function write($id, $data)
public function write(string $id, string $data): bool
{
try {
$session = $this->credentials['prefix'] . $id;
Expand All @@ -129,7 +129,7 @@ public function write($id, $data)
default:
$res = false;
}
return $res;
return $res !== false;
} catch (\Exception $e) {
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Adapters/MongoDBAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(array $options)
/**
* @inheritDoc
*/
public function destroy($id)
public function destroy(string $id): bool
{
try {
$bulkWrite = new \MongoDB\Driver\BulkWrite();
Expand All @@ -65,7 +65,7 @@ public function destroy($id)
/**
* @inheritDoc
*/
public function read($id)
public function read(string $id): string|false
{
try {
$query = new \MongoDB\Driver\Query(['_id' => $id]);
Expand All @@ -84,7 +84,7 @@ public function read($id)
/**
* @inheritDoc
*/
public function write($id, $data)
public function write(string $id, string $data): bool
{
try {
$bulkWrite = new \MongoDB\Driver\BulkWrite();
Expand All @@ -95,7 +95,7 @@ public function write($id, $data)

unset($bulkWrite);

return $res->isAcknowledged() !== FALSE;
return $res->isAcknowledged() !== false;
} catch (\Exception $e) {
return false;
}
Expand Down
14 changes: 7 additions & 7 deletions src/Adapters/PDOAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(array $options)
/**
* @inheritDoc
*/
public function destroy($id)
public function destroy(string $id): bool
{
try {
$arguments = [
Expand All @@ -64,7 +64,7 @@ public function destroy($id)
}

$stmt = $this->query($sql, $arguments);
return $stmt !== FALSE && $stmt->rowCount() > 0;
return $stmt !== false && $stmt->rowCount() > 0;
} catch (\Exception $e) {
return false;
}
Expand All @@ -73,7 +73,7 @@ public function destroy($id)
/**
* @inheritDoc
*/
public function read($id)
public function read(string $id): string|false
{
try {
$arguments = [
Expand All @@ -86,7 +86,7 @@ public function read($id)
}
$sql .= " LIMIT 0, 1";
$stmt = $this->query($sql, $arguments);
if ($stmt === FALSE) {
if ($stmt === false) {
return false;
}
if ($stmt->rowCount() < 1) {
Expand All @@ -102,7 +102,7 @@ public function read($id)
/**
* @inheritDoc
*/
public function write($id, $data)
public function write(string $id, string $data): bool
{
try {
$stmt = $this->query("REPLACE INTO " . $this->table . " (id, sess_timestamp, sess_ip_address, sess_data) VALUES (:sess_id, :sess_data, :sess_timestamp, :ip_address);", [
Expand All @@ -112,13 +112,13 @@ public function write($id, $data)
':ip_address' => $this->getIP(),
]);

return $stmt !== FALSE && $stmt->rowCount() > 0;
return $stmt !== false && $stmt->rowCount() > 0;
} catch (\Exception $e) {
return false;
}
}

private function query(string $query, ?array $arguments = null)
private function query(string $query, ?array $arguments = null): bool|\PDOStatement
{
$stmt = $this->pdo->prepare($query);
if ($stmt === FALSE) {
Expand Down
14 changes: 8 additions & 6 deletions src/Adapters/RedisAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public function __construct(array $options)
/**
* @inheritDoc
*/
public function destroy($id)
public function destroy(string $id): bool
{
try {
return $this->redis->del($this->prefix . $id) !== FALSE;
return $this->redis->del($this->prefix . $id) !== false;
} catch (\Exception $e) {
return false;
}
Expand All @@ -85,10 +85,12 @@ public function destroy($id)
/**
* @inheritDoc
*/
public function read($id)
public function read(string $id): string|false
{
try {
return (string)$this->redis->get($this->prefix . $id);
$data = $this->redis->get($this->prefix . $id);

return $data !== false ? (string)$data : false;
} catch (\Exception $e) {
return false;
}
Expand All @@ -97,11 +99,11 @@ public function read($id)
/**
* @inheritDoc
*/
public function write($id, $data)
public function write(string $id, string $data): bool
{
try {
$set = $this->redis->set($this->prefix . $id, $data);
if($set === FALSE){
if($set === false){
return false;
}
$this->redis->expireAt($this->prefix . $id, time() + $this->ttl);
Expand Down

0 comments on commit dce9624

Please sign in to comment.