From 3f803219e784b53f363b5ef25b3be00fd310fb1c Mon Sep 17 00:00:00 2001 From: xue <> Date: Tue, 24 Jan 2006 23:21:08 +0000 Subject: Modified THttpRequest and THttpSession so that they can be used like an array. Items properties are removed as a result. --- demos/personal/protected/Pages/UserLogin.php | 2 +- .../quickstart/protected/controls/SampleLayout.php | 2 +- framework/Security/TAuthManager.php | 4 +- framework/Web/THttpRequest.php | 66 ++++-- framework/Web/THttpSession.php | 255 +++++++++++++++++---- framework/Web/UI/TPage.php | 2 +- framework/Web/UI/TPageStatePersister.php | 2 +- 7 files changed, 265 insertions(+), 68 deletions(-) diff --git a/demos/personal/protected/Pages/UserLogin.php b/demos/personal/protected/Pages/UserLogin.php index 0dd2cd79..ba33f1fa 100644 --- a/demos/personal/protected/Pages/UserLogin.php +++ b/demos/personal/protected/Pages/UserLogin.php @@ -13,7 +13,7 @@ class UserLogin extends TPage { parent::onLoadComplete($param); if($this->IsPostBack && $this->IsValid) - $this->Response->redirect($this->Request->Items['ReturnUrl']); + $this->Response->redirect($this->Request['ReturnUrl']); } } diff --git a/demos/quickstart/protected/controls/SampleLayout.php b/demos/quickstart/protected/controls/SampleLayout.php index 94b7ac74..2224572a 100644 --- a/demos/quickstart/protected/controls/SampleLayout.php +++ b/demos/quickstart/protected/controls/SampleLayout.php @@ -5,7 +5,7 @@ class SampleLayout extends TTemplateControl public function __construct() { - if($this->Request->Items->contains('functionaltest')) + if(isset($this->Request['functionaltest'])) $this->Service->RequestedPage->EnableTheming=false; parent::__construct(); } diff --git a/framework/Security/TAuthManager.php b/framework/Security/TAuthManager.php index ca0be56e..c46e32c1 100644 --- a/framework/Security/TAuthManager.php +++ b/framework/Security/TAuthManager.php @@ -194,7 +194,7 @@ class TAuthManager extends TModule if(($session=$application->getSession())===null) throw new TConfigurationException('authmanager_session_required'); $session->open(); - $sessionInfo=$session->getItems()->itemAt($this->generateUserSessionKey()); + $sessionInfo=$session->itemAt($this->generateUserSessionKey()); $user=$this->_userManager->getUser(null)->loadFromString($sessionInfo); $application->setUser($user); } @@ -238,7 +238,7 @@ class TAuthManager extends TModule if(($session=$this->getSession())===null) throw new TConfigurationException('authmanager_session_required'); else - $session->getItems()->add($this->generateUserSessionKey(),$user->saveToString()); + $session->add($this->generateUserSessionKey(),$user->saveToString()); } } diff --git a/framework/Web/THttpRequest.php b/framework/Web/THttpRequest.php index d397cc56..2a3a35aa 100644 --- a/framework/Web/THttpRequest.php +++ b/framework/Web/THttpRequest.php @@ -16,7 +16,27 @@ * THttpRequest provides storage and access scheme for user request sent via HTTP. * It also encapsulates a uniform way to parse and construct URLs. * - * To retrieve user POST and GET variables, use {@link getItems()} method. + * User post data can be retrieved from THttpRequest by using it like an associative array. + * For example, to test if a user supplies a variable named 'param1', you can use, + * + * if(isset($request['param1'])) ... + * // equivalent to: + * // if($request->contains('param1')) ... + * + * To get the value of 'param1', use, + * + * $value=$request['param1']; + * // equivalent to: + * // $value=$request->itemAt('param1'); + * + * To traverse the user post data, use + * + * foreach($request as $name=>$value) ... + * + * Note, POST and GET variables are merged together in THttpRequest. + * If a variable name appears in both POST and GET data, then POST data + * takes precedence. + * * To construct a URL that can be recognized by Prado, use {@link constructUrl()}. * THttpRequest also provides the cookies sent by the user, user information such * as his browser capabilities, accepted languages, etc. @@ -29,7 +49,7 @@ * @package System.Web * @since 3.0 */ -class THttpRequest extends TModule +class THttpRequest extends TMap implements IModule { /** * GET variable name to store service information @@ -59,13 +79,29 @@ class THttpRequest extends TModule * @var string path info of URL */ private $_pathInfo; - /** - * @var TMap list of input variables (including GET and POST) - */ - private $_items; private $_services; private $_requestResolved=false; + /** + * @var string module id + */ + private $_id; + + /** + * @return string id of this module + */ + public function getID() + { + return $this->_id; + } + + /** + * @param string id of this module + */ + public function setID($value) + { + $this->_id=$value; + } /** * Initializes the module. @@ -104,7 +140,7 @@ class THttpRequest extends TModule $_COOKIE=$this->stripSlashes($_COOKIE); } - $this->_items=new TMap(array_merge($_POST,$_GET)); + $this->copyfrom(array_merge($_GET,$_POST)); $this->_initialized=true; $this->getApplication()->setRequest($this); @@ -279,22 +315,6 @@ class THttpRequest extends TModule return Prado::getUserLanguages(); } - /** - * @return TMap list of input variables, include GET, POST - */ - public function getItems() - { - return $this->_items; - } - - /** - * @return TMap list of input variables, include GET, POST - */ - public function getParameters() - { - return $this->_items; - } - /** * @return THttpCookieCollection list of cookies to be sent */ diff --git a/framework/Web/THttpSession.php b/framework/Web/THttpSession.php index 7dfc708d..8d35b008 100644 --- a/framework/Web/THttpSession.php +++ b/framework/Web/THttpSession.php @@ -18,13 +18,14 @@ * to destroy the session, call {@destroy}. If AutoStart is true, then the session * will be started once the session module is loaded and initialized. * - * To access data stored in session, use the Items property. For example, + * To access data stored in session, use THttpSession like an associative array. For example, * * $session=new THttpSession; * $session->open(); - * foreach($session->Items as $key=>$value) - * ; // read data in session - * $session->Items['key']=$data; // store new data into session + * $value1=$session['name1']; // get session variable 'name1' + * $value2=$session['name2']; // get session variable 'name2' + * foreach($session as $name=>$value) // traverse all session variables + * $session['name3']=$value3; // set session variable 'name3' * * * The following configurations are available for session: @@ -54,12 +55,8 @@ * @package System.Web * @since 3.0 */ -class THttpSession extends TModule +class THttpSession extends TComponent implements IteratorAggregate,ArrayAccess,IModule { - /** - * @var THttpSessionCollection list of session variables - */ - private $_items; /** * @var boolean whether this module has been initialized */ @@ -76,6 +73,26 @@ class THttpSession extends TModule * @var THttpCookie cookie to be used to store session ID and other data */ private $_cookie=null; + /** + * @var string module id + */ + private $_id; + + /** + * @return string id of this module + */ + public function getID() + { + return $this->_id; + } + + /** + * @param string id of this module + */ + public function setID($value) + { + $this->_id=$value; + } /** * Initializes the module. @@ -129,16 +146,6 @@ class THttpSession extends TModule } } - /** - * @return THttpSessionCollection list of session variables - */ - public function getItems() - { - if($this->_items===null) - $this->_items=new THttpSessionCollection($_SESSION); - return $this->_items; - } - /** * @return boolean whether the session has started */ @@ -467,53 +474,223 @@ class THttpSession extends TModule { return true; } + + //------ The following methods enable THttpSession to be TMap-like ----- + + /** + * Returns an iterator for traversing the session variables. + * This method is required by the interface IteratorAggregate. + * @return TSessionIterator an iterator for traversing the session variables. + */ + public function getIterator() + { + return new TSessionIterator; + } + + /** + * @return integer the number of session variables + */ + public function getCount() + { + return count($_SESSION); + } + + /** + * @return array the list of session variable names + */ + public function getKeys() + { + return array_keys($_SESSION); + } + + /** + * Returns the session variable value with the session variable name. + * This method is exactly the same as {@link offsetGet}. + * @param mixed the session variable name + * @return mixed the session variable value, null if no such variable exists + */ + public function itemAt($key) + { + return isset($_SESSION[$key]) ? $_SESSION[$key] : null; + } + + /** + * Adds a session variable. + * Note, if the specified name already exists, the old value will be removed first. + * @param mixed session variable name + * @param mixed session variable value + */ + public function add($key,$value) + { + $_SESSION[$key]=$value; + } + + /** + * Removes a session variable. + * @param mixed the name of the session variable to be removed + * @return mixed the removed value, null if no such session variable. + */ + public function remove($key) + { + if(isset($_SESSION[$key])) + { + $value=$_SESSION[$key]; + unset($_SESSION[$key]); + return $value; + } + else + return null; + } + + /** + * Removes all session variables + */ + public function clear() + { + foreach(array_keys($_SESSION) as $key) + unset($_SESSION[$key]); + } + + /** + * @param mixed session variable name + * @return boolean whether there is the named session variable + */ + public function contains($key) + { + return isset($_SESSION[$key]); + } + + /** + * @return array the list of all session variables in array + */ + public function toArray() + { + return $_SESSION; + } + + /** + * This method is required by the interface ArrayAccess. + * @param mixed the offset to check on + * @return boolean + */ + public function offsetExists($offset) + { + return isset($_SESSION[$offset]); + } + + /** + * This method is required by the interface ArrayAccess. + * @param integer the offset to retrieve element. + * @return mixed the element at the offset, null if no element is found at the offset + */ + public function offsetGet($offset) + { + return isset($_SESSION[$offset]) ? $_SESSION[$offset] : null; + } + + /** + * This method is required by the interface ArrayAccess. + * @param integer the offset to set element + * @param mixed the element value + */ + public function offsetSet($offset,$item) + { + $_SESSION[$offset]=$item; + } + + /** + * This method is required by the interface ArrayAccess. + * @param mixed the offset to unset element + */ + public function offsetUnset($offset) + { + unset($_SESSION[$offset]); + } } /** - * THttpSessionCollection class. + * TSessionIterator class * - * THttpSessionCollection implements a collection class to store session data items. + * TSessionIterator implements Iterator interface. + * + * TSessionIterator is used by THttpSession. It allows THttpSession to return a new iterator + * for traversing the session variables. * * @author Qiang Xue * @version $Revision: $ $Date: $ - * @package System.Web + * @package System.Web.UI * @since 3.0 */ -class THttpSessionCollection extends TMap +class TSessionIterator implements Iterator { /** - * @var boolean whether the initial session data has been loaded into the collection + * @var array list of keys in the map */ - private $_initialized=false; + private $_keys; + /** + * @var mixed current key + */ + private $_key; /** * Constructor. - * Initializes the list with an array or an iterable object. - * @param array|Iterator the intial data. + * @param array the data to be iterated through */ - public function __construct($data=null) + public function __construct() { - parent::__construct($data); - $this->_initialized=true; + $this->_keys=array_keys($_SESSION); } /** - * Adds the item into session. - * This method will be invoked whenever an item is added to the collection. + * Rewinds internal array pointer. + * This method is required by the interface Iterator. */ - protected function addedItem($key,$value) + public function rewind() { - if($this->_initialized) - $_SESSION[$key]=$value; + $this->_key=reset($this->_keys); + } + + /** + * Returns the key of the current array element. + * This method is required by the interface Iterator. + * @return mixed the key of the current array element + */ + public function key() + { + return $this->_key; + } + + /** + * Returns the current array element. + * This method is required by the interface Iterator. + * @return mixed the current array element + */ + public function current() + { + return isset($_SESSION[$this->_key])?$_SESSION[$this->_key]:null; + } + + /** + * Moves the internal pointer to the next array element. + * This method is required by the interface Iterator. + */ + public function next() + { + do + { + $this->_key=next($this->_keys); + } + while(!isset($_SESSION[$this->_key]) && $this->_key!==false); } /** - * Removes the item from session. - * This method will be invoked whenever an item is removed from the collection. + * Returns whether there is an element at current position. + * This method is required by the interface Iterator. + * @return boolean */ - protected function removedItem($key,$value) + public function valid() { - unset($_SESSION[$key]); + return $this->_key!==false; } } ?> \ No newline at end of file diff --git a/framework/Web/UI/TPage.php b/framework/Web/UI/TPage.php index f4d5fa99..70439237 100644 --- a/framework/Web/UI/TPage.php +++ b/framework/Web/UI/TPage.php @@ -489,7 +489,7 @@ class TPage extends TTemplateControl */ private function determinePostBackMode() { - $postData=$this->getApplication()->getRequest()->getItems(); + $postData=$this->getRequest(); if($postData->contains(self::FIELD_PAGESTATE) || $postData->contains(self::FIELD_POSTBACK_TARGET)) $this->_postData=$postData; } diff --git a/framework/Web/UI/TPageStatePersister.php b/framework/Web/UI/TPageStatePersister.php index bc65f74a..4ece9a09 100644 --- a/framework/Web/UI/TPageStatePersister.php +++ b/framework/Web/UI/TPageStatePersister.php @@ -70,7 +70,7 @@ class TPageStatePersister extends TModule implements IStatePersister public function load() { Prado::trace("Loading state",'System.Web.UI.TPageStatePersister'); - $str=base64_decode($this->getApplication()->getRequest()->getItems()->itemAt(TPage::FIELD_PAGESTATE)); + $str=base64_decode($this->getRequest()->itemAt(TPage::FIELD_PAGESTATE)); if($str==='') return null; if(extension_loaded('zlib')) -- cgit v1.2.3