vendor/predis/predis/src/Connection/AbstractConnection.php line 120

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Predis package.
  4.  *
  5.  * (c) 2009-2020 Daniele Alessandri
  6.  * (c) 2021-2023 Till Krüss
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Predis\Connection;
  12. use InvalidArgumentException;
  13. use Predis\Command\CommandInterface;
  14. use Predis\Command\RawCommand;
  15. use Predis\CommunicationException;
  16. use Predis\Protocol\ProtocolException;
  17. /**
  18.  * Base class with the common logic used by connection classes to communicate
  19.  * with Redis.
  20.  */
  21. abstract class AbstractConnection implements NodeConnectionInterface
  22. {
  23.     private $resource;
  24.     private $cachedId;
  25.     protected $parameters;
  26.     /**
  27.      * @var RawCommand[]
  28.      */
  29.     protected $initCommands = [];
  30.     /**
  31.      * @param ParametersInterface $parameters Initialization parameters for the connection.
  32.      */
  33.     public function __construct(ParametersInterface $parameters)
  34.     {
  35.         $this->parameters $this->assertParameters($parameters);
  36.     }
  37.     /**
  38.      * Disconnects from the server and destroys the underlying resource when
  39.      * PHP's garbage collector kicks in.
  40.      */
  41.     public function __destruct()
  42.     {
  43.         $this->disconnect();
  44.     }
  45.     /**
  46.      * Checks some of the parameters used to initialize the connection.
  47.      *
  48.      * @param ParametersInterface $parameters Initialization parameters for the connection.
  49.      *
  50.      * @return ParametersInterface
  51.      * @throws InvalidArgumentException
  52.      */
  53.     abstract protected function assertParameters(ParametersInterface $parameters);
  54.     /**
  55.      * Creates the underlying resource used to communicate with Redis.
  56.      *
  57.      * @return mixed
  58.      */
  59.     abstract protected function createResource();
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function isConnected()
  64.     {
  65.         return isset($this->resource);
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     public function connect()
  71.     {
  72.         if (!$this->isConnected()) {
  73.             $this->resource $this->createResource();
  74.             return true;
  75.         }
  76.         return false;
  77.     }
  78.     /**
  79.      * {@inheritdoc}
  80.      */
  81.     public function disconnect()
  82.     {
  83.         unset($this->resource);
  84.     }
  85.     /**
  86.      * {@inheritdoc}
  87.      */
  88.     public function addConnectCommand(CommandInterface $command)
  89.     {
  90.         $this->initCommands[] = $command;
  91.     }
  92.     /**
  93.      * {@inheritdoc}
  94.      */
  95.     public function getInitCommands(): array
  96.     {
  97.         return $this->initCommands;
  98.     }
  99.     /**
  100.      * {@inheritdoc}
  101.      */
  102.     public function executeCommand(CommandInterface $command)
  103.     {
  104.         $this->writeRequest($command);
  105.         return $this->readResponse($command);
  106.     }
  107.     /**
  108.      * {@inheritdoc}
  109.      */
  110.     public function readResponse(CommandInterface $command)
  111.     {
  112.         return $this->read();
  113.     }
  114.     /**
  115.      * Helper method to handle connection errors.
  116.      *
  117.      * @param string $message Error message.
  118.      * @param int    $code    Error code.
  119.      */
  120.     protected function onConnectionError($message$code 0)
  121.     {
  122.         CommunicationException::handle(
  123.             new ConnectionException($this"$message [{$this->getParameters()}]"$code)
  124.         );
  125.     }
  126.     /**
  127.      * Helper method to handle protocol errors.
  128.      *
  129.      * @param string $message Error message.
  130.      */
  131.     protected function onProtocolError($message)
  132.     {
  133.         CommunicationException::handle(
  134.             new ProtocolException($this"$message [{$this->getParameters()}]")
  135.         );
  136.     }
  137.     /**
  138.      * {@inheritdoc}
  139.      */
  140.     public function getResource()
  141.     {
  142.         if (isset($this->resource)) {
  143.             return $this->resource;
  144.         }
  145.         $this->connect();
  146.         return $this->resource;
  147.     }
  148.     /**
  149.      * {@inheritdoc}
  150.      */
  151.     public function getParameters()
  152.     {
  153.         return $this->parameters;
  154.     }
  155.     /**
  156.      * Gets an identifier for the connection.
  157.      *
  158.      * @return string
  159.      */
  160.     protected function getIdentifier()
  161.     {
  162.         if ($this->parameters->scheme === 'unix') {
  163.             return $this->parameters->path;
  164.         }
  165.         return "{$this->parameters->host}:{$this->parameters->port}";
  166.     }
  167.     /**
  168.      * {@inheritdoc}
  169.      */
  170.     public function __toString()
  171.     {
  172.         if (!isset($this->cachedId)) {
  173.             $this->cachedId $this->getIdentifier();
  174.         }
  175.         return $this->cachedId;
  176.     }
  177.     /**
  178.      * {@inheritdoc}
  179.      */
  180.     public function __sleep()
  181.     {
  182.         return ['parameters''initCommands'];
  183.     }
  184. }