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

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