FixedUriConnector.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace React\Socket;
  3. /**
  4. * Decorates an existing Connector to always use a fixed, preconfigured URI
  5. *
  6. * This can be useful for consumers that do not support certain URIs, such as
  7. * when you want to explicitly connect to a Unix domain socket (UDS) path
  8. * instead of connecting to a default address assumed by an higher-level API:
  9. *
  10. * ```php
  11. * $connector = new React\Socket\FixedUriConnector(
  12. * 'unix:///var/run/docker.sock',
  13. * new React\Socket\UnixConnector()
  14. * );
  15. *
  16. * // destination will be ignored, actually connects to Unix domain socket
  17. * $promise = $connector->connect('localhost:80');
  18. * ```
  19. */
  20. class FixedUriConnector implements ConnectorInterface
  21. {
  22. private $uri;
  23. private $connector;
  24. /**
  25. * @param string $uri
  26. * @param ConnectorInterface $connector
  27. */
  28. public function __construct($uri, ConnectorInterface $connector)
  29. {
  30. $this->uri = $uri;
  31. $this->connector = $connector;
  32. }
  33. public function connect($_)
  34. {
  35. return $this->connector->connect($this->uri);
  36. }
  37. }