Skip to content

Commit

Permalink
fix direct $_SESSION usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Feb 25, 2022
1 parent 3b0b152 commit 1d4f4ac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 26 deletions.
9 changes: 1 addition & 8 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ class Auth
use ContainerTrait;
use DiContainerTrait;
use HookTrait;
use InitializerTrait {
init as private _init;
}
use InitializerTrait;
use TrackableTrait;

/** @const string */
Expand Down Expand Up @@ -117,11 +115,6 @@ public function __construct(App $app, $options = [])
}
}

protected function init(): void
{
$this->_init();
}

/**
* Specify a model for a user check here.
*
Expand Down
25 changes: 7 additions & 18 deletions src/Cache/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,18 @@ class Session // implements CacheInterface
public function __construct(App $app, array $options = [])
{
$this->setApp($app);
$this->name = '__atk4_login';

$this->setDefaults($options);
}

/**
* Initialize cache.
*/
public function init(): void
{
if (\PHP_SAPI !== 'cli') { // helps with unit tests
$this->getApp()->session->startSession(); // TODO use SessionTrait methods instead
}
}

/**
* Return cache key.
*
* @return mixed
*/
public function getKey()
{
$this->init();

return static::class . ':' . ($this->key ?? $this->name);
}

Expand All @@ -61,12 +50,12 @@ public function getKey()
public function getData(): array
{
$key = $this->getKey();

if (!isset($_SESSION[$key]) || ($this->expireTime && $_SESSION[$key . '-at'] + $this->expireTime < microtime(true))) {
$_SESSION[$key] = [];
$data = $this->recall($key);
if ($data === null || ($this->expireTime && $this->recall($key . '-at') + $this->expireTime < microtime(true))) {
$data = [];
}

return $_SESSION[$key];
return $data;
}

/**
Expand All @@ -77,8 +66,8 @@ public function getData(): array
public function setData(array $data)
{
$key = $this->getKey();
$_SESSION[$key] = $data;
$_SESSION[$key . '-at'] = microtime(true);
$this->memorize($key, $data);
$this->memorize($key . '-at', microtime(true));

return $this;
}
Expand Down

0 comments on commit 1d4f4ac

Please sign in to comment.