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

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-2025 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 Predis\Command\CommandInterface;
  13. use Predis\Command\RawCommand;
  14. use Predis\CommunicationException;
  15. use Predis\Connection\Resource\Exception\StreamInitException;
  16. use Predis\Protocol\Parser\ParserStrategyResolver;
  17. use Predis\Protocol\Parser\Strategy\ParserStrategyInterface;
  18. use Predis\Protocol\ProtocolException;
  19. /**
  20.  * Base class with the common logic used by connection classes to communicate
  21.  * with Redis.
  22.  */
  23. abstract class AbstractConnection implements NodeConnectionInterface
  24. {
  25.     /**
  26.      * @var ParserStrategyInterface
  27.      */
  28.     protected $parserStrategy;
  29.     /**
  30.      * @var int|null
  31.      */
  32.     protected $clientId;
  33.     protected $resource;
  34.     private $cachedId;
  35.     protected $parameters;
  36.     /**
  37.      * @var RawCommand[]
  38.      */
  39.     protected $initCommands = [];
  40.     /**
  41.      * @param ParametersInterface $parameters Initialization parameters for the connection.
  42.      */
  43.     public function __construct(ParametersInterface $parameters)
  44.     {
  45.         $this->parameters $parameters;
  46.         $this->setParserStrategy();
  47.     }
  48.     /**
  49.      * Disconnects from the server and destroys the underlying resource when
  50.      * PHP's garbage collector kicks in.
  51.      */
  52.     public function __destruct()
  53.     {
  54.         $this->disconnect();
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function isConnected()
  60.     {
  61.         return isset($this->resource);
  62.     }
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function hasDataToRead(): bool
  67.     {
  68.         return true;
  69.     }
  70.     /**
  71.      * Creates a stream resource to communicate with Redis.
  72.      *
  73.      * @return mixed
  74.      * @throws StreamInitException
  75.      */
  76.     abstract protected function createResource();
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public function connect()
  81.     {
  82.         if (!$this->isConnected()) {
  83.             $this->resource $this->createResource();
  84.             return true;
  85.         }
  86.         return false;
  87.     }
  88.     /**
  89.      * {@inheritdoc}
  90.      */
  91.     public function disconnect()
  92.     {
  93.         unset($this->resource);
  94.     }
  95.     /**
  96.      * {@inheritdoc}
  97.      */
  98.     public function addConnectCommand(CommandInterface $command)
  99.     {
  100.         $this->initCommands[] = $command;
  101.     }
  102.     /**
  103.      * {@inheritdoc}
  104.      */
  105.     public function getInitCommands(): array
  106.     {
  107.         return $this->initCommands;
  108.     }
  109.     /**
  110.      * {@inheritdoc}
  111.      */
  112.     public function executeCommand(CommandInterface $command)
  113.     {
  114.         $this->writeRequest($command);
  115.         return $this->readResponse($command);
  116.     }
  117.     /**
  118.      * {@inheritdoc}
  119.      */
  120.     public function readResponse(CommandInterface $command)
  121.     {
  122.         return $this->read();
  123.     }
  124.     /**
  125.      * Helper method to handle connection errors.
  126.      *
  127.      * @param  string                 $message Error message.
  128.      * @param  int                    $code    Error code.
  129.      * @throws CommunicationException
  130.      */
  131.     protected function onConnectionError($message$code 0): void
  132.     {
  133.         CommunicationException::handle(
  134.             new ConnectionException($this"$message [{$this->getParameters()}]"$code)
  135.         );
  136.     }
  137.     /**
  138.      * Helper method to handle protocol errors.
  139.      *
  140.      * @param  string                 $message Error message.
  141.      * @throws CommunicationException
  142.      */
  143.     protected function onProtocolError($message)
  144.     {
  145.         CommunicationException::handle(
  146.             new ProtocolException($this"$message [{$this->getParameters()}]")
  147.         );
  148.     }
  149.     /**
  150.      * {@inheritdoc}
  151.      */
  152.     public function getResource()
  153.     {
  154.         if (isset($this->resource)) {
  155.             return $this->resource;
  156.         }
  157.         $this->connect();
  158.         return $this->resource;
  159.     }
  160.     /**
  161.      * {@inheritdoc}
  162.      */
  163.     public function getParameters()
  164.     {
  165.         return $this->parameters;
  166.     }
  167.     /**
  168.      * Gets an identifier for the connection.
  169.      *
  170.      * @return string
  171.      */
  172.     protected function getIdentifier()
  173.     {
  174.         if ($this->parameters->scheme === 'unix') {
  175.             return $this->parameters->path;
  176.         }
  177.         return "{$this->parameters->host}:{$this->parameters->port}";
  178.     }
  179.     /**
  180.      * {@inheritDoc}
  181.      */
  182.     public function getClientId(): ?int
  183.     {
  184.         return $this->clientId;
  185.     }
  186.     /**
  187.      * {@inheritdoc}
  188.      */
  189.     public function __toString()
  190.     {
  191.         if (!isset($this->cachedId)) {
  192.             $this->cachedId $this->getIdentifier();
  193.         }
  194.         return $this->cachedId;
  195.     }
  196.     /**
  197.      * {@inheritdoc}
  198.      */
  199.     public function __sleep()
  200.     {
  201.         return ['parameters''initCommands'];
  202.     }
  203.     /**
  204.      * Set parser strategy for given connection.
  205.      *
  206.      * @return void
  207.      */
  208.     protected function setParserStrategy(): void
  209.     {
  210.         $strategyResolver = new ParserStrategyResolver();
  211.         $this->parserStrategy $strategyResolver->resolve((int) $this->parameters->protocol);
  212.     }
  213. }