Merge "Clean up postgres connection handling"
[lhc/web/wiklou.git] / includes / session / PHPSessionHandler.php
index 795e253..695ce5a 100644 (file)
@@ -28,13 +28,10 @@ use BagOStuff;
 
 /**
  * Adapter for PHP's session handling
- * @todo Once we drop support for PHP < 5.4, use SessionHandlerInterface
- *  (should just be a matter of adding "implements SessionHandlerInterface" and
- *  changing the session_set_save_handler() call).
  * @ingroup Session
  * @since 1.27
  */
-class PHPSessionHandler {
+class PHPSessionHandler implements \SessionHandlerInterface {
        /** @var PHPSessionHandler */
        protected static $instance = null;
 
@@ -52,7 +49,7 @@ class PHPSessionHandler {
        protected $logger;
 
        /** @var array Track original session fields for later modification check */
-       protected $sessionFieldCache = array();
+       protected $sessionFieldCache = [];
 
        protected function __construct( SessionManager $manager ) {
                $this->setEnableFlags(
@@ -114,6 +111,12 @@ class PHPSessionHandler {
                        return;
                }
 
+               // @codeCoverageIgnoreStart
+               if ( defined( 'MW_NO_SESSION_HANDLER' ) ) {
+                       throw new \BadMethodCallException( 'MW_NO_SESSION_HANDLER is defined' );
+               }
+               // @codeCoverageIgnoreEnd
+
                self::$instance = new self( $manager );
 
                // Close any auto-started session, before we replace it
@@ -132,20 +135,9 @@ class PHPSessionHandler {
                // Also set a sane serialization handler
                \Wikimedia\PhpSessionSerializer::setSerializeHandler();
 
-               session_set_save_handler(
-                       array( self::$instance, 'open' ),
-                       array( self::$instance, 'close' ),
-                       array( self::$instance, 'read' ),
-                       array( self::$instance, 'write' ),
-                       array( self::$instance, 'destroy' ),
-                       array( self::$instance, 'gc' )
-               );
-
-               // It's necessary to register a shutdown function to call session_write_close(),
-               // because by the time the request shutdown function for the session module is
-               // called, other needed objects may have already been destroyed. Shutdown functions
-               // registered this way are called before object destruction.
-               register_shutdown_function( array( self::$instance, 'handleShutdown' ) );
+               // Register this as the save handler, and register an appropriate
+               // shutdown function.
+               session_set_save_handler( self::$instance, true );
        }
 
        /**
@@ -196,7 +188,7 @@ class PHPSessionHandler {
                if ( self::$instance !== $this ) {
                        throw new \UnexpectedValueException( __METHOD__ . ': Wrong instance called!' );
                }
-               $this->sessionFieldCache = array();
+               $this->sessionFieldCache = [];
                return true;
        }
 
@@ -248,9 +240,9 @@ class PHPSessionHandler {
                        // invalid. Let's emit a log warning instead of a PHP warning.
                        $this->logger->warning(
                                __METHOD__ . ': Session "{session}" cannot be loaded, skipping write.',
-                               array(
+                               [
                                        'session' => $id,
-                       ) );
+                       ] );
                        return true;
                }
 
@@ -264,7 +256,7 @@ class PHPSessionHandler {
 
                // Now merge the data into the Session object.
                $changed = false;
-               $cache = isset( $this->sessionFieldCache[$id] ) ? $this->sessionFieldCache[$id] : array();
+               $cache = isset( $this->sessionFieldCache[$id] ) ? $this->sessionFieldCache[$id] : [];
                foreach ( $data as $key => $value ) {
                        if ( !array_key_exists( $key, $cache ) ) {
                                if ( $session->exists( $key ) ) {
@@ -302,7 +294,7 @@ class PHPSessionHandler {
                \Wikimedia\PhpSessionSerializer::setLogger( new \Psr\Log\NullLogger() );
                foreach ( $cache as $key => $value ) {
                        if ( !array_key_exists( $key, $data ) && $session->exists( $key ) &&
-                               \Wikimedia\PhpSessionSerializer::encode( array( $key => true ) )
+                               \Wikimedia\PhpSessionSerializer::encode( [ $key => true ] )
                        ) {
                                if ( $cache[$key] === $session->get( $key ) ) {
                                        // Unchanged in Session, delete it
@@ -368,18 +360,4 @@ class PHPSessionHandler {
                $this->store->deleteObjectsExpiringBefore( $before );
                return true;
        }
-
-       /**
-        * Shutdown function.
-        *
-        * See the comment inside self::install for rationale.
-        * @codeCoverageIgnore
-        * @private For internal use only
-        */
-       public function handleShutdown() {
-               if ( $this->enable ) {
-                       session_write_close();
-               }
-       }
-
 }