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

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Jan 11, 2017
1 parent 52ebbac commit 0ab1e39
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 4 deletions.
6 changes: 2 additions & 4 deletions .gitignore
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/
29 changes: 29 additions & 0 deletions composer.json
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/"
}
}
}
15 changes: 15 additions & 0 deletions demo/session.php
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');
84 changes: 84 additions & 0 deletions src/Session/Extension.php
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);

}
33 changes: 33 additions & 0 deletions src/Session/Extensions/Flash.php
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;
}

}
45 changes: 45 additions & 0 deletions src/Session/Extensions/Variable.php
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;
}

}
81 changes: 81 additions & 0 deletions src/Session/Session.php
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() ?: [];
}

}

0 comments on commit 0ab1e39

Please sign in to comment.