This commit is contained in:
User
2023-10-10 17:12:40 +03:00
commit 3429959de1
5 changed files with 138 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
.idea/
/vendor
composer.lock

21
README.md Normal file
View File

@@ -0,0 +1,21 @@
## Rmphp/Session
DB component for **Rmphp**
## Install
Stable version
```bash
composer require rmphp/session
```
```bash
composer require rmphp/session:"^1.0"
```
Dev version contains the latest changes
```bash
composer require rmphp/session:"1.x-dev"
```

19
composer.json Normal file
View File

@@ -0,0 +1,19 @@
{
"name": "rmphp/session",
"license": "proprietary",
"authors": [
{
"name": "Yuri Zuev",
"email": "y_zuev@mail.ru"
}
],
"require": {
"php": "^8.1"
},
"autoload": {
"psr-4": {
"Rmphp\\Session\\": "src/"
}
}
}

57
src/Session/Session.php Normal file
View File

@@ -0,0 +1,57 @@
<?php
namespace Rmphp\Session\Session;
class Session implements SessionInterface {
const INT = "INT";
const STRING = "STRING";
public function __construct(string $name = "usi") {
if(session_status() == PHP_SESSION_NONE) {
if(in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)){
session_id("cli");
} elseif(!empty($name)) {
session_name($name);
}
session_start();
}
}
/** @inheritDoc */
public function has(string $name = "") : bool {
return (!empty($name)) ? isset($this->getSession()[$name]) : !empty($this->getSession());
}
/** @inheritDoc */
public function get(string $name = "", string $type = ""): mixed {
$session = $this->getSession();
if(!empty($name = strtolower($name))) {
if (!isset($session[$name])) return null;
elseif ($type == self::STRING) {
return (!empty($session[$name])) ? (string)$session[$name] : null;
}
elseif ($type == self::INT) {
return (!empty((int)$session[$name]) || $session[$name]==0) ? (int)$session[$name] : null;
}
return $session[$name];
}
return $session;
}
/** @inheritDoc */
public function set(string $name, $value = null) : void {
$_SESSION[$name] = $value;
}
/** @inheritDoc */
public function clear(string $name = null) : void {
if (isset($name)) unset($_SESSION[$name]);
else $_SESSION = [];
}
/** @inheritDoc */
private function getSession() : array {
return $_SESSION;
}
}

View File

@@ -0,0 +1,38 @@
<?php
/**
* Created by PhpStorm.
* User: Zuev Yuri
* Date: 10.10.2023
* Time: 14:34
*/
namespace Rmphp\Session\Session;
interface SessionInterface {
/**
* @param string $name
* @return bool
*/
public function has(string $name = "") : bool;
/**
* @param string $name
* @param string $type
* @return mixed
*/
public function get(string $name = "", string $type = ""): mixed;
/**
* @param string $name
* @param $value
*/
public function set(string $name, $value = null) : void;
/**
* @param string|null $name
* @return void
*/
public function clear(string $name = null) : void;
}