From 49840a8981153bbdd0e0e06fbcf84e95a9ab5ca9 Mon Sep 17 00:00:00 2001 From: iaejean Date: Fri, 25 Jul 2014 11:20:29 -0500 Subject: [PATCH] Feature allow create a custom Session Handler --- .../Exception/HttpSessionException.php | 6 ++++ src/mg/Ding/HttpSession/HttpSession.php | 2 +- src/mg/Ding/HttpSession/IHttpSession.php | 16 ++++++++++ src/mg/Ding/Mvc/Http/HttpFrontController.php | 12 ++++++- src/mg/Ding/Mvc/Http/HttpViewRender.php | 32 +++++++++++++------ 5 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 src/mg/Ding/HttpSession/Exception/HttpSessionException.php create mode 100644 src/mg/Ding/HttpSession/IHttpSession.php diff --git a/src/mg/Ding/HttpSession/Exception/HttpSessionException.php b/src/mg/Ding/HttpSession/Exception/HttpSessionException.php new file mode 100644 index 00000000..510e0a05 --- /dev/null +++ b/src/mg/Ding/HttpSession/Exception/HttpSessionException.php @@ -0,0 +1,6 @@ +getBean('HttpViewResolver'); $exceptionMapper = $container->getBean('HttpExceptionMapper'); $render = $container->getBean('HttpViewRender'); + 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"); + $method = strtolower($_SERVER['REQUEST_METHOD']); $url = $_SERVER['REQUEST_URI']; $urlStart = strpos($url, $baseUrl); diff --git a/src/mg/Ding/Mvc/Http/HttpViewRender.php b/src/mg/Ding/Mvc/Http/HttpViewRender.php index ac8f0e41..204a229d 100644 --- a/src/mg/Ding/Mvc/Http/HttpViewRender.php +++ b/src/mg/Ding/Mvc/Http/HttpViewRender.php @@ -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(); + 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') + ); + } } /**