206 if ( self::$instance !== $this ) {
207 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
209 if ( !$this->enable ) {
210 throw new \BadMethodCallException(
'Attempt to use PHP session management' );
213 $session = $this->getSessionManager()->getSessionById( $id,
false );
220 $data = iterator_to_array( $session );
221 $this->sessionFieldCache[$id] = $data;
222 return (
string)PhpSessionSerializer::encode( $data );
234 array_walk_recursive( $value,
static function ( $val ) use ( &$result ) {
254 public function write( $id, $dataStr ) {
255 if ( self::$instance !== $this ) {
256 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
258 if ( !$this->enable ) {
259 throw new \BadMethodCallException(
'Attempt to use PHP session management' );
262 $session = $this->getSessionManager()->getSessionById( $id,
true );
266 $this->logger->warning(
267 __METHOD__ .
': Session "{session}" cannot be loaded, skipping write.',
275 $data = PhpSessionSerializer::decode( $dataStr );
276 if ( $data ===
null ) {
284 $cache = $this->sessionFieldCache[$id] ?? [];
285 foreach ( $data as $key => $value ) {
286 if ( !array_key_exists( $key, $cache ) ) {
287 if ( $session->exists( $key ) ) {
289 $this->logger->warning(
290 __METHOD__ .
": Key \"$key\" added in both Session and \$_SESSION!"
294 $session->set( $key, $value );
297 } elseif ( $cache[$key] === $value ) {
300 $this->valueContainsAnyObject( $cache[$key] ) &&
301 $this->valueContainsAnyObject( $value ) &&
302 PhpSessionSerializer::encode( [ $key => $cache[$key] ] ) ===
303 PhpSessionSerializer::encode( [ $key => $value ] )
309 } elseif ( !$session->exists( $key ) ) {
311 $this->logger->warning(
312 __METHOD__ .
": Key \"$key\" deleted in Session and changed in \$_SESSION!"
314 $session->set( $key, $value );
316 } elseif ( $cache[$key] === $session->get( $key ) ) {
318 $session->set( $key, $value );
322 $this->logger->warning(
323 __METHOD__ .
": Key \"$key\" changed in both Session and \$_SESSION!"
329 PhpSessionSerializer::setLogger(
new NullLogger() );
330 foreach ( $cache as $key => $value ) {
331 if ( !array_key_exists( $key, $data ) && $session->exists( $key ) &&
332 PhpSessionSerializer::encode( [ $key =>
true ] )
334 if ( $value === $session->get( $key ) ) {
336 $session->remove( $key );
340 $this->logger->warning(
341 __METHOD__ .
": Key \"$key\" changed in Session and deleted in \$_SESSION!"
346 PhpSessionSerializer::setLogger( $this->logger );
352 foreach ( $changed as $key ) {
353 $this->logger->warning(
"Something wrote to \$_SESSION['$key']!" );
358 $this->sessionFieldCache[$id] = iterator_to_array( $session );
374 if ( self::$instance !== $this ) {
375 throw new \UnexpectedValueException( __METHOD__ .
': Wrong instance called!' );
377 if ( !$this->enable ) {
378 throw new \BadMethodCallException(
'Attempt to use PHP session management' );
380 $session = $this->getSessionManager()->getSessionById( $id,
false );