Skip to content

Commit

Permalink
Merge branch 'master' of github.com:swoft-cloud/swoft-component
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzhhui committed Jun 14, 2018
2 parents 348adc7 + d99e078 commit e2bc8e6
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 39 deletions.
25 changes: 10 additions & 15 deletions src/Handler/FileSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,35 @@


/**
* Class FileSessionHandler
*
* @Bean()
* @uses FileSessionHandler
* @version 2017年12月05日
* @author huangzhhui <[email protected]>
* @copyright Copyright 2010-2017 Swoft software
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
* @package Swoft\Session\Handler
*/
class FileSessionHandler implements \SessionHandlerInterface
class FileSessionHandler implements \SessionHandlerInterface, LifetimeInterface
{

use LifetimeTrait;

/**
* @var string
*/
private $path;

/**
* @var int
*/
private $minutes;

/**
* FileSessionHandler constructor.
*
* @param string $path
* @param int $minutes
* @param int $lifetime
*/
public function __construct($path = null, $minutes = 15)
public function __construct($path = null, $lifetime = 15 * 60)
{
if (null !== $path) {
$this->path = $path;
} else {
$this->path = App::getAlias('@runtime/sessions');
}
$this->minutes = $minutes;
$this->setLifetime($lifetime);
}

/**
Expand Down Expand Up @@ -85,7 +80,7 @@ public function read($sessionId)
{
$path = $this->path . '/' . $sessionId;
if (file_exists($path)) {
if (filemtime($path) >= Carbon::now()->subMinutes($this->minutes)->getTimestamp()) {
if (filemtime($path) >= Carbon::now()->subSeconds($this->getLifetime())->getTimestamp()) {
return file_get_contents($path);
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/Handler/LifetimeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Swoft\Session\Handler;


interface LifetimeInterface
{

/**
* @param int $seconds
* @return $this
*/
public function setLifetime(int $seconds);

/**
* @return int
*/
public function getLifetime(): int;

}
32 changes: 32 additions & 0 deletions src/Handler/LifetimeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Swoft\Session\Handler;


trait LifetimeTrait
{

/**
* @var int
*/
protected $lifetime = 0;

/**
* @param int $seconds
* @return $this
*/
public function setLifetime(int $seconds)
{
$this->lifetime = $seconds;
return $this;
}

/**
* @return int
*/
public function getLifetime(): int
{
return $this->lifetime;
}

}
8 changes: 3 additions & 5 deletions src/Handler/NeedRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
use Swoft\Http\Message\Server\Request;

/**
* @uses NeedRequestInterface
* @version 2018年02月01日
* @author huangzhhui <[email protected]>
* @copyright Copyright 2010-2018 Swoft software
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
* Interface NeedRequestInterface
*
* @package Swoft\Session\Handler
*/
interface NeedRequestInterface
{
Expand Down
25 changes: 10 additions & 15 deletions src/Handler/RedisSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@


/**
* Class RedisSessionHandler
*
* @Bean()
* @uses RedisSessionHandler
* @version 2017年12月05日
* @author huangzhhui <[email protected]>
* @copyright Copyright 2010-2017 Swoft software
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt}
* @package Swoft\Session\Handler
*/
class RedisSessionHandler implements \SessionHandlerInterface
class RedisSessionHandler implements \SessionHandlerInterface, LifetimeInterface
{

use LifetimeTrait;

/**
* @var string
*/
Expand All @@ -27,11 +27,6 @@ class RedisSessionHandler implements \SessionHandlerInterface
*/
private $glue = ':';

/**
* @var int
*/
private $minutes;

/**
* @Inject()
* @var \Swoft\Redis\Redis
Expand All @@ -41,11 +36,11 @@ class RedisSessionHandler implements \SessionHandlerInterface
/**
* RedisSessionHandler constructor.
*
* @param int $minutes
* @param int $lifetime
*/
public function __construct($minutes = 15)
public function __construct($lifetime = 15 * 60)
{
$this->minutes = $minutes;
$this->setLifetime($lifetime);
}

/**
Expand Down Expand Up @@ -94,7 +89,7 @@ public function read($sessionId)
*/
public function write($sessionId, $data)
{
return (bool)$this->redis->set($this->key($sessionId), $this->serialize($data));
return (bool)$this->redis->set($this->key($sessionId), $this->serialize($data), $this->getLifetime());
}

/**
Expand Down
9 changes: 5 additions & 4 deletions src/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Swoft\Bean\Annotation\Bean;
use Swoft\Bean\Annotation\Scope;
use Swoft\Session\Handler\FileSessionHandler;
use Swoft\Session\Handler\LifetimeInterface;
use Swoft\Session\Handler\RedisSessionHandler;

/**
Expand Down Expand Up @@ -42,10 +43,11 @@ class SessionManager
*/
public function createHandlerByConfig(): \SessionHandlerInterface
{
if (!isset($this->config['driver'])) {
if (!isset($this->getConfig()['driver'])) {
throw new \InvalidArgumentException('Session driver required');
}
$handler = $this->getHandler($this->config['driver']);
$handler = $this->getHandler($this->getConfig()['driver']);
$handler instanceof LifetimeInterface && $handler->setLifetime($this->getConfig()['lifetime']);
return $handler;
}

Expand All @@ -60,8 +62,7 @@ public function getHandler(string $name): \SessionHandlerInterface
{
$name = strtolower($name);
$this->isValidate($name);
$class = $this->handlers[$name];
return App::getBean($class);
return App::getBean($this->handlers[$name]);
}

/**
Expand Down

0 comments on commit e2bc8e6

Please sign in to comment.