Skip to content

Commit 8150b6f

Browse files
committed
fix: check if array is associative or not
1 parent 8d9a7f0 commit 8150b6f

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

bootstrap/helpers/shared.php

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3573,6 +3573,23 @@ function generate_fluentd_configuration(): array
35733573
];
35743574
}
35753575

3576+
function isAssociativeArray($array)
3577+
{
3578+
if ($array instanceof Collection) {
3579+
$array = $array->toArray();
3580+
}
3581+
3582+
if (! is_array($array)) {
3583+
throw new \InvalidArgumentException('Input must be an array or a Collection.');
3584+
}
3585+
3586+
if ($array === []) {
3587+
return false;
3588+
}
3589+
3590+
return array_keys($array) !== range(0, count($array) - 1);
3591+
}
3592+
35763593
/**
35773594
* This method adds the default environment variables to the resource.
35783595
* - COOLIFY_APP_NAME
@@ -3584,42 +3601,45 @@ function generate_fluentd_configuration(): array
35843601
*/
35853602
function add_coolify_default_environment_variables(StandaloneRedis|StandalonePostgresql|StandaloneMongodb|StandaloneMysql|StandaloneMariadb|StandaloneKeydb|StandaloneDragonfly|StandaloneClickhouse|Application|Service $resource, Collection &$where_to_add, ?Collection $where_to_check = null)
35863603
{
3604+
if ($resource instanceof Service) {
3605+
$ip = $resource->server->ip;
3606+
} else {
3607+
$ip = $resource->destination->server->ip;
3608+
}
3609+
if (isAssociativeArray($where_to_add)) {
3610+
$isAssociativeArray = true;
3611+
} else {
3612+
$isAssociativeArray = false;
3613+
}
35873614
if ($where_to_check != null && $where_to_check->where('key', 'COOLIFY_APP_NAME')->isEmpty()) {
3588-
if ($resource instanceof Application && $resource->build_pack === 'dockercompose') {
3589-
$where_to_add->put('COOLIFY_APP_NAME', $resource->name);
3590-
} elseif ($resource instanceof Service) {
3615+
if ($isAssociativeArray) {
35913616
$where_to_add->put('COOLIFY_APP_NAME', $resource->name);
35923617
} else {
35933618
$where_to_add->push("COOLIFY_APP_NAME={$resource->name}");
35943619
}
35953620
}
35963621
if ($where_to_check != null && $where_to_check->where('key', 'COOLIFY_SERVER_IP')->isEmpty()) {
3597-
if ($resource instanceof Application && $resource->build_pack === 'dockercompose') {
3598-
$where_to_add->put('COOLIFY_SERVER_IP', $resource->destination->server->ip);
3599-
} elseif ($resource instanceof Service) {
3600-
$where_to_add->put('COOLIFY_SERVER_IP', $resource->server->ip);
3622+
if ($isAssociativeArray) {
3623+
$where_to_add->put('COOLIFY_SERVER_IP', $ip);
36013624
} else {
3602-
$where_to_add->push("COOLIFY_SERVER_IP={$resource->destination->server->ip}");
3625+
$where_to_add->push("COOLIFY_SERVER_IP={$ip}");
36033626
}
36043627
}
36053628
if ($where_to_check != null && $where_to_check->where('key', 'COOLIFY_ENVIRONMENT_NAME')->isEmpty()) {
3606-
if ($resource instanceof Application && $resource->build_pack === 'dockercompose') {
3607-
$where_to_add->put('COOLIFY_ENVIRONMENT_NAME', $resource->environment->name);
3608-
} elseif ($resource instanceof Service) {
3629+
if ($isAssociativeArray) {
36093630
$where_to_add->put('COOLIFY_ENVIRONMENT_NAME', $resource->environment->name);
36103631
} else {
36113632
$where_to_add->push("COOLIFY_ENVIRONMENT_NAME={$resource->environment->name}");
36123633
}
36133634
}
36143635
if ($where_to_check != null && $where_to_check->where('key', 'COOLIFY_PROJECT_NAME')->isEmpty()) {
3615-
if ($resource instanceof Application && $resource->build_pack === 'dockercompose') {
3616-
$where_to_add->put('COOLIFY_PROJECT_NAME', $resource->project()->name);
3617-
} elseif ($resource instanceof Service) {
3636+
if ($isAssociativeArray) {
36183637
$where_to_add->put('COOLIFY_PROJECT_NAME', $resource->project()->name);
36193638
} else {
36203639
$where_to_add->push("COOLIFY_PROJECT_NAME={$resource->project()->name}");
36213640
}
36223641
}
3642+
ray($where_to_add);
36233643
}
36243644

36253645
function convertComposeEnvironmentToArray($environment)

tests/Feature/ConvertArraysTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
test('isAssociativeArray', function () {
4+
expect(isAssociativeArray([1, 2, 3]))->toBeFalse();
5+
expect(isAssociativeArray(collect([1, 2, 3])))->toBeFalse();
6+
expect(isAssociativeArray(collect(['a' => 1, 'b' => 2, 'c' => 3])))->toBeTrue();
7+
});

0 commit comments

Comments
 (0)