Skip to content

Commit

Permalink
merged branch drak/nativestorage (PR #4899)
Browse files Browse the repository at this point in the history
Commits
-------

a351362 [HttpFoundation] Add NativeSessionHandler tests
653821a [HttpFoundation] Remove FileSessionHandler
3456787 Partially revert "[HttpFoundation][Sessions] Refactored tests"
39813a0 Revert "[FrameworkBundle] Refactor session file handler service name and update changelogs"
fbee4cf Restore NativeFileSessionHandler

Discussion
----------

[Session] Restore NativeFileSessionStorage

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #4668
Todo: -
License of the code: MIT

This reverts the removal of the native file handler.
  • Loading branch information
fabpot committed Jul 13, 2012
2 parents 7b0d100 + a351362 commit a798ff1
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 253 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Expand Up @@ -26,7 +26,7 @@ CHANGELOG
* [BC BREAK] following session options: 'lifetime', 'path', 'domain', 'secure',
'httponly' are now prefixed with cookie_ when dumped to the container
* Added `handler_id` configuration under `session` key to represent `session.handler`
service, defaults to `session.handler.file`.
service, defaults to `session.handler.native_file`.
* Added `gc_maxlifetime`, `gc_probability`, and `gc_divisor` to session
configuration. This means session garbage collection has a
`gc_probability`/`gc_divisor` chance of being run. The `gc_maxlifetime` defines
Expand Down
Expand Up @@ -10,7 +10,7 @@
<parameter key="session.attribute_bag.class">Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag</parameter>
<parameter key="session.storage.native.class">Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage</parameter>
<parameter key="session.storage.mock_file.class">Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage</parameter>
<parameter key="session.handler.file.class">Symfony\Component\HttpFoundation\Session\Storage\Handler\FileSessionHandler</parameter>
<parameter key="session.handler.native_file.class">Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler</parameter>
<parameter key="session_listener.class">Symfony\Bundle\FrameworkBundle\EventListener\SessionListener</parameter>
</parameters>

Expand All @@ -34,7 +34,7 @@
<argument>%kernel.cache_dir%/sessions</argument>
</service>

<service id="session.handler.file" class="%session.handler.file.class%" public="false">
<service id="session.handler.native_file" class="%session.handler.native_file.class%" public="false">
<argument>%session.save_path%</argument>
</service>

Expand All @@ -45,6 +45,5 @@

<!-- for BC -->
<service id="session.storage.filesystem" alias="session.storage.mock_file" />
<service id="session.handler.native_file" alias="session.handler.file" />
</services>
</container>
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpFoundation/CHANGELOG.md
Expand Up @@ -22,7 +22,8 @@ CHANGELOG
* [BC BREAK] Moved all session related classes and interfaces into own namespace, as
`Symfony\Component\HttpFoundation\Session` and renamed classes accordingly.
Session handlers are located in the subnamespace `Symfony\Component\HttpFoundation\Session\Handler`.
* SessionHandlers must implement `\SessionHandlerInterface`.
* SessionHandlers must implement `\SessionHandlerInterface` or extend from the
`Symfony\Component\HttpFoundation\Storage\Handler\NativeSessionHandler` base class.
* Added internal storage driver proxy mechanism for forward compatibility with
PHP 5.4 `\SessionHandler` class.
* Added session handlers for custom Memcache, Memcached and Null session save handlers.
Expand Down

This file was deleted.

@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;

/**
* NativeFileSessionHandler.
*
* Native session handler using PHP's built in file storage.
*
* @author Drak <drak@zikula.org>
*/
class NativeFileSessionHandler extends NativeSessionHandler
{
/**
* Constructor.
*
* @param string $savePath Path of directory to save session files. Default null will leave setting as defined by PHP.
*/
public function __construct($savePath = null)
{
if (null === $savePath) {
$savePath = ini_get('session.save_path');
}

if ($savePath && !is_dir($savePath)) {
mkdir($savePath, 0777, true);
}

ini_set('session.save_handler', 'files');
ini_set('session.save_path', $savePath);
}
}
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;

/**
* Adds SessionHandler functionality if available.
*
* @see http://php.net/sessionhandler
*/

if (version_compare(phpversion(), '5.4.0', '>=')) {
class NativeSessionHandler extends \SessionHandler {}
} else {
class NativeSessionHandler {}
}
Expand Up @@ -11,9 +11,6 @@

namespace Symfony\Component\HttpFoundation\Session\Storage;

use Symfony\Component\HttpFoundation\Session\Storage\Handler\FileSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;

/**
* MockFileSessionStorage is used to mock sessions for
* functional testing when done in a single PHP process.
Expand All @@ -28,27 +25,35 @@
class MockFileSessionStorage extends MockArraySessionStorage
{
/**
* @var FileSessionHandler
* @var string
*/
private $savePath;

/**
* @var array
*/
private $handler;
private $sessionData;

/**
* Constructor.
*
* @param string $savePath Path of directory to save session files.
* @param string $name Session name.
* @param FileSessionHandler $handler Save handler
* @param MetadataBag $metaData Metadatabag
* @param string $savePath Path of directory to save session files.
* @param string $name Session name.
* @param MetadataBag $metaBag MetadataBag instance.
*/
public function __construct($savePath = null, $name = 'MOCKSESSID', FileSessionHandler $handler = null, MetadataBag $metaData = null)
public function __construct($savePath = null, $name = 'MOCKSESSID', MetadataBag $metaBag = null)
{
if (null == $handler) {
$handler = new FileSessionHandler($savePath, 'mocksess_');
if (null === $savePath) {
$savePath = sys_get_temp_dir();
}

$this->handler = $handler;
if (!is_dir($savePath)) {
mkdir($savePath, 0777, true);
}

$this->savePath = $savePath;

parent::__construct($name, $metaData);
parent::__construct($name, $metaBag);
}

/**
Expand Down Expand Up @@ -92,7 +97,7 @@ public function regenerate($destroy = false, $lifetime = null)
*/
public function save()
{
$this->handler->write($this->id, serialize($this->data));
file_put_contents($this->getFilePath(), serialize($this->data));

// this is needed for Silex, where the session object is re-used across requests
// in functional tests. In Symfony, the container is rebooted, so we don't have
Expand All @@ -106,16 +111,28 @@ public function save()
*/
private function destroy()
{
$this->handler->destroy($this->id);
if (is_file($this->getFilePath())) {
unlink($this->getFilePath());
}
}

/**
* Calculate path to file.
*
* @return string File path
*/
private function getFilePath()
{
return $this->savePath.'/'.$this->id.'.mocksess';
}

/**
* Reads session from storage and loads session.
*/
private function read()
{
$data = $this->handler->read($this->id);
$this->data = $data ? unserialize($data) : array();
$filePath = $this->getFilePath();
$this->data = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : array();

$this->loadSession();
}
Expand Down

0 comments on commit a798ff1

Please sign in to comment.