-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move SessionTrait from atk4/core and separate handler (#1733)
- Loading branch information
Showing
13 changed files
with
458 additions
and
45 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
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
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
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,62 @@ | ||
============= | ||
Session Trait | ||
============= | ||
|
||
.. php:trait:: SessionTrait | ||
Introduction | ||
============ | ||
|
||
SessionTrait is a simple way to let object store relevant data in the session. Specifically used in ATK UI | ||
some objects want to memorize data. (see https://github.com/atk4/ui/blob/develop/src/Wizard.php#L12) | ||
|
||
You would need 3 things. First make use of session trait:: | ||
|
||
use \Atk4\Ui\SessionTrait; | ||
|
||
next you may memorize any value, which will be stored independently from any other object (even of a same class):: | ||
|
||
$this->memorize('dsn', $dsn); | ||
|
||
Later when you need the value, you can simply recall it:: | ||
|
||
$dsn = $this->recall('dsn'); | ||
|
||
|
||
Properties | ||
========== | ||
|
||
.. php:attr:: session_key | ||
Internal property to make sure that all session data will be stored in one | ||
"container" (array key). | ||
|
||
Methods | ||
======= | ||
|
||
.. php:method:: startSession($options = []) | ||
Create new session. | ||
|
||
.. php:method:: destroySession() | ||
Destroy existing session. | ||
|
||
.. php:method:: memorize($key, $value) | ||
Remember data in object-relevant session data. | ||
|
||
.. php:method:: learn($key, $default = null) | ||
Similar to memorize, but if value for key exist, will return it. | ||
|
||
.. php:method:: recall($key, $default = null) | ||
Returns session data for this object. If not previously set, then $default | ||
is returned. | ||
|
||
.. php:method:: forget($key = null) | ||
Forget session data for arg $key. If $key is omitted will forget all | ||
associated session data. |
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
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
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
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,121 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Ui\App; | ||
|
||
use Atk4\Ui\Exception; | ||
|
||
class Session | ||
{ | ||
/** @var string Session container key. */ | ||
protected $session_key = '__atk_session'; | ||
|
||
/** | ||
* Create new session. | ||
* | ||
* @param array $options Options for session_start() | ||
*/ | ||
public function startSession(array $options = []): void | ||
{ | ||
switch (session_status()) { | ||
case \PHP_SESSION_DISABLED: | ||
// @codeCoverageIgnoreStart - impossible to test | ||
throw new Exception('Sessions are disabled on server'); | ||
// @codeCoverageIgnoreEnd | ||
case \PHP_SESSION_NONE: | ||
session_start($options); | ||
|
||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Destroy existing session. | ||
*/ | ||
public function destroySession(): void | ||
{ | ||
if (session_status() === \PHP_SESSION_ACTIVE) { | ||
session_destroy(); | ||
unset($_SESSION); | ||
} | ||
} | ||
|
||
/** | ||
* Remember data in object-relevant session data. | ||
* | ||
* @param mixed $value | ||
* | ||
* @return mixed $value | ||
*/ | ||
public function memorize(string $namespace, string $key, $value) | ||
{ | ||
$this->startSession(); | ||
|
||
$_SESSION[$this->session_key][$namespace][$key] = $value; | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* Similar to memorize, but if value for key exist, will return it. | ||
* | ||
* @param mixed $default | ||
* | ||
* @return mixed Previously memorized data or $default | ||
*/ | ||
public function learn(string $namespace, string $key, $default = null) | ||
{ | ||
$this->startSession(); | ||
|
||
if (!isset($_SESSION[$this->session_key][$namespace][$key])) { | ||
if ($default instanceof \Closure) { | ||
$default = $default($key); | ||
} | ||
|
||
return $this->memorize($namespace, $key, $default); | ||
} | ||
|
||
return $this->recall($namespace, $key); | ||
} | ||
|
||
/** | ||
* Returns session data for this object. If not previously set, then | ||
* $default is returned. | ||
* | ||
* @param mixed $default | ||
* | ||
* @return mixed Previously memorized data or $default | ||
*/ | ||
public function recall(string $namespace, string $key, $default = null) | ||
{ | ||
$this->startSession(); | ||
|
||
if (!isset($_SESSION[$this->session_key][$namespace][$key])) { | ||
if ($default instanceof \Closure) { | ||
$default = $default($key); | ||
} | ||
|
||
return $default; | ||
} | ||
|
||
return $_SESSION[$this->session_key][$namespace][$key]; | ||
} | ||
|
||
/** | ||
* Forget session data for $key. If $key is omitted will forget all | ||
* associated session data. | ||
* | ||
* @param string $key Optional key of data to forget | ||
*/ | ||
public function forget(string $namespace, string $key = null): void | ||
{ | ||
$this->startSession(); | ||
|
||
if ($key === null) { | ||
unset($_SESSION[$this->session_key][$namespace]); | ||
} else { | ||
unset($_SESSION[$this->session_key][$namespace][$key]); | ||
} | ||
} | ||
} |
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,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Ui; | ||
|
||
use Atk4\Core\TraitUtil; | ||
|
||
trait SessionTrait | ||
{ | ||
private function getSession(): App\Session | ||
{ | ||
// all methods use this method, so we better check NameTrait existence here in one place | ||
if (!TraitUtil::hasNameTrait($this)) { | ||
throw new Exception('Object should have NameTrait applied to use session'); | ||
} | ||
|
||
return $this->getApp()->session; | ||
} | ||
|
||
/** | ||
* Remember data in object-relevant session data. | ||
* | ||
* @param mixed $value | ||
* | ||
* @return mixed $value | ||
*/ | ||
public function memorize(string $key, $value) | ||
{ | ||
return $this->getSession()->memorize($this->name, $key, $value); | ||
} | ||
|
||
/** | ||
* Similar to memorize, but if value for key exist, will return it. | ||
* | ||
* @param mixed $default | ||
* | ||
* @return mixed Previously memorized data or $default | ||
*/ | ||
public function learn(string $key, $default = null) | ||
{ | ||
return $this->getSession()->learn($this->name, $key, $default); | ||
} | ||
|
||
/** | ||
* Returns session data for this object. If not previously set, then | ||
* $default is returned. | ||
* | ||
* @param mixed $default | ||
* | ||
* @return mixed Previously memorized data or $default | ||
*/ | ||
public function recall(string $key, $default = null) | ||
{ | ||
return $this->getSession()->recall($this->name, $key, $default); | ||
} | ||
|
||
/** | ||
* Forget session data for $key. If $key is omitted will forget all | ||
* associated session data. | ||
* | ||
* @param string $key Optional key of data to forget | ||
* | ||
* @return $this | ||
*/ | ||
public function forget(string $key = null) | ||
{ | ||
$this->getSession()->forget($this->name, $key); | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.