"Fossies" - the Fresh Open Source Software Archive

Member "framework-8.67.0/src/Illuminate/Session/ArraySessionHandler.php" (22 Oct 2021, 2689 Bytes) of package /linux/www/laravel-framework-8.67.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ArraySessionHandler.php" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 8.61.0_vs_8.62.0.

    1 <?php
    2 
    3 namespace Illuminate\Session;
    4 
    5 use Illuminate\Support\InteractsWithTime;
    6 use SessionHandlerInterface;
    7 
    8 class ArraySessionHandler implements SessionHandlerInterface
    9 {
   10     use InteractsWithTime;
   11 
   12     /**
   13      * The array of stored values.
   14      *
   15      * @var array
   16      */
   17     protected $storage = [];
   18 
   19     /**
   20      * The number of minutes the session should be valid.
   21      *
   22      * @var int
   23      */
   24     protected $minutes;
   25 
   26     /**
   27      * Create a new array driven handler instance.
   28      *
   29      * @param  int  $minutes
   30      * @return void
   31      */
   32     public function __construct($minutes)
   33     {
   34         $this->minutes = $minutes;
   35     }
   36 
   37     /**
   38      * {@inheritdoc}
   39      *
   40      * @return bool
   41      */
   42     #[\ReturnTypeWillChange]
   43     public function open($savePath, $sessionName)
   44     {
   45         return true;
   46     }
   47 
   48     /**
   49      * {@inheritdoc}
   50      *
   51      * @return bool
   52      */
   53     #[\ReturnTypeWillChange]
   54     public function close()
   55     {
   56         return true;
   57     }
   58 
   59     /**
   60      * {@inheritdoc}
   61      *
   62      * @return string|false
   63      */
   64     #[\ReturnTypeWillChange]
   65     public function read($sessionId)
   66     {
   67         if (! isset($this->storage[$sessionId])) {
   68             return '';
   69         }
   70 
   71         $session = $this->storage[$sessionId];
   72 
   73         $expiration = $this->calculateExpiration($this->minutes * 60);
   74 
   75         if (isset($session['time']) && $session['time'] >= $expiration) {
   76             return $session['data'];
   77         }
   78 
   79         return '';
   80     }
   81 
   82     /**
   83      * {@inheritdoc}
   84      *
   85      * @return bool
   86      */
   87     #[\ReturnTypeWillChange]
   88     public function write($sessionId, $data)
   89     {
   90         $this->storage[$sessionId] = [
   91             'data' => $data,
   92             'time' => $this->currentTime(),
   93         ];
   94 
   95         return true;
   96     }
   97 
   98     /**
   99      * {@inheritdoc}
  100      *
  101      * @return bool
  102      */
  103     #[\ReturnTypeWillChange]
  104     public function destroy($sessionId)
  105     {
  106         if (isset($this->storage[$sessionId])) {
  107             unset($this->storage[$sessionId]);
  108         }
  109 
  110         return true;
  111     }
  112 
  113     /**
  114      * {@inheritdoc}
  115      *
  116      * @return int|false
  117      */
  118     #[\ReturnTypeWillChange]
  119     public function gc($lifetime)
  120     {
  121         $expiration = $this->calculateExpiration($lifetime);
  122 
  123         foreach ($this->storage as $sessionId => $session) {
  124             if ($session['time'] < $expiration) {
  125                 unset($this->storage[$sessionId]);
  126             }
  127         }
  128 
  129         return true;
  130     }
  131 
  132     /**
  133      * Get the expiration time of the session.
  134      *
  135      * @param  int  $seconds
  136      * @return int
  137      */
  138     protected function calculateExpiration($seconds)
  139     {
  140         return $this->currentTime() - $seconds;
  141     }
  142 }