major update
This commit is contained in:
50
lib/Repository/Support/DatabaseSessionRepository.php
Normal file
50
lib/Repository/Support/DatabaseSessionRepository.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Repository\Support;
|
||||
|
||||
use MintyPHP\DB;
|
||||
|
||||
class DatabaseSessionRepository
|
||||
{
|
||||
public function acquireAdvisoryLock(string $lockName, int $timeoutSeconds = 0): bool
|
||||
{
|
||||
$lockName = trim($lockName);
|
||||
if ($lockName === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$timeoutSeconds = max(0, (int) $timeoutSeconds);
|
||||
$got = DB::selectValue(
|
||||
'select GET_LOCK(?, ?) as got_lock',
|
||||
$lockName,
|
||||
(string) $timeoutSeconds
|
||||
);
|
||||
|
||||
return (int) $got === 1;
|
||||
}
|
||||
|
||||
public function releaseAdvisoryLock(string $lockName): void
|
||||
{
|
||||
$lockName = trim($lockName);
|
||||
if ($lockName === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
DB::selectValue('select RELEASE_LOCK(?) as released_lock', $lockName);
|
||||
}
|
||||
|
||||
public function beginTransaction(): void
|
||||
{
|
||||
DB::handle()->begin_transaction();
|
||||
}
|
||||
|
||||
public function commitTransaction(): void
|
||||
{
|
||||
DB::handle()->commit();
|
||||
}
|
||||
|
||||
public function rollbackTransaction(): void
|
||||
{
|
||||
DB::handle()->rollback();
|
||||
}
|
||||
}
|
||||
@@ -148,4 +148,50 @@ class RepoQuery
|
||||
sort($ids, SORT_NUMERIC);
|
||||
return $ids;
|
||||
}
|
||||
|
||||
public static function normalizeStringList(mixed $value, int $max = 200, ?callable $sanitizer = null): array
|
||||
{
|
||||
if ($max <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (is_string($value)) {
|
||||
$value = array_filter(array_map('trim', explode(',', $value)));
|
||||
} elseif (!is_array($value)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$flat = [];
|
||||
array_walk_recursive($value, static function ($item) use (&$flat): void {
|
||||
$flat[] = $item;
|
||||
});
|
||||
|
||||
$items = [];
|
||||
$seen = [];
|
||||
foreach ($flat as $item) {
|
||||
$text = trim((string) $item);
|
||||
if ($text === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($sanitizer !== null) {
|
||||
$text = trim((string) $sanitizer($text));
|
||||
if ($text === '') {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($seen[$text])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$seen[$text] = true;
|
||||
$items[] = $text;
|
||||
if (count($items) >= $max) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user