Skip to content

Commit cd67526

Browse files
committed
- Fix variable reuse in Template resolver (Closes #62)
- Add initial version URL resolver that passes spec tests
1 parent 6a705e6 commit cd67526

File tree

3 files changed

+110
-4
lines changed

3 files changed

+110
-4
lines changed

src/Resolver/Template.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public function resolve($definition)
5353
$renderData = [];
5454

5555
if ($definition->has('provide')) {
56-
foreach ($definition->get('provide') as $index => $definition) {
57-
$key = \is_int($index) ? $definition : $index;
58-
$renderData[$key] = $definition instanceof Definition
56+
foreach ($definition->get('provide') as $index => $provideDefinition) {
57+
$key = \is_int($index) ? $provideDefinition : $index;
58+
$renderData[$key] = $provideDefinition instanceof Definition
5959
? $this->getIterator()->get('provide.' . $index)
60-
: $this->getIterator()->get($definition);
60+
: $this->getIterator()->get($provideDefinition);
6161
}
6262
}
6363

src/Resolver/Url.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
}

src/ResolverFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ResolverFactory
1717
public const RESOLVER_TYPE_PROXY = 'proxy';
1818
public const RESOLVER_TYPE_SERVICE = 'service';
1919
public const RESOLVER_TYPE_TEMPLATE = 'template';
20+
public const RESOLVER_TYPE_URL = 'url';
2021

2122
/**
2223
* @var array map of resolver key to their class implementation
@@ -29,6 +30,7 @@ class ResolverFactory
2930
self::RESOLVER_TYPE_PROXY => Resolver\Proxy::class,
3031
self::RESOLVER_TYPE_SERVICE => Resolver\Service::class,
3132
self::RESOLVER_TYPE_TEMPLATE => Resolver\Template::class,
33+
self::RESOLVER_TYPE_URL => Resolver\Url::class,
3234
];
3335

3436
/**

0 commit comments

Comments
 (0)