UnixDomainSocketConnection.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace Predis\Network;
  3. use Predis\ConnectionParameters;
  4. use Predis\CommunicationException;
  5. class UnixDomainSocketConnection extends TcpConnection {
  6. protected function checkParameters(ConnectionParameters $parameters) {
  7. if ($parameters->scheme != 'unix') {
  8. throw new \InvalidArgumentException("Invalid scheme: {$parameters->scheme}");
  9. }
  10. $pathToSocket = $parameters->path;
  11. if (!isset($pathToSocket)) {
  12. throw new \InvalidArgumentException('Missing UNIX domain socket path');
  13. }
  14. if (!file_exists($pathToSocket)) {
  15. throw new \InvalidArgumentException("Could not find $pathToSocket");
  16. }
  17. return $parameters;
  18. }
  19. protected function createResource() {
  20. $uri = sprintf('unix:///%s', $this->_params->path);
  21. $connectFlags = STREAM_CLIENT_CONNECT;
  22. if ($this->_params->connection_persistent) {
  23. $connectFlags |= STREAM_CLIENT_PERSISTENT;
  24. }
  25. $this->_socket = @stream_socket_client(
  26. $uri, $errno, $errstr, $this->_params->connection_timeout, $connectFlags
  27. );
  28. if (!$this->_socket) {
  29. $this->onCommunicationException(trim($errstr), $errno);
  30. }
  31. }
  32. }