|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Acquia\Cli\CloudApi; |
| 6 | + |
| 7 | +use AcquiaCloudApi\Connector\ConnectorInterface; |
| 8 | +use Psr\Http\Message\RequestInterface; |
| 9 | +use Psr\Http\Message\ResponseInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * Decorates a ConnectorInterface to rewrite specific API paths before sending |
| 13 | + * requests. Useful for redirecting legacy or alternative API endpoints to new |
| 14 | + * ones transparently. |
| 15 | + */ |
| 16 | +final class PathRewriteConnector implements ConnectorInterface |
| 17 | +{ |
| 18 | + /** |
| 19 | + * The underlying connector to which requests are delegated after path rewriting. |
| 20 | + */ |
| 21 | + private ConnectorInterface $inner; |
| 22 | + |
| 23 | + /** |
| 24 | + * PathRewriteConnector constructor. |
| 25 | + * |
| 26 | + * @param ConnectorInterface $inner The connector to decorate. |
| 27 | + */ |
| 28 | + public function __construct( |
| 29 | + ConnectorInterface $inner, |
| 30 | + ) { |
| 31 | + $this->inner = $inner; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Creates a PSR-7 request, rewriting the path if it matches a rewrite rule. |
| 36 | + * |
| 37 | + * @param string $verb HTTP method (e.g., 'GET', 'POST'). |
| 38 | + * @param string $path The original API path. |
| 39 | + * @return RequestInterface The PSR-7 request with possibly rewritten path. |
| 40 | + */ |
| 41 | + public function createRequest(string $verb, string $path): RequestInterface |
| 42 | + { |
| 43 | + return $this->inner->createRequest($verb, $this->rewritePath($path)); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Sends an HTTP request, rewriting the path if it matches a rewrite rule. |
| 48 | + * |
| 49 | + * @param string $verb HTTP method (e.g., 'GET', 'POST'). |
| 50 | + * @param string $path The original API path. |
| 51 | + * @param array<string, mixed> $options Additional request options. |
| 52 | + * @return ResponseInterface The HTTP response. |
| 53 | + */ |
| 54 | + public function sendRequest(string $verb, string $path, array $options): ResponseInterface |
| 55 | + { |
| 56 | + return $this->inner->sendRequest($verb, $this->rewritePath($path), $options); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns the base URI for the API. |
| 61 | + * |
| 62 | + * @return string The base URI. |
| 63 | + */ |
| 64 | + public function getBaseUri(): string |
| 65 | + { |
| 66 | + return $this->inner->getBaseUri(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns the access token for URL authentication. |
| 71 | + * |
| 72 | + * @return string The access token. |
| 73 | + */ |
| 74 | + public function getUrlAccessToken(): string |
| 75 | + { |
| 76 | + return $this->inner->getUrlAccessToken(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Rewrites the API path using preg_replace if it matches any rewrite rule. |
| 81 | + * |
| 82 | + * @param string $path The original API path. |
| 83 | + * @return string The rewritten path, or the original if no rule matches. |
| 84 | + */ |
| 85 | + private function rewritePath(string $path): string |
| 86 | + { |
| 87 | + foreach ($this->getPathsToRewrite() as $pattern => $replacement) { |
| 88 | + if (preg_match($pattern, $path) === 1) { |
| 89 | + return (string) preg_replace($pattern, $replacement, $path); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Return the original path if no rewrite rule matches. |
| 94 | + return $path; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns an array of regex patterns and their corresponding replacement paths for rewriting API request paths. |
| 99 | + * |
| 100 | + * Two rules cover all cases: |
| 101 | + * - Paths with a trailing segment: /applications/{uuid}/foo/bar → /translation/codebases/{codebaseUuid}/foo/bar |
| 102 | + * - Bare application UUID paths: /applications/{uuid} → /translation/codebases/{codebaseUuid} |
| 103 | + * |
| 104 | + * The first rule uses a capture group ($1) so any trailing path is preserved automatically, |
| 105 | + * avoiding the need to enumerate every possible sub-path. |
| 106 | + * |
| 107 | + * @return array<string, string> Regex pattern => preg_replace replacement string. |
| 108 | + */ |
| 109 | + private function getPathsToRewrite(): array |
| 110 | + { |
| 111 | + $codebaseUuid = $this->getCodeBaseUuid(); |
| 112 | + return [ |
| 113 | + // Matches bare /applications/{uuid} with no trailing segment. |
| 114 | + '#^/applications/[0-9a-f\-]+$#i' => '/translation/codebases/' . $codebaseUuid, |
| 115 | + // Matches /applications/{uuid}/{anything} and preserves the trailing segment via $1. |
| 116 | + '#^/applications/[0-9a-f\-]+/(.+)$#i' => '/translation/codebases/' . $codebaseUuid . '/$1', |
| 117 | + ]; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Retrieves the codebase UUID. |
| 122 | + */ |
| 123 | + private function getCodeBaseUuid(): string |
| 124 | + { |
| 125 | + $codebaseUuid = getenv('AH_CODEBASE_UUID'); |
| 126 | + if (!$codebaseUuid) { |
| 127 | + throw new \RuntimeException('Environment variable AH_CODEBASE_UUID is not set.'); |
| 128 | + } |
| 129 | + return $codebaseUuid; |
| 130 | + } |
| 131 | +} |
0 commit comments