Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#5328 adds phalcon version 3.4.x module #5329

Closed
wants to merge 7 commits into from
Closed
12 changes: 10 additions & 2 deletions src/Codeception/Lib/Connector/Phalcon/MemorySession.php
Expand Up @@ -30,6 +30,10 @@ class MemorySession implements AdapterInterface
*/
protected $options = [];

/**
* MemorySession constructor.
* @param array|null $options
*/
public function __construct(array $options = null)
{
$this->sessionId = $this->generateId();
Expand Down Expand Up @@ -288,7 +292,11 @@ public function __unset($index)
$this->remove($index);
}

private function prepareIndex($index)
/**
* @param $index
* @return string
*/
protected function prepareIndex($index)
{
if ($this->sessionId) {
$key = $this->sessionId . '#' . $index;
Expand All @@ -302,7 +310,7 @@ private function prepareIndex($index)
/**
* @return string
*/
private function generateId()
protected function generateId()
{
return md5(time());
}
Expand Down
317 changes: 317 additions & 0 deletions src/Codeception/Lib/Connector/Phalcon/MemorySession34x.php
@@ -0,0 +1,317 @@
<?php
namespace Codeception\Lib\Connector\Phalcon;

use Phalcon\Session\AdapterInterface;

class MemorySession34x implements AdapterInterface
{
/**
* @var string
*/
protected $sessionId;

/**
* @var string
*/
protected $name;

/**
* @var bool
*/
protected $started = false;

/**
* @var array
*/
protected $memory = [];

/**
* @var array
*/
protected $options = [];

/**
* MemorySession constructor.
* @param array|null $options
*/
public function __construct(array $options = null)
{
$this->sessionId = $this->generateId();

if (is_array($options)) {
$this->setOptions($options);
}
}

/**
* @inheritdoc
*/
public function start()
{
if ($this->status() !== PHP_SESSION_ACTIVE) {
$this->memory = [];
$this->started = true;

return true;
}

return false;
}

/**
* @inheritdoc
*
* @param array $options
*/
public function setOptions(array $options)
{
if (isset($options['uniqueId'])) {
$this->sessionId = $options['uniqueId'];
}

$this->options = $options;
}

/**
* @inheritdoc
*
* @return array
*/
public function getOptions(): array
chilimatic marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->options;
}

/**
* @inheritdoc
*
* @param string $index
* @param mixed $defaultValue
* @param bool $remove
* @return mixed
*/
public function get($index, $defaultValue = null, $remove = false)
{
$key = $this->prepareIndex($index);

if (!isset($this->memory[$key])) {
return $defaultValue;
}

$return = $this->memory[$key];

if ($remove) {
unset($this->memory[$key]);
}

return $return;
}

/**
* @inheritdoc
*
* @param string $index
* @param mixed $value
*/
public function set($index, $value)
{
$this->memory[$this->prepareIndex($index)] = $value;
}

/**
* @inheritdoc
*
* @param string $index
* @return bool
*/
public function has(string $index): bool
{
return isset($this->memory[$this->prepareIndex($index)]);
}

/**
* @inheritdoc
*
* @param string $index
*/
public function remove($index)
{
unset($this->memory[$this->prepareIndex($index)]);
}

/**
* @inheritdoc
*
* @return string
*/
public function getId(): string
{
return $this->sessionId;
}

/**
* @inheritdoc
*
* @return bool
*/
public function isStarted(): bool
{
return $this->started;
}

/**
* Returns the status of the current session
*
* ``` php
* <?php
* if ($session->status() !== PHP_SESSION_ACTIVE) {
* $session->start();
* }
* ?>
* ```
*
* @return int
*/
public function status()
{
if ($this->isStarted()) {
return PHP_SESSION_ACTIVE;
}

return PHP_SESSION_NONE;
}

/**
* @inheritdoc
*
* @param bool $removeData
* @return bool
*/
public function destroy($removeData = null): bool
{
if ($removeData) {
if (!empty($this->sessionId)) {
foreach ($this->memory as $key => $value) {
if (0 === strpos($key, $this->sessionId . '#')) {
unset($this->memory[$key]);
}
}
} else {
$this->memory = [];
}
}

$this->started = false;

return true;
}

/**
* @inheritdoc
*
* @param bool $deleteOldSession
* @return \Phalcon\Session\AdapterInterface
*/
public function regenerateId(bool $deleteOldSession = null): \Phalcon\Session\AdapterInterface
{
$this->sessionId = $this->generateId();

return $this;
}

/**
* @inheritdoc
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}

/**
* @inheritdoc
*
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* Dump all session
*
* @return array
*/
public function toArray()
{
return (array) $this->memory;
}

/**
* Alias: Gets a session variable from an application context
*
* @param string $index
* @return mixed
*/
public function __get($index)
{
return $this->get($index);
}

/**
* Alias: Sets a session variable in an application context
*
* @param string $index
* @param mixed $value
*/
public function __set($index, $value)
{
$this->set($index, $value);
}

/**
* Alias: Check whether a session variable is set in an application context
*
* @param string $index
* @return bool
*/
public function __isset($index)
{
return $this->has($index);
}

/**
* Alias: Removes a session variable from an application context
*
* @param string $index
*/
public function __unset($index)
{
$this->remove($index);
}

/**
* @param $index
* @return string
*/
protected function prepareIndex($index)
{
if ($this->sessionId) {
$key = $this->sessionId . '#' . $index;
} else {
$key = $index;
}

return $key;
}

/**
* @return string
*/
protected function generateId()
{
return md5(time());
}
}