Skip to content

Commit

Permalink
Moved session stuff under Cake/Network namespace.
Browse files Browse the repository at this point in the history
Replaced custom SessionHandlerInterface with one provided by PHP itself.
  • Loading branch information
ADmad committed Nov 14, 2013
1 parent 918403b commit b6b8528
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 178 deletions.
2 changes: 1 addition & 1 deletion Cake/Controller/Component/SessionComponent.php
Expand Up @@ -15,7 +15,7 @@
namespace Cake\Controller\Component;

use Cake\Controller\Component;
use Cake\Model\Datasource\Session;
use Cake\Network\Session;

/**
* The CakePHP SessionComponent provides a way to persist client data between
Expand Down
2 changes: 1 addition & 1 deletion Cake/I18n/I18n.php
Expand Up @@ -21,8 +21,8 @@
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Error;
use Cake\Model\Datasource\Session;
use Cake\Network\Request;
use Cake\Network\Session;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use Cake\Utility\String;
Expand Down
72 changes: 0 additions & 72 deletions Cake/Model/Datasource/Session/SessionHandlerInterface.php

This file was deleted.

13 changes: 6 additions & 7 deletions Cake/Model/Datasource/Session.php → Cake/Network/Session.php
Expand Up @@ -12,13 +12,13 @@
* @since CakePHP(tm) v .0.10.0.1222
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Model\Datasource;
namespace Cake\Network;

use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Error;
use Cake\Model\Datasource\Session\SessionHandlerInterface;
use Cake\Utility\Hash;
use SessionHandlerInterface;

/**
* Session class for CakePHP.
Expand Down Expand Up @@ -111,7 +111,7 @@ class Session {
* This feature is only used when config value `Session.autoRegenerate` is set to true.
*
* @var integer
* @see Cake\Model\Datasource\Session::_checkValid()
* @see Cake\Network\Session::_checkValid()
*/
public static $requestCountdown = 10;

