Skip to content

SessionHandler #119

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

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
{
}
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
16 changes: 16 additions & 0 deletions src/mg/Ding/HttpSession/IHttpSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace Ding\HttpSession;

interface IHttpSession
{

public function destroy();

public function hasAttribute($name);

public function getAttribute($name);

public function setAttribute($name, $value);

public static 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.

a static function in an interface it's not a good idea, it should be an instance method

}
12 changes: 11 additions & 1 deletion src/mg/Ding/Mvc/Http/HttpFrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

use Ding\Mvc\IViewRender;
use Ding\HttpSession\HttpSession;
use Ding\HttpSession\Exception\HttpSessionException;
use Ding\Mvc\IMapper;
use Ding\Mvc\Exception\MvcException;
use Ding\Mvc\ModelAndView;
Expand Down Expand Up @@ -116,7 +117,7 @@ public static function handle(array $properties = array(), $baseUrl = '/')
{
$exceptionThrown = null;
$filtersPassed = true;
$session = HttpSession::getSession();
//$session = HttpSession::getSession();
Copy link
Owner

Choose a reason for hiding this comment

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

unused code should be removed, not commented

$container = ContainerImpl::getInstance($properties);
self::$_logger = \Logger::getLogger(__CLASS__);
$baseUrlLen = strlen($baseUrl);
Expand All @@ -129,6 +130,15 @@ public static function handle(array $properties = array(), $baseUrl = '/')
$viewResolver = $container->getBean('HttpViewResolver');
$exceptionMapper = $container->getBean('HttpExceptionMapper');
$render = $container->getBean('HttpViewRender');
try {
Copy link
Owner

Choose a reason for hiding this comment

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

please fix the identation levels, we're using 4 spaces

$session = $container->getBean('SessionHandler');
Copy link
Owner

Choose a reason for hiding this comment

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

session should be declared out of the try-catch block

} catch(BeanFactoryException $e) {
$session = HttpSession::getSession();
}

if (!method_exists($session, "getSession") || !method_exists($session, "getAttribute") )
Copy link
Owner

Choose a reason for hiding this comment

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

this is not needed. also, the right check would be to check if the object implements the right interface, not just these methods. but let's remove this check

throw new HttpSessionException("Session Handler must implement IHttpSession Interface");

$method = strtolower($_SERVER['REQUEST_METHOD']);
$url = $_SERVER['REQUEST_URI'];
$urlStart = strpos($url, $baseUrl);
Expand Down
32 changes: 22 additions & 10 deletions src/mg/Ding/Mvc/Http/HttpViewRender.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
use Ding\MessageSource\IMessageSourceAware;
use Ding\Mvc\IViewRender;
use Ding\Mvc\View;
use Ding\Container\Impl\ContainerImpl;
use Ding\HttpSession\Exception\HttpSessionException;
use Ding\Bean\Factory\Exception\BeanFactoryException;

/**
* Http view render.
Expand All @@ -60,16 +63,25 @@ 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();
Copy link
Owner

Choose a reason for hiding this comment

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

this change is not needed, let's only change line 63 to get the bean from the container and nothing more

try {
$session = $container->getBean('SessionHandler');
} catch(BeanFactoryException $e) {
$session = HttpSession::getSession();
}

if (!method_exists($session, "getSession") || !method_exists($session, "getAttribute") )
throw new HttpSessionException("Session Handler must implement IHttpSession Interface");

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

/**
Expand Down