Skip to content

Commit 7ca7a61

Browse files
committed
feat: Slight performance improvements
1 parent c373c01 commit 7ca7a61

9 files changed

Lines changed: 69 additions & 10 deletions

File tree

src/Api/ApiClient.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,12 @@ protected function buildHttpClientConfig(): array
424424
];
425425
}
426426

427-
return array_merge_recursive($clientConfig, $authConfig);
427+
if (isset($authConfig['headers'])) {
428+
$clientConfig['headers'] = array_merge($clientConfig['headers'], $authConfig['headers']);
429+
unset($authConfig['headers']);
430+
}
431+
432+
return array_merge($clientConfig, $authConfig);
428433
}
429434

430435
/**

src/Asset/AssetFinalizerTrait.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ protected function finalizeSource(): string
186186
{
187187
$source = $this->asset->publicId(true);
188188

189-
if (! preg_match('/^https?:\//i', $source)) {
189+
if (stripos($source, 'http:/') !== 0 && stripos($source, 'https:/') !== 0) {
190190
$source = rawurldecode($source);
191191
}
192192

@@ -212,10 +212,14 @@ protected function finalizeVersion(): ?string
212212

213213
if (empty($version) && $this->urlConfig->forceVersion
214214
&& ! empty($this->asset->location)
215-
&& ! preg_match('/^https?:\//', $this->asset->publicId())
216-
&& ! preg_match('/^v\d+/', $this->asset->publicId())
217215
) {
218-
$version = '1';
216+
$publicId = $this->asset->publicId();
217+
if (strncmp($publicId, 'http:/', 6) !== 0
218+
&& strncmp($publicId, 'https:/', 7) !== 0
219+
&& ! preg_match('/^v\d+/', $publicId)
220+
) {
221+
$version = '1';
222+
}
219223
}
220224

221225
return $version ? 'v' . $version : null;

src/Asset/AuthToken.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,13 @@ public function isEnabled(): bool
8585
*/
8686
public function configuration(mixed $configuration): static
8787
{
88-
$tempConfiguration = new Configuration($configuration, false); // TODO: improve performance here
88+
if ($configuration instanceof Configuration) {
89+
$this->config = clone $configuration->authToken;
90+
91+
return $this;
92+
}
93+
94+
$tempConfiguration = new Configuration($configuration, false);
8995

9096
$this->config = $tempConfiguration->authToken;
9197

src/Asset/BaseAsset.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,15 @@ public function importJson(array|string $json): static
266266
*/
267267
public function configuration(Configuration|array $configuration): static
268268
{
269-
$tempConfiguration = new Configuration($configuration, true); // TODO: improve performance here
269+
if ($configuration instanceof Configuration) {
270+
$this->cloud = clone $configuration->cloud;
271+
$this->urlConfig = clone $configuration->url;
272+
$this->logging = clone $configuration->logging;
273+
274+
return $this;
275+
}
276+
277+
$tempConfiguration = new Configuration($configuration, true);
270278
$this->cloud = $tempConfiguration->cloud;
271279
$this->urlConfig = $tempConfiguration->url;
272280
$this->logging = $tempConfiguration->logging;
@@ -372,7 +380,7 @@ public function jsonSerialize(bool $includeEmptyKeys = false, bool $includeEmpty
372380
if (! $includeEmptySections && empty(array_values($section)[0])) {
373381
continue;
374382
}
375-
$json = array_merge($json, $section);
383+
$json += $section;
376384
}
377385

378386
return $json;

src/Asset/Descriptor/AssetDescriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function setSuffix(?string $suffix): static
162162
return $this;
163163
}
164164

165-
if (preg_match('/[.\/]/', $suffix)) {
165+
if (str_contains($suffix, '.') || str_contains($suffix, '/')) {
166166
throw new \UnexpectedValueException(static::class . '::$suffix must not include . or /');
167167
}
168168

src/Configuration/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public function jsonSerialize(
331331
if (! $includeEmptySections && empty(array_values($section)[0])) {
332332
continue;
333333
}
334-
$json = array_merge($json, $section);
334+
$json += $section;
335335
}
336336

337337
return $json;

tests/Unit/Admin/OAuthTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public function testOauthTokenAdminApi()
4343
'Authorization' => ['Bearer ' . self::FAKE_OAUTH_TOKEN]
4444
]
4545
);
46+
47+
self::assertArrayHasKey(
48+
'User-Agent',
49+
$lastRequest->getHeaders(),
50+
'User-Agent header must be present alongside Authorization when using OAuth token'
51+
);
4652
}
4753

4854
/**

tests/Unit/Configuration/ConfigurationTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,28 @@ public function testConfigJsonSerialize()
152152
json_encode(Configuration::fromJson($expectedJsonConfig))
153153
);
154154
}
155+
156+
public function testAssetConfigurationIsIndependentFromGlobalConfig()
157+
{
158+
$globalConfig = Configuration::instance();
159+
$originalCloudName = $globalConfig->cloud->cloudName;
160+
$originalSecure = $globalConfig->url->secure;
161+
162+
$image = new \Cloudinary\Asset\Image('sample.png');
163+
164+
// Mutating the asset's config sections must not affect the global configuration
165+
$image->cloud->cloudName = 'mutated_cloud';
166+
$image->urlConfig->secure = ! $originalSecure;
167+
168+
self::assertEquals(
169+
$originalCloudName,
170+
$globalConfig->cloud->cloudName,
171+
'Mutating asset cloud config must not affect the global Configuration'
172+
);
173+
self::assertEquals(
174+
$originalSecure,
175+
$globalConfig->url->secure,
176+
'Mutating asset url config must not affect the global Configuration'
177+
);
178+
}
155179
}

tests/Unit/Upload/OAuthTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public function testOauthTokenUploadApi()
4747
'Authorization' => ['Bearer ' . self::FAKE_OAUTH_TOKEN]
4848
]
4949
);
50+
51+
self::assertArrayHasKey(
52+
'User-Agent',
53+
$lastRequest->getHeaders(),
54+
'User-Agent header must be present alongside Authorization when using OAuth token'
55+
);
5056
}
5157

5258
/**

0 commit comments

Comments
 (0)