Skip to content

Commit abef291

Browse files
committed
Always use the external API proxy URL if set
1 parent bf97f31 commit abef291

2 files changed

Lines changed: 32 additions & 11 deletions

File tree

src/Model/Project.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function addUser($user, $role, $byUuid = false)
123123
public function setApiUrl($url)
124124
{
125125
$projectUrl = Url::fromString($url)->combine('/projects/' . \urlencode($this->id))->__toString();
126-
$this->urlViaGateway = $projectUrl;
126+
$this->baseUrl = $this->urlViaGateway = $projectUrl;
127127
}
128128

129129
/**
@@ -213,14 +213,37 @@ public function getEnvironment($id)
213213
*/
214214
public function getLink($rel, $absolute = true)
215215
{
216+
/**
217+
* Require the API URL to be set for 'invitations' and 'access'.
218+
*
219+
* These endpoints require the external API proxy or gateway, or may
220+
* require it in the future.
221+
*
222+
* The parent method, via self::makeAbsoluteUrl(), ensures an
223+
* appropriate base URL is used otherwise.
224+
*
225+
* @see setApiUrl()
226+
* @see makeAbsoluteUrl()
227+
*/
228+
if ($rel === 'invitations' || $rel === 'access') {
229+
if (!isset($this->urlViaGateway)) {
230+
throw new \RuntimeException('The API gateway URL must be set');
231+
}
232+
}
233+
234+
// Use the HAL links available on the project.
216235
if ($this->hasLink($rel)) {
217236
return parent::getLink($rel, $absolute);
218237
}
219238

239+
// If the 'self' link is not present then this might be a project
240+
// stub with an 'endpoint' property.
220241
if ($rel === 'self') {
221-
return $this->getProperty('endpoint');
242+
return $this->makeAbsoluteUrl($this->getProperty('endpoint'));
222243
}
223244

245+
// If the '#ui' link is not present then this might be a project
246+
// stub with a 'uri' property.
224247
if ($rel === '#ui') {
225248
return $this->getProperty('uri');
226249
}
@@ -229,13 +252,6 @@ public function getLink($rel, $absolute = true)
229252
return $this->getUri() . '/variables';
230253
}
231254

232-
if ($rel === 'invitations') {
233-
if (!isset($this->urlViaGateway)) {
234-
throw new \RuntimeException('The API gateway URL must be set');
235-
}
236-
return rtrim($this->urlViaGateway, '/') . '/invitations';
237-
}
238-
239255
return $this->getUri() . '/' . ltrim($rel, '#');
240256
}
241257

src/Model/Resource.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ public function getLink($rel, $absolute = true)
589589
throw new \InvalidArgumentException("Link not found: $rel");
590590
}
591591
$url = $this->data['_links'][$rel]['href'];
592-
if ($absolute && strpos($url, '//') === false) {
592+
if ($absolute || strpos($url, '//') !== false) {
593593
$url = $this->makeAbsoluteUrl($url);
594594
}
595595
return $url;
@@ -610,7 +610,12 @@ protected function makeAbsoluteUrl($relativeUrl, $baseUrl = null)
610610
throw new \RuntimeException('No base URL');
611611
}
612612
$base = Url::fromString($baseUrl);
613-
return (string) $base->combine($relativeUrl);
613+
$target = Url::fromString($relativeUrl);
614+
// Ensure an absolute base URL overrides an absolute target URL.
615+
if ($base->isAbsolute()) {
616+
return (string) $base->combine($target->getPath());
617+
}
618+
return (string) $base->combine($target);
614619
}
615620

616621
/**

0 commit comments

Comments
 (0)