From 49840a8981153bbdd0e0e06fbcf84e95a9ab5ca9 Mon Sep 17 00:00:00 2001 From: iaejean Date: Fri, 25 Jul 2014 11:20:29 -0500 Subject: [PATCH 1/2] 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') + ); + } } /** From 228548d2aa57b7826b350669c7d245dd2836dfb1 Mon Sep 17 00:00:00 2001 From: Israel hernandez Date: Wed, 3 Sep 2014 12:32:52 -0500 Subject: [PATCH 2/2] Sesion handler & test --- src/mg/Ding/HttpSession/IHttpSession.php | 3 +- src/mg/Ding/Mvc/Http/HttpFrontController.php | 14 ++-------- src/mg/Ding/Mvc/Http/HttpViewRender.php | 15 ++-------- test/httpsession/Test_HttpSession.php | 29 ++++++++++++++++---- test/resources/sesionHandler.xml | 4 +++ 5 files changed, 33 insertions(+), 32 deletions(-) create mode 100644 test/resources/sesionHandler.xml diff --git a/src/mg/Ding/HttpSession/IHttpSession.php b/src/mg/Ding/HttpSession/IHttpSession.php index c9935dfe..21ed0b62 100644 --- a/src/mg/Ding/HttpSession/IHttpSession.php +++ b/src/mg/Ding/HttpSession/IHttpSession.php @@ -3,7 +3,6 @@ interface IHttpSession { - public function destroy(); public function hasAttribute($name); @@ -12,5 +11,5 @@ public function getAttribute($name); public function setAttribute($name, $value); - public static function getSession(); + public function getSession(); } diff --git a/src/mg/Ding/Mvc/Http/HttpFrontController.php b/src/mg/Ding/Mvc/Http/HttpFrontController.php index 00deedf5..c1fdf2d8 100644 --- a/src/mg/Ding/Mvc/Http/HttpFrontController.php +++ b/src/mg/Ding/Mvc/Http/HttpFrontController.php @@ -29,8 +29,6 @@ namespace Ding\Mvc\Http; 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; @@ -117,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); @@ -130,15 +127,8 @@ public static function handle(array $properties = array(), $baseUrl = '/') $viewResolver = $container->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"); - + $session = $container->getBean('SessionHandler'); + $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 204a229d..e6b79db4 100644 --- a/src/mg/Ding/Mvc/Http/HttpViewRender.php +++ b/src/mg/Ding/Mvc/Http/HttpViewRender.php @@ -27,16 +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; -use Ding\HttpSession\Exception\HttpSessionException; -use Ding\Bean\Factory\Exception\BeanFactoryException; /** * Http view render. @@ -63,16 +59,9 @@ public function setMessageSource(IMessageSource $messageSource) public function translate($bundle, $message, $arguments = array()) { - $container = ContainerImpl::getInstance(); - try { - $session = $container->getBean('SessionHandler'); - } catch(BeanFactoryException $e) { - $session = HttpSession::getSession(); - } + $container = ContainerImpl::getInstance(); + $session = $container->getBean('SessionHandler'); - 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 diff --git a/test/httpsession/Test_HttpSession.php b/test/httpsession/Test_HttpSession.php index efb88115..5b2d8055 100644 --- a/test/httpsession/Test_HttpSession.php +++ b/test/httpsession/Test_HttpSession.php @@ -27,7 +27,7 @@ * */ -use Ding\HttpSession\HttpSession; +use Ding\Container\Impl\ContainerImpl; /** * This class will test the HttpSession. @@ -43,13 +43,30 @@ */ 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(); } /** @@ -57,7 +74,8 @@ public function can_use_session() */ 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')); } @@ -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')); diff --git a/test/resources/sesionHandler.xml b/test/resources/sesionHandler.xml new file mode 100644 index 00000000..c400ac2f --- /dev/null +++ b/test/resources/sesionHandler.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file