Skip to content
This repository was archived by the owner on Aug 13, 2023. It is now read-only.

Commit 0ab1e39

Browse files
committed
first version
1 parent 52ebbac commit 0ab1e39

File tree

7 files changed

+289
-4
lines changed

7 files changed

+289
-4
lines changed

.gitignore

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
composer.phar
2+
composer.lock
23
/vendor/
3-
4-
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
5-
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
6-
# composer.lock
4+
.idea/

composer.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "deimos/session",
3+
"description": "Deimos Session.",
4+
"keywords": [
5+
"deimos",
6+
"session"
7+
],
8+
"homepage": "https://github.com/REZ1DENT3/DeimosSession",
9+
"license": "MIT",
10+
"type": "library",
11+
"authors": [
12+
{
13+
"name": "rez1dent3",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"require": {
18+
"deimos/helper": "~0.3"
19+
},
20+
"require-dev": {
21+
"phpunit/phpunit": "~5.7",
22+
"codeclimate/php-test-reporter": "dev-master"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"Deimos\\": "src/"
27+
}
28+
}
29+
}

demo/session.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
include_once dirname(__DIR__) . '/vendor/autoload.php';
4+
5+
$builder = new Deimos\Builder\Builder();
6+
$session = new Deimos\Session\Session($builder);
7+
8+
var_dump($session->hello);
9+
10+
$session->hello = 'привет';
11+
12+
var_dump($session->getRequired('hello'));
13+
var_dump($session->get('world', 'мир'));
14+
15+
$session->remove('hello');

src/Session/Extension.php

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Deimos\Session;
4+
5+
use Deimos\Builder\Builder;
6+
use Deimos\Helper\Traits\Helper;
7+
use Deimos\Session\Extensions\Variable;
8+
9+
abstract class Extension
10+
{
11+
12+
use Helper;
13+
use Variable;
14+
15+
/**
16+
* Session constructor.
17+
*
18+
* @param Builder $builder
19+
*/
20+
public function __construct(Builder $builder)
21+
{
22+
$this->builder = $builder;
23+
24+
$this->init();
25+
}
26+
27+
/**
28+
* @param string $name
29+
* @param mixed $value
30+
*/
31+
public function set($name, $value)
32+
{
33+
$this->object[$name] = $value;
34+
}
35+
36+
/**
37+
* @param string $name
38+
*
39+
* @return bool
40+
*/
41+
public function remove($name)
42+
{
43+
if (isset($this->{$name}))
44+
{
45+
unset($this->object[$name]);
46+
47+
return true;
48+
}
49+
50+
return false;
51+
}
52+
53+
/**
54+
* remove all keys
55+
*/
56+
public final function removeAll()
57+
{
58+
foreach ($this->object() as $name => &$value)
59+
{
60+
$this->remove($name);
61+
}
62+
}
63+
64+
/**
65+
* @param string $name
66+
*
67+
* @return mixed
68+
*/
69+
abstract public function __get($name);
70+
71+
/**
72+
* @param string $name
73+
*
74+
* @return bool
75+
*/
76+
abstract public function __isset($name);
77+
78+
/**
79+
* @param string $name
80+
* @param mixed $value
81+
*/
82+
abstract public function __set($name, $value);
83+
84+
}

src/Session/Extensions/Flash.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Deimos\Session\Extensions;
4+
5+
trait Flash
6+
{
7+
8+
/**
9+
* @param string $name
10+
* @param mixed $value
11+
*
12+
* @return mixed
13+
*
14+
* @throws \InvalidArgumentException
15+
*/
16+
public function flash($name, $value = null)
17+
{
18+
$name .= 'DeimosFlash';
19+
20+
if ($value === null)
21+
{
22+
$value = $this->get($name);
23+
$this->remove($name);
24+
}
25+
else
26+
{
27+
$this->set($name, $value);
28+
}
29+
30+
return $value;
31+
}
32+
33+
}

src/Session/Extensions/Variable.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Deimos\Session\Extensions;
4+
5+
trait Variable
6+
{
7+
8+
/**
9+
* @var array
10+
*/
11+
private $object;
12+
13+
/**
14+
* @var bool
15+
*/
16+
private $init;
17+
18+
/**
19+
* @return bool
20+
*/
21+
private function init()
22+
{
23+
if (!$this->init)
24+
{
25+
if ($_SESSION === null)
26+
{
27+
global $_SESSION;
28+
}
29+
30+
$this->init = session_start();
31+
$this->object = &$_SESSION;
32+
}
33+
34+
return !$this->init;
35+
}
36+
37+
/**
38+
* @return array
39+
*/
40+
protected function &object()
41+
{
42+
return $this->object;
43+
}
44+
45+
}

src/Session/Session.php

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Deimos\Session;
4+
5+
use Deimos\Session\Extensions\Flash;
6+
7+
class Session extends Extension
8+
{
9+
10+
use Flash;
11+
12+
/**
13+
* @param string $name
14+
* @param null $default
15+
*
16+
* @return mixed
17+
*
18+
* @throws \InvalidArgumentException
19+
*/
20+
public function get($name, $default = null)
21+
{
22+
return $this->helper()->arr()->get($this->asArray(), $name, $default);
23+
}
24+
25+
/**
26+
* @param string $name
27+
*
28+
* @return mixed
29+
*
30+
* @throws \Deimos\Helper\Exceptions\ExceptionEmpty
31+
* @throws \InvalidArgumentException
32+
*/
33+
public function getRequired($name)
34+
{
35+
return $this->helper()->arr()->getRequired($this->asArray(), $name);
36+
}
37+
38+
/**
39+
* Alias getRequired($name)
40+
*
41+
* @param string $name
42+
*
43+
* @return mixed
44+
*
45+
* @throws \InvalidArgumentException
46+
*/
47+
public function __get($name)
48+
{
49+
return $this->get($name);
50+
}
51+
52+
/**
53+
* @param string $name
54+
* @param mixed $value
55+
*/
56+
public function __set($name, $value)
57+
{
58+
$this->set($name, $value);
59+
}
60+
61+
/**
62+
* @param string $name
63+
*
64+
* @return bool
65+
*
66+
* @throws \InvalidArgumentException
67+
*/
68+
public function __isset($name)
69+
{
70+
return $this->helper()->arr()->keyExists($this->asArray(), $name);
71+
}
72+
73+
/**
74+
* @return array
75+
*/
76+
public function asArray()
77+
{
78+
return $this->object() ?: [];
79+
}
80+
81+
}

0 commit comments

Comments
 (0)