Skip to content

Commit ec70d27

Browse files
committed
improved FormatCache::formatExists performance, added getter for a hash set of format names
1 parent 311e9fa commit ec70d27

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

app/helpers/MetaFormats/FormatCache.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
*/
1111
class FormatCache
1212
{
13+
// do not access the following three arrays directly, use the getter methods instead
14+
// (there is no guarantee that the arrays are initialized)
1315
private static ?array $formatNames = null;
16+
private static ?array $formatNamesHashSet = null;
1417
private static ?array $formatToFieldFormatsMap = null;
1518

1619
/**
@@ -40,9 +43,24 @@ public static function getFormatNames(): array
4043
return self::$formatNames;
4144
}
4245

46+
/**
47+
* @return array Returns a hash set of all defined formats (actually a dictionary with arbitrary values).
48+
*/
49+
public static function getFormatNamesHashSet(): array
50+
{
51+
if (self::$formatNamesHashSet == null) {
52+
$formatNames = self::getFormatNames();
53+
self::$formatNamesHashSet = [];
54+
foreach ($formatNames as $formatName) {
55+
self::$formatNamesHashSet[$formatName] = true;
56+
}
57+
}
58+
return self::$formatNamesHashSet;
59+
}
60+
4361
public static function formatExists(string $format): bool
4462
{
45-
return in_array($format, self::getFormatNames());
63+
return array_key_exists($format, self::getFormatNamesHashSet());
4664
}
4765

4866
/**

0 commit comments

Comments
 (0)