AmpResolver.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. /**
  25. * @var mixed[]
  26. */
  27. private $dnsMap;
  28. /**
  29. * @param mixed[] $dnsMap
  30. */
  31. public function __construct(&$dnsMap)
  32. {
  33. $this->dnsMap = &$dnsMap;
  34. }
  35. /**
  36. * @param string $name
  37. * @param int|null $typeRestriction
  38. */
  39. public function resolve($name, $typeRestriction = null)
  40. {
  41. if (!isset($this->dnsMap[$name]) || !\in_array($typeRestriction, [Record::A, null], true)) {
  42. return Dns\resolver()->resolve($name, $typeRestriction);
  43. }
  44. return new Success([new Record($this->dnsMap[$name], Record::A, null)]);
  45. }
  46. /**
  47. * @param string $name
  48. * @param int $type
  49. */
  50. public function query($name, $type)
  51. {
  52. if (!isset($this->dnsMap[$name]) || Record::A !== $type) {
  53. return Dns\resolver()->query($name, $type);
  54. }
  55. return new Success([new Record($this->dnsMap[$name], Record::A, null)]);
  56. }
  57. }