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

Iaejean custom http session #120

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/mg/Ding/HttpSession/Exception/HttpSessionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Ding\HttpSession\Exception;

class HttpSessionException extends \Exception
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this exception thrown? Doesn't seem to be used at all

{
}
2 changes: 1 addition & 1 deletion src/mg/Ding/HttpSession/HttpSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @license http://marcelog.github.com/ Apache License 2.0
* @link http://marcelog.github.com/
*/
class HttpSession
class HttpSession implements IHttpSession
{
/**
* Current instance.
Expand Down
15 changes: 15 additions & 0 deletions src/mg/Ding/HttpSession/IHttpSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Ding\HttpSession;

interface IHttpSession
{
public function destroy();

public function hasAttribute($name);

public function getAttribute($name);

public function setAttribute($name, $value);

public function getSession();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is still defined as static in HttpSession... are you sure you ran the tests and they pass?

}
4 changes: 2 additions & 2 deletions src/mg/Ding/Mvc/Http/HttpFrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
namespace Ding\Mvc\Http;

use Ding\Mvc\IViewRender;
use Ding\HttpSession\HttpSession;
use Ding\Mvc\IMapper;
use Ding\Mvc\Exception\MvcException;
use Ding\Mvc\ModelAndView;
Expand Down Expand Up @@ -116,7 +115,6 @@ public static function handle(array $properties = array(), $baseUrl = '/')
{
$exceptionThrown = null;
$filtersPassed = true;
$session = HttpSession::getSession();
$container = ContainerImpl::getInstance($properties);
self::$_logger = \Logger::getLogger(__CLASS__);
$baseUrlLen = strlen($baseUrl);
Expand All @@ -129,6 +127,8 @@ public static function handle(array $properties = array(), $baseUrl = '/')
$viewResolver = $container->getBean('HttpViewResolver');
$exceptionMapper = $container->getBean('HttpExceptionMapper');
$render = $container->getBean('HttpViewRender');
$session = $container->getBean('SessionHandler');

$method = strtolower($_SERVER['REQUEST_METHOD']);
$url = $_SERVER['REQUEST_URI'];
$urlStart = strpos($url, $baseUrl);
Expand Down
25 changes: 13 additions & 12 deletions src/mg/Ding/Mvc/Http/HttpViewRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@
*/
namespace Ding\Mvc\Http;

use Ding\HttpSession\HttpSession;

use Ding\MessageSource\IMessageSource;

use Ding\MessageSource\IMessageSourceAware;
use Ding\Mvc\IViewRender;
use Ding\Mvc\View;
use Ding\Container\Impl\ContainerImpl;

/**
* Http view render.
Expand All @@ -60,16 +59,18 @@ public function setMessageSource(IMessageSource $messageSource)

public function translate($bundle, $message, $arguments = array())
{
$session = HttpSession::getSession();
if (!$session->hasAttribute('LANGUAGE')) {
return $this->messageSource->getMessage(
$bundle, $message, $arguments
);
} else {
return $this->messageSource->getMessage(
$bundle, $message, $arguments, $session->getAttribute('LANGUAGE')
);
}
$container = ContainerImpl::getInstance();
$session = $container->getBean('SessionHandler');

if (!$session->hasAttribute('LANGUAGE')) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Identation seems to be wrong here, can you make it 4 spaces? :)

return $this->messageSource->getMessage(
$bundle, $message, $arguments
);
} else {
return $this->messageSource->getMessage(
$bundle, $message, $arguments, $session->getAttribute('LANGUAGE')
);
}
}

/**
Expand Down
29 changes: 24 additions & 5 deletions test/httpsession/Test_HttpSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
*/

use Ding\HttpSession\HttpSession;
use Ding\Container\Impl\ContainerImpl;

/**
* This class will test the HttpSession.
Expand All @@ -43,21 +43,39 @@
*/
class Test_HttpSession extends PHPUnit_Framework_TestCase
{
private $_properties = array();

public function setUp()
{
$this->_properties = array(
'ding' => array(
'log4php.properties' => RESOURCES_DIR . DIRECTORY_SEPARATOR . 'log4php.properties',
'factory' => array(
'bdef' => array(
'xml' => array('filename' => 'sesionHandler.xml', 'directories' => array(RESOURCES_DIR))
)
)
)
);
}

/**
* @test
*/
public function can_use_session()
{
$session = HttpSession::getSession();
$session->destroy();
$container = ContainerImpl::getInstance($this->_properties);
$session = $container->getBean("SesionHandler");
$session->destroy();
}

/**
* @test
*/
public function can_return_false_on_invalid_attribute()
{
$session = HttpSession::getSession();
$container = ContainerImpl::getInstance($this->_properties);
$session = $container->getBean("SesionHandler");
$this->assertFalse($session->getAttribute('notexistant'));
}

Expand All @@ -66,7 +84,8 @@ public function can_return_false_on_invalid_attribute()
*/
public function can_use_attributes()
{
$session = HttpSession::getSession();
$container = ContainerImpl::getInstance($this->_properties);
$session = $container->getBean("SesionHandler");
$session->setAttribute('foo', 'bar');
$this->assertEquals($session->getAttribute('foo'), 'bar');
$this->assertTrue($session->hasAttribute('foo'));
Expand Down
4 changes: 4 additions & 0 deletions test/resources/sesionHandler.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="sesionHandler" scope="singleton" class="Ding\HttpSession\HttpSession" />
</beans>