|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Upward\Resolver; |
| 10 | + |
| 11 | +use Magento\Upward\Definition; |
| 12 | +use Zend\Uri\UriFactory; |
| 13 | + |
| 14 | +class Url extends AbstractResolver |
| 15 | +{ |
| 16 | + public const FAKE_BASE_HOST = 'upward-fake.localhost'; |
| 17 | + public const FAKE_BASE_URL = 'https://' . self::FAKE_BASE_HOST; |
| 18 | + |
| 19 | + /** |
| 20 | + * {@inheritdoc} |
| 21 | + */ |
| 22 | + public function getIndicator(): string |
| 23 | + { |
| 24 | + return 'baseUrl'; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * {@inheritdoc} |
| 29 | + */ |
| 30 | + public function isValid(Definition $definition): bool |
| 31 | + { |
| 32 | + if ($definition->has('query')) { |
| 33 | + $query = $this->getIterator()->get('query', $definition); |
| 34 | + if (!\is_array($query)) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return parent::isValid($definition); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * {@inheritdoc} |
| 44 | + */ |
| 45 | + public function resolve($definition) |
| 46 | + { |
| 47 | + if (!$definition instanceof Definition) { |
| 48 | + throw new \InvalidArgumentException('$definition must be an instance of ' . Definition::class); |
| 49 | + } |
| 50 | + |
| 51 | + $baseUrl = $this->getIterator()->get('baseUrl', $definition) |
| 52 | + ? $this->getIterator()->get('baseUrl', $definition) |
| 53 | + : self::FAKE_BASE_URL; |
| 54 | + $uri = UriFactory::factory($baseUrl); |
| 55 | + |
| 56 | + if ($definition->has('hostname')) { |
| 57 | + $uri->setHost($this->getIterator()->get('hostname', $definition)); |
| 58 | + } |
| 59 | + |
| 60 | + if ($definition->has('protocol')) { |
| 61 | + $uri->setScheme(str_replace(':', '', $this->getIterator()->get('protocol', $definition))); |
| 62 | + } |
| 63 | + |
| 64 | + if ($definition->has('pathname')) { |
| 65 | + $pathname = $this->getIterator()->get('pathname', $definition); |
| 66 | + $currentPathname = $uri->getPath(); |
| 67 | + if ($pathname[0] !== '/') { |
| 68 | + if ($currentPathname === null) { |
| 69 | + $pathname = "/${pathname}"; |
| 70 | + } elseif ($currentPathname[\strlen($currentPathname) - 1] === '/') { |
| 71 | + $pathname = $currentPathname . $pathname; |
| 72 | + } else { |
| 73 | + $trimmedCurrentPathname = substr($currentPathname, 0, strrpos($currentPathname, '/') + 1); |
| 74 | + $pathname = $trimmedCurrentPathname . $pathname; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + $uri->setPath($pathname); |
| 79 | + } |
| 80 | + |
| 81 | + if ($definition->has('query')) { |
| 82 | + $mergedQuery = array_merge($uri->getQueryAsArray(), $this->getIterator()->get('query', $definition)); |
| 83 | + $uri->setQuery($mergedQuery); |
| 84 | + } |
| 85 | + |
| 86 | + if ($definition->has('port')) { |
| 87 | + $uri->setPort($this->getIterator()->get('port', $definition)); |
| 88 | + } |
| 89 | + |
| 90 | + if ($definition->has('username')) { |
| 91 | + $userInfo = $this->getIterator()->get('username', $definition); |
| 92 | + if ($definition->has('password')) { |
| 93 | + $userInfo .= ':' . $this->getIterator()->get('password', $definition); |
| 94 | + } |
| 95 | + $uri->setUserInfo($userInfo); |
| 96 | + } |
| 97 | + |
| 98 | + $returnUrl = $uri->toString(); |
| 99 | + |
| 100 | + return $uri->getHost() === self::FAKE_BASE_HOST |
| 101 | + ? str_replace(self::FAKE_BASE_URL, '', $returnUrl) |
| 102 | + : $returnUrl; |
| 103 | + } |
| 104 | +} |
0 commit comments