This repository has been archived by the owner on Aug 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
289 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
composer.phar | ||
composer.lock | ||
/vendor/ | ||
|
||
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file | ||
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file | ||
# composer.lock | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "deimos/session", | ||
"description": "Deimos Session.", | ||
"keywords": [ | ||
"deimos", | ||
"session" | ||
], | ||
"homepage": "https://github.com/REZ1DENT3/DeimosSession", | ||
"license": "MIT", | ||
"type": "library", | ||
"authors": [ | ||
{ | ||
"name": "rez1dent3", | ||
"email": "maksim.babichev95@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"deimos/helper": "~0.3" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~5.7", | ||
"codeclimate/php-test-reporter": "dev-master" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Deimos\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
include_once dirname(__DIR__) . '/vendor/autoload.php'; | ||
|
||
$builder = new Deimos\Builder\Builder(); | ||
$session = new Deimos\Session\Session($builder); | ||
|
||
var_dump($session->hello); | ||
|
||
$session->hello = 'привет'; | ||
|
||
var_dump($session->getRequired('hello')); | ||
var_dump($session->get('world', 'мир')); | ||
|
||
$session->remove('hello'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Deimos\Session; | ||
|
||
use Deimos\Builder\Builder; | ||
use Deimos\Helper\Traits\Helper; | ||
use Deimos\Session\Extensions\Variable; | ||
|
||
abstract class Extension | ||
{ | ||
|
||
use Helper; | ||
use Variable; | ||
|
||
/** | ||
* Session constructor. | ||
* | ||
* @param Builder $builder | ||
*/ | ||
public function __construct(Builder $builder) | ||
{ | ||
$this->builder = $builder; | ||
|
||
$this->init(); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param mixed $value | ||
*/ | ||
public function set($name, $value) | ||
{ | ||
$this->object[$name] = $value; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return bool | ||
*/ | ||
public function remove($name) | ||
{ | ||
if (isset($this->{$name})) | ||
{ | ||
unset($this->object[$name]); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* remove all keys | ||
*/ | ||
public final function removeAll() | ||
{ | ||
foreach ($this->object() as $name => &$value) | ||
{ | ||
$this->remove($name); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return mixed | ||
*/ | ||
abstract public function __get($name); | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return bool | ||
*/ | ||
abstract public function __isset($name); | ||
|
||
/** | ||
* @param string $name | ||
* @param mixed $value | ||
*/ | ||
abstract public function __set($name, $value); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Deimos\Session\Extensions; | ||
|
||
trait Flash | ||
{ | ||
|
||
/** | ||
* @param string $name | ||
* @param mixed $value | ||
* | ||
* @return mixed | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function flash($name, $value = null) | ||
{ | ||
$name .= 'DeimosFlash'; | ||
|
||
if ($value === null) | ||
{ | ||
$value = $this->get($name); | ||
$this->remove($name); | ||
} | ||
else | ||
{ | ||
$this->set($name, $value); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Deimos\Session\Extensions; | ||
|
||
trait Variable | ||
{ | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $object; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $init; | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
private function init() | ||
{ | ||
if (!$this->init) | ||
{ | ||
if ($_SESSION === null) | ||
{ | ||
global $_SESSION; | ||
} | ||
|
||
$this->init = session_start(); | ||
$this->object = &$_SESSION; | ||
} | ||
|
||
return !$this->init; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function &object() | ||
{ | ||
return $this->object; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Deimos\Session; | ||
|
||
use Deimos\Session\Extensions\Flash; | ||
|
||
class Session extends Extension | ||
{ | ||
|
||
use Flash; | ||
|
||
/** | ||
* @param string $name | ||
* @param null $default | ||
* | ||
* @return mixed | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function get($name, $default = null) | ||
{ | ||
return $this->helper()->arr()->get($this->asArray(), $name, $default); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return mixed | ||
* | ||
* @throws \Deimos\Helper\Exceptions\ExceptionEmpty | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function getRequired($name) | ||
{ | ||
return $this->helper()->arr()->getRequired($this->asArray(), $name); | ||
} | ||
|
||
/** | ||
* Alias getRequired($name) | ||
* | ||
* @param string $name | ||
* | ||
* @return mixed | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function __get($name) | ||
{ | ||
return $this->get($name); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param mixed $value | ||
*/ | ||
public function __set($name, $value) | ||
{ | ||
$this->set($name, $value); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return bool | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function __isset($name) | ||
{ | ||
return $this->helper()->arr()->keyExists($this->asArray(), $name); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function asArray() | ||
{ | ||
return $this->object() ?: []; | ||
} | ||
|
||
} |