-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13538 from niden/T12833-standardize-session-inter…
…face Reorganized/cleaned up Session classes
- Loading branch information
Showing
10 changed files
with
412 additions
and
510 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,10 @@ | ||
|
||
/* | ||
+------------------------------------------------------------------------+ | ||
| Phalcon Framework | | ||
+------------------------------------------------------------------------+ | ||
| Copyright (c) 2011-2017 Phalcon Team (https://phalconphp.com) | | ||
+------------------------------------------------------------------------+ | ||
| This source file is subject to the New BSD License that is bundled | | ||
| with this package in the file LICENSE.txt. | | ||
| | | ||
| If you did not receive a copy of the license and are unable to | | ||
| obtain it through the world-wide-web, please send an email | | ||
| to [email protected] so we can send you a copy immediately. | | ||
+------------------------------------------------------------------------+ | ||
| Authors: Andres Gutierrez <[email protected]> | | ||
| Eduar Carvajal <[email protected]> | | ||
+------------------------------------------------------------------------+ | ||
/** | ||
* This file is part of the Phalcon. | ||
* | ||
* (c) Phalcon Team <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Phalcon\Session; | ||
|
@@ -28,15 +18,15 @@ abstract class Adapter implements AdapterInterface | |
{ | ||
const SESSION_ACTIVE = 2; | ||
|
||
const SESSION_NONE = 1; | ||
|
||
const SESSION_DISABLED = 0; | ||
|
||
protected _uniqueId; | ||
const SESSION_NONE = 1; | ||
|
||
protected _options; | ||
|
||
protected _started = false; | ||
|
||
protected _options; | ||
protected _uniqueId; | ||
|
||
/** | ||
* Phalcon\Session\Adapter constructor | ||
|
@@ -46,74 +36,71 @@ abstract class Adapter implements AdapterInterface | |
this->setOptions(options); | ||
} | ||
|
||
/** | ||
* Starts the session (if headers are already sent the session will not be started) | ||
*/ | ||
public function start() -> boolean | ||
public function __destruct() | ||
{ | ||
if !headers_sent() { | ||
if !this->_started && this->status() !== self::SESSION_ACTIVE { | ||
session_start(); | ||
let this->_started = true; | ||
return true; | ||
} | ||
if this->_started { | ||
session_write_close(); | ||
let this->_started = false; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Sets session's options | ||
* | ||
*<code> | ||
* $session->setOptions( | ||
* [ | ||
* "uniqueId" => "my-private-app", | ||
* ] | ||
* ); | ||
*</code> | ||
* Alias: Gets a session variable from an application context | ||
*/ | ||
public function setOptions(array! options) | ||
public function __get(string index) -> var | ||
{ | ||
var uniqueId; | ||
|
||
if fetch uniqueId, options["uniqueId"] { | ||
let this->_uniqueId = uniqueId; | ||
} | ||
|
||
let this->_options = options; | ||
return this->get(index); | ||
} | ||
|
||
/** | ||
* Get internal options | ||
* Alias: Check whether a session variable is set in an application context | ||
*/ | ||
public function getOptions() -> array | ||
public function __isset(string index) -> boolean | ||
{ | ||
return this->_options; | ||
return this->has(index); | ||
} | ||
|
||
/** | ||
* Set session name | ||
* Alias: Sets a session variable in an application context | ||
*/ | ||
public function setName(string name) | ||
public function __set(string index, var value) | ||
{ | ||
session_name(name); | ||
return this->set(index, value); | ||
} | ||
|
||
/** | ||
* Get session name | ||
* Alias: Removes a session variable from an application context | ||
* | ||
* <code> | ||
* unset($session->auth); | ||
* </code> | ||
*/ | ||
public function getName() -> string | ||
public function __unset(string index) | ||
{ | ||
return session_name(); | ||
this->remove(index); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* Destroys the active session | ||
* | ||
*<code> | ||
* var_dump( | ||
* $session->destroy() | ||
* ); | ||
* | ||
* var_dump( | ||
* $session->destroy(true) | ||
* ); | ||
*</code> | ||
*/ | ||
public function regenerateId(bool deleteOldSession = true) -> <Adapter> | ||
public function destroy(boolean removeData = false) -> boolean | ||
{ | ||
session_regenerate_id(deleteOldSession); | ||
return this; | ||
if removeData { | ||
this->removeSessionData(); | ||
} | ||
|
||
let this->_started = false; | ||
return session_destroy(); | ||
} | ||
|
||
/** | ||
|
@@ -145,23 +132,31 @@ abstract class Adapter implements AdapterInterface | |
} | ||
|
||
/** | ||
* Sets a session variable in an application context | ||
* Returns active session id | ||
* | ||
*<code> | ||
* $session->set("auth", "yes"); | ||
* echo $session->getId(); | ||
*</code> | ||
*/ | ||
public function set(string index, var value) | ||
public function getId() -> string | ||
{ | ||
var uniqueId; | ||
return session_id(); | ||
} | ||
|
||
let uniqueId = this->_uniqueId; | ||
if !empty uniqueId { | ||
let _SESSION[uniqueId . "#" . index] = value; | ||
return; | ||
} | ||
/** | ||
* Get session name | ||
*/ | ||
public function getName() -> string | ||
{ | ||
return session_name(); | ||
} | ||
|
||
let _SESSION[index] = value; | ||
/** | ||
* Get internal options | ||
*/ | ||
public function getOptions() -> array | ||
{ | ||
return this->_options; | ||
} | ||
|
||
/** | ||
|
@@ -185,6 +180,29 @@ abstract class Adapter implements AdapterInterface | |
return isset _SESSION[index]; | ||
} | ||
|
||
/** | ||
* Check whether the session has been started | ||
* | ||
*<code> | ||
* var_dump( | ||
* $session->isStarted() | ||
* ); | ||
*</code> | ||
*/ | ||
public function isStarted() -> boolean | ||
{ | ||
return this->_started; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function regenerateId(bool deleteOldSession = true) -> <Adapter> | ||
{ | ||
session_regenerate_id(deleteOldSession); | ||
return this; | ||
} | ||
|
||
/** | ||
* Removes a session variable from an application context | ||
* | ||
|
@@ -206,15 +224,23 @@ abstract class Adapter implements AdapterInterface | |
} | ||
|
||
/** | ||
* Returns active session id | ||
* Sets a session variable in an application context | ||
* | ||
*<code> | ||
* echo $session->getId(); | ||
* $session->set("auth", "yes"); | ||
*</code> | ||
*/ | ||
public function getId() -> string | ||
public function set(string index, var value) | ||
{ | ||
return session_id(); | ||
var uniqueId; | ||
|
||
let uniqueId = this->_uniqueId; | ||
if !empty uniqueId { | ||
let _SESSION[uniqueId . "#" . index] = value; | ||
return; | ||
} | ||
|
||
let _SESSION[index] = value; | ||
} | ||
|
||
/** | ||
|
@@ -230,40 +256,48 @@ abstract class Adapter implements AdapterInterface | |
} | ||
|
||
/** | ||
* Check whether the session has been started | ||
* | ||
*<code> | ||
* var_dump( | ||
* $session->isStarted() | ||
* ); | ||
*</code> | ||
* Set session name | ||
*/ | ||
public function isStarted() -> boolean | ||
public function setName(string name) | ||
{ | ||
return this->_started; | ||
session_name(name); | ||
} | ||
|
||
/** | ||
* Destroys the active session | ||
* Sets session's options | ||
* | ||
*<code> | ||
* var_dump( | ||
* $session->destroy() | ||
* ); | ||
* | ||
* var_dump( | ||
* $session->destroy(true) | ||
* $session->setOptions( | ||
* [ | ||
* "uniqueId" => "my-private-app", | ||
* ] | ||
* ); | ||
*</code> | ||
*/ | ||
public function destroy(boolean removeData = false) -> boolean | ||
public function setOptions(array! options) | ||
{ | ||
if removeData { | ||
this->removeSessionData(); | ||
var uniqueId; | ||
|
||
if fetch uniqueId, options["uniqueId"] { | ||
let this->_uniqueId = uniqueId; | ||
} | ||
|
||
let this->_started = false; | ||
return session_destroy(); | ||
let this->_options = options; | ||
} | ||
|
||
/** | ||
* Starts the session (if headers are already sent the session will not be started) | ||
*/ | ||
public function start() -> boolean | ||
{ | ||
if !headers_sent() { | ||
if !this->_started && this->status() !== self::SESSION_ACTIVE { | ||
session_start(); | ||
let this->_started = true; | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
|
@@ -297,49 +331,8 @@ abstract class Adapter implements AdapterInterface | |
} | ||
|
||
/** | ||
* Alias: Gets a session variable from an application context | ||
*/ | ||
public function __get(string index) -> var | ||
{ | ||
return this->get(index); | ||
} | ||
|
||
/** | ||
* Alias: Sets a session variable in an application context | ||
*/ | ||
public function __set(string index, var value) | ||
{ | ||
return this->set(index, value); | ||
} | ||
|
||
/** | ||
* Alias: Check whether a session variable is set in an application context | ||
* Helper method to remove session data | ||
*/ | ||
public function __isset(string index) -> boolean | ||
{ | ||
return this->has(index); | ||
} | ||
|
||
/** | ||
* Alias: Removes a session variable from an application context | ||
* | ||
* <code> | ||
* unset($session->auth); | ||
* </code> | ||
*/ | ||
public function __unset(string index) | ||
{ | ||
this->remove(index); | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
if this->_started { | ||
session_write_close(); | ||
let this->_started = false; | ||
} | ||
} | ||
|
||
protected function removeSessionData() -> void | ||
{ | ||
var uniqueId, key; | ||
|
Oops, something went wrong.