Cake\Network\Session::engine PHP Метод

engine() публичный Метод

If a string is passed for the first argument, it will be treated as the class name and the second argument will be passed as the first argument in the constructor. If an instance of a SessionHandlerInterface is provided as the first argument, the handler will be set to it. If no arguments are passed it will return the currently configured handler instance or null if none exists.
public engine ( string | SessionHandlerInterface | null $class = null, array $options = [] ) : SessionHandlerInterface | null
$class string | SessionHandlerInterface | null The session handler to use
$options array the options to pass to the SessionHandler constructor
Результат SessionHandlerInterface | null
    public function engine($class = null, array $options = [])
    {
        if ($class instanceof SessionHandlerInterface) {
            return $this->_engine = $class;
        }
        if ($class === null) {
            return $this->_engine;
        }
        $className = App::className($class, 'Network/Session');
        if (!$className) {
            throw new InvalidArgumentException(sprintf('The class "%s" does not exist and cannot be used as a session engine', $class));
        }
        $handler = new $className($options);
        if (!$handler instanceof SessionHandlerInterface) {
            throw new InvalidArgumentException('The chosen SessionHandler does not implement SessionHandlerInterface, it cannot be used as an engine.');
        }
        return $this->_engine = $handler;
    }

Usage Example

Пример #1
0
 /**
  * Tests instantiating a missing engine
  *
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage The class "Derp" does not exist and cannot be used as a session engine
  * @return void
  */
 public function testBadEngine()
 {
     $session = new Session();
     $session->engine('Derp');
 }