-
Notifications
You must be signed in to change notification settings - Fork 26
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
base: master
Are you sure you want to change the base?
SessionHandler #119
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
namespace Ding\HttpSession\Exception; | ||
|
||
class HttpSessionException extends \Exception | ||
{ | ||
} |
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(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -116,7 +117,7 @@ public static function handle(array $properties = array(), $baseUrl = '/') | |
{ | ||
$exceptionThrown = null; | ||
$filtersPassed = true; | ||
$session = HttpSession::getSession(); | ||
//$session = HttpSession::getSession(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
); | ||
} | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
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