Expand Down Expand Up @@ -513,15 +513,15 @@ protected static function _configureSession() {
* @throws Cake\Error\Exception
*/
protected static function _getHandler($class) {
$class = App::className($class, 'Model/Datasource/Session');
$class = App::className($class, 'Network/Session');
if (!class_exists($class)) {
throw new Error\Exception(__d('cake_dev', 'Could not load %s to handle the session.', $class));
}
$handler = new $class();
if ($handler instanceof SessionHandlerInterface) {
return $handler;
}
throw new Error\Exception(__d('cake_dev', 'Chosen SessionHandler does not implement SessionHandlerInterface it cannot be used with an engine key.'));
throw new Error\Exception(__d('cake_dev', 'Chosen SessionHandler does not implement SessionHandlerInterface, it cannot be used with an engine key.'));
}

/**
Expand Down Expand Up @@ -584,8 +584,7 @@ protected static function _defaultConfig($name) {
'session.serialize_handler' => 'php',
),
'handler' => array(
'engine' => 'DatabaseSession',
'model' => 'Session'
'engine' => 'DatabaseSession'
)
)
);
Expand Down
Expand Up @@ -16,10 +16,11 @@
* @since CakePHP(tm) v 2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Model\Datasource\Session;
namespace Cake\Network\Session;

use Cake\Cache\Cache;
use Cake\Core\Configure;
use SessionHandlerInterface;

/**
* CacheSession provides method for saving sessions into a Cache engine. Used with Session
Expand All @@ -31,9 +32,11 @@ class CacheSession implements SessionHandlerInterface {
/**
* Method called on open of a database session.
*
* @param The path where to store/retrieve the session.
* @param The session name.
* @return boolean Success
*/
public function open() {
public function open($savePath, $name) {
return true;
}

Expand All @@ -47,7 +50,7 @@ public function close() {
}

/**
* Method used to read from a database session.
* Method used to read from a cache session.
*
* @param string $id The key of the value to read
* @return mixed The value of the key or false if it does not exist
Expand All @@ -57,7 +60,7 @@ public function read($id) {
}

/**
* Helper function called on write for database sessions.
* Helper function called on write for cache sessions.
*
* @param integer $id ID that uniquely identifies session in database
* @param mixed $data The value of the data to be saved.
Expand All @@ -68,7 +71,7 @@ public function write($id, $data) {
}

/**
* Method called on the destruction of a database session.
* Method called on the destruction of a cache session.
*
* @param integer $id ID that uniquely identifies session in cache
* @return boolean True for successful delete, false otherwise.
Expand All @@ -80,11 +83,11 @@ public function destroy($id) {
/**
* Helper function called on gc for cache sessions.
*
* @param integer $expires Timestamp (defaults to current time)
* @return boolean Success
* @param string $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
* @return boolean True on success, false on failure.
*/
public function gc($expires = null) {
return Cache::gc(Configure::read('Session.handler.config'), $expires);
public function gc($maxlifetime) {
return Cache::gc(Configure::read('Session.handler.config'), time() - $maxlifetime);
}

}
Expand Up @@ -17,10 +17,12 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

namespace Cake\Model\Datasource\Session;
namespace Cake\Network\Session;

use Cake\Core\Configure;
use Cake\Utility\ClassRegistry;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use SessionHandlerInterface;

/**
* DatabaseSession provides methods to be used with Session.
Expand All @@ -29,11 +31,11 @@
class DatabaseSession implements SessionHandlerInterface {

/**
* Reference to the model handling the session data
* Reference to the table handling the session data
*
* @var Model
*/
protected $_model;
protected $_table;

/**
* Number of seconds to mark the session as expired
Expand All @@ -52,26 +54,27 @@ public function __construct() {

if (empty($modelName)) {
$settings = array(
'class' => 'Session',
'alias' => 'Session',
'alias' => 'Sessions',
'table' => 'cake_sessions',
);
} else {
$settings = array(
'class' => $modelName,
'alias' => 'Session',
'className' => $modelName,
'alias' => 'Sessions',
);
}
$this->_model = ClassRegistry::init($settings);
$this->_table = TableRegistry::get('Sessions', $settings);
$this->_timeout = Configure::read('Session.timeout') * 60;
}

/**
* Method called on open of a database session.
*
* @param The path where to store/retrieve the session.
* @param The session name.
* @return boolean Success
*/
public function open() {
public function open($savePath, $name) {
return true;
}

Expand All @@ -91,15 +94,18 @@ public function close() {
* @return mixed The value of the key or false if it does not exist
*/
public function read($id) {
$row = $this->_model->find('first', array(
'conditions' => array($this->_model->primaryKey => $id)
));

if (empty($row[$this->_model->alias]['data'])) {
$result = $this->_table
->find('all')
->select(['data'])
->where([$this->_table->primaryKey() => $id])
->hydrate(false)
->first();

if (empty($result)) {
return false;
}

return $row[$this->_model->alias]['data'];
return $result['data'];
}

/**
Expand All @@ -114,9 +120,13 @@ public function write($id, $data) {
return false;
}
$expires = time() + $this->_timeout;
$record = compact('id', 'data', 'expires');
$record[$this->_model->primaryKey] = $id;
return $this->_model->save($record);
$record = compact('data', 'expires');
$record[$this->_table->primaryKey()] = $id;
$result = $this->_table->save(new Entity($record));
if ($result) {
return $result->toArray();
}
return false;
}

/**
Expand All @@ -126,22 +136,17 @@ public function write($id, $data) {
* @return boolean True for successful delete, false otherwise.
*/
public function destroy($id) {
return $this->_model->delete($id);
return $this->_table->delete(new Entity([$this->_table->primaryKey() => $id]));
}

/**
* Helper function called on gc for database sessions.
*
* @param integer $expires Timestamp (defaults to current time)
* @return boolean Success
* @param string $maxlifetime Sessions that have not updated for the last maxlifetime seconds will be removed.
* @return boolean True on success, false on failure.
*/
public function gc($expires = null) {
if (!$expires) {
$expires = time();
} else {
$expires = time() - $expires;
}
return $this->_model->deleteAll(array($this->_model->alias . ".expires <" => $expires), false, false);
public function gc($maxlifetime) {
return $this->_table->deleteAll(['expires <' => time() - $maxlifetime]);
}

}
47 changes: 47 additions & 0 deletions Cake/Test/Fixture/CakeSessionFixture.php
@@ -0,0 +1,47 @@
<?php
/**
* Short description for file.
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @since CakePHP(tm) v 1.2.0.4667
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

/**
* Class SessionFixture
*
*/
class CakeSessionFixture extends TestFixture {

/**
* fields property
*
* @var array
*/
public $fields = array(
'id' => ['type' => 'string', 'length' => 128],
'data' => ['type' => 'text', 'null' => true],
'expires' => ['type' => 'integer', 'length' => 11, 'null' => true],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
);

/**
* records property
*
* @var array
*/
public $records = array();
}

0 comments on commit b6b8528

Please sign in to comment.