AmpResolver.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\HttpClient\Internal;
  11. use Amp\Dns;
  12. use Amp\Dns\Record;
  13. use Amp\Promise;
  14. use Amp\Success;
  15. /**
  16. * Handles local overrides for the DNS resolver.
  17. *
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. *
  20. * @internal
  21. */
  22. class AmpResolver implements Dns\Resolver
  23. {
  24. private $dnsMap;
  25. public function __construct(array &$dnsMap)
  26. {
  27. $this->dnsMap = &$dnsMap;
  28. }
  29. public function resolve(string $name, int $typeRestriction = null): Promise
  30. {
  31. if (!isset($this->dnsMap[$name]) || !\in_array($typeRestriction, [Record::A, null], true)) {
  32. return Dns\resolver()->resolve($name, $typeRestriction);
  33. }
  34. return new Success([new Record($this->dnsMap[$name], Record::A, null)]);
  35. }
  36. public function query(string $name, int $type): Promise
  37. {
  38. if (!isset($this->dnsMap[$name]) || Record::A !== $type) {
  39. return Dns\resolver()->query($name, $type);
  40. }
  41. return new Success([new Record($this->dnsMap[$name], Record::A, null)]);
  42. }
  43. }