Skip to content

Commit

Permalink
Version 3.5.1
Browse files Browse the repository at this point in the history
Exception fix
  • Loading branch information
emretulek committed Apr 24, 2021
1 parent 7377e02 commit 1ddd2d5
Show file tree
Hide file tree
Showing 38 changed files with 506 additions and 322 deletions.
2 changes: 1 addition & 1 deletion Core/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class App
/**
* semantic version
*/
const VERSION = "3.5.0";
const VERSION = "3.5.1";

/**
* @var static
Expand Down
29 changes: 15 additions & 14 deletions Core/Auth/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,21 @@ public function logout(bool $clear_all = false)
}


/**
* @param $key
* @param null $value
* @return bool|mixed
*/
public function authSession($key, $value = null)
{
if ($value === null) {
return $this->session()->get('AUTH.USER.' . $key);
} else {
return $this->session()->set('AUTH.USER.' . $key, $value);
}
}


/**
* Beni hatırla seçeneği için cookie oluşturur.
* @param string $token
Expand Down Expand Up @@ -396,20 +411,6 @@ protected function getRoleName()
}


/**
* @param $key
* @param null $value
* @return bool|mixed
*/
public function authSession($key, $value = null)
{
if ($value === null) {
return $this->session()->get('AUTH.USER.' . $key);
} else {
return $this->session()->set('AUTH.USER.' . $key, $value);
}
}


/**
* @return QueryBuilder
Expand Down
10 changes: 5 additions & 5 deletions Core/Cache/ApcuCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Closure;
use RuntimeException;

class ApcuCache implements CacheInterface
class ApcuCache extends BaseCache
{

/**
Expand Down Expand Up @@ -55,7 +55,7 @@ public function get(string $key, $default = null)
*/
public function set(string $key, $value, $ttl = null): bool
{
return apcu_store($key, $value, $ttl);
return apcu_store($key, $value, $this->ttl($ttl));
}


Expand All @@ -70,7 +70,7 @@ public function set(string $key, $value, $ttl = null): bool
*/
public function add(string $key, $value, $ttl = null): bool
{
return apcu_add($key, $value, $ttl);
return apcu_add($key, $value, $this->ttl($ttl));
}

/**
Expand All @@ -88,7 +88,7 @@ public function getSet(string $key, $ttl = null, $default = null)
};
}

return apcu_entry($key, $default, $ttl);
return apcu_entry($key, $default, $this->ttl($ttl));
}

/**
Expand Down Expand Up @@ -132,7 +132,7 @@ public function getMultiple(array $keys): array
*/
public function setMultiple(array $items, $ttl = null): bool
{
return apcu_store($items, null, $ttl);
return apcu_store($items, null, $this->ttl($ttl));
}

/**
Expand Down
100 changes: 100 additions & 0 deletions Core/Cache/BaseCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php


namespace Core\Cache;


use Core\Era\Era;
use DateInterval;

abstract class BaseCache implements CacheInterface
{
/**
* @param null|int|DateInterval $ttl
* @return int
*/
protected function ttl($ttl = null): int
{
if (is_null($ttl)) {
$ttl = 0;
}

if ($ttl instanceof DateInterval) {
$ttl = Era::dateIntervalTo($ttl, 'second');
}

return (int)$ttl;
}

/**
* @param $ttl
* @return int
* @throws \Exception
*/
protected function expires($ttl): int
{
$seconds = $this->ttl($ttl);
$seconds = $seconds ?: 315360000;
return Era::now()->addSecond($seconds)->getTimestamp();
}

/**
* @inheritDoc
*/
abstract public function get(string $key, $default = null);

/**
* @inheritDoc
*/
abstract public function set(string $key, $value, $ttl = null): bool;

/**
* @inheritDoc
*/
abstract public function add(string $key, $value, $ttl = null): bool;

/**
* @inheritDoc
*/
abstract public function getSet(string $key, $ttl = null, $default = null);

/**
* @inheritDoc
*/
abstract public function delete(string $key): bool;

/**
* @inheritDoc
*/
abstract public function clear(): bool;

/**
* @inheritDoc
*/
abstract public function getMultiple(array $keys): array;

/**
* @inheritDoc
*/
abstract public function setMultiple(array $items, $ttl = null): bool;

/**
* @inheritDoc
*/
abstract public function deleteMultiple(array $keys): bool;

/**
* @inheritDoc
*/
abstract public function has(string $key): bool;

/**
* @inheritDoc
*/
abstract public function increment(string $key, $value = 1);

/**
* @inheritDoc
*/
abstract public function decrement(string $key, $value = 1);
}
62 changes: 17 additions & 45 deletions Core/Cache/DatabaseCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
use Closure;
use Core\Database\Database;
use Core\Database\QueryBuilder;
use DateInterval;
use DateTime;
use Exception;

class DatabaseCache implements CacheInterface
class DatabaseCache extends BaseCache
{
protected Database $database;
protected string $table;
Expand Down Expand Up @@ -43,7 +41,7 @@ public function __construct(Database $database, array $config)
*/
public function get(string $key, $default = null)
{
if ($item = $this->table()->select("*")->where("key", $key)->getRow()) {
if ($item = $this->table()->select()->where("key", $key)->getRow()) {

if ($item->expires > time()) {
return unserialize($item->value);
Expand Down Expand Up @@ -71,7 +69,7 @@ public function get(string $key, $default = null)
public function set(string $key, $value, $ttl = null): bool
{
$value = serialize($value);
$expires = $this->timeout($ttl);
$expires = $this->expires($ttl);

try {
$result = $this->table()->insert(compact('key', 'value', 'expires'));
Expand All @@ -90,12 +88,11 @@ public function set(string $key, $value, $ttl = null): bool
* @param mixed $value
* @param int|null|\DateInterval $ttl
* @return bool
* @throws InvalidArgumentException
*/
public function add(string $key, $value, $ttl = null): bool
{
$value = serialize($value);
$expires = $this->timeout($ttl);
$expires = $this->expires($ttl);

try {
$result = $this->table()->insert(compact('key', 'value', 'expires'));
Expand Down Expand Up @@ -135,7 +132,6 @@ public function getSet(string $key, $ttl = null, $default = null)
*
* @param string $key
* @return bool
* @throws Exception
*/
public function delete(string $key): bool
{
Expand All @@ -145,18 +141,16 @@ public function delete(string $key): bool
/**
* Tüm önbelleği temizler
* @return bool
* @throws Exception
*/
public function clear(): bool
{
return $this->table()->delete(null, true);
return $this->table()->delete(null, false,true);
}

/**
* Çoklu önbellek listesi
* @param array $keys liste
* @return array A list of key
* @throws InvalidArgumentException
*/
public function getMultiple(array $keys): array
{
Expand All @@ -178,7 +172,6 @@ public function getMultiple(array $keys): array
* @param array $items anahtar değer ilişkili liste
* @param int|null|\DateInterval $ttl geçerlilik süresi
* @return bool
* @throws InvalidArgumentException
*/
public function setMultiple(array $items, $ttl = null): bool
{
Expand All @@ -199,7 +192,6 @@ public function setMultiple(array $items, $ttl = null): bool
* Çoklu önbellekten veri silme
* @param array $keys
* @return bool
* @throws InvalidArgumentException
*/
public function deleteMultiple(array $keys): bool
{
Expand Down Expand Up @@ -231,36 +223,36 @@ public function has(string $key): bool
/**
* @param string $key
* @param int $value
* @return int|false
* @return int
*/
public function increment(string $key, $value = 1)
public function increment(string $key, $value = 1):int
{
$ttl = $this->timeout(null);
$ttl = $this->expires(null);

if (!$this->add($key, $value)) {
$item = $this->get($key);
if ($item = $this->get($key)) {
$value = (int)$item + $value;
$this->set($key, $value, $ttl);
}

$this->set($key, $value, $ttl);

return $value;
}

/**
* @param string $key
* @param int $value
* @return int|false
* @return int
*/
public function decrement(string $key, $value = 1)
public function decrement(string $key, $value = 1):int
{
$ttl = $this->timeout(null);
$ttl = $this->expires(null);

if (!$this->add($key, $value)) {
$item = $this->get($key);
if ($item = $this->get($key)) {
$value = (int)$item - $value;
$this->set($key, $value, $ttl);
}

$this->set($key, $value, $ttl);

return $value;
}

Expand All @@ -271,24 +263,4 @@ private function table(): QueryBuilder
{
return $this->database->table($this->table);
}

/**
* @param $timeout
* @return int
*/
private function timeout($timeout): int
{
$timeout = empty($timeout) ? '999999999' : $timeout;

$date = new DateTime();

if ($timeout instanceof DateInterval) {
$date->add($timeout);
} else {
$dateInterval = new DateInterval("PT{$timeout}S");
$date->add($dateInterval);
}

return $date->format("U");
}
}
Loading

0 comments on commit 1ddd2d5

Please sign in to comment.