Skip to content

Commit a2099eb

Browse files
committed
added Format attribute caching
1 parent ebde4ab commit a2099eb

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

app/V1Module/presenters/base/BasePresenter.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,22 @@ public function getFormatInstance(): MetaFormat
207207

208208
private function processParams(ReflectionMethod $reflection)
209209
{
210+
$actionPath = get_class($this) . $reflection->name;
211+
212+
// cache whether the action has a Format attribute
213+
if (!FormatCache::formatAttributeStringCached($actionPath)) {
214+
$extractedFormat = MetaFormatHelper::extractFormatFromAttribute($reflection);
215+
FormatCache::cacheFormatAttributeString($actionPath, $extractedFormat);
216+
}
210217
// use a method specialized for formats if there is a format available
211-
$format = MetaFormatHelper::extractFormatFromAttribute($reflection);
218+
$format = FormatCache::getFormatAttributeString($actionPath);
212219
if ($format !== null) {
213220
$this->requestFormatInstance = $this->processParamsFormat($format, null);
214221
}
215222

216223
// handle loose parameters
217224

218225
// cache the data from the loose attributes to improve performance
219-
$actionPath = get_class($this) . $reflection->name;
220226
if (!FormatCache::looseParametersCached($actionPath)) {
221227
$newParamData = MetaFormatHelper::extractRequestParamData($reflection);
222228
FormatCache::cacheLooseParameters($actionPath, $newParamData);

app/helpers/MetaFormats/FormatCache.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class FormatCache
1919
// this array caches loose attribute data which are added over time by the presenters
2020
private static array $actionToRequestParamDataMap = [];
2121

22+
// array that caches Format attribute format strings for actions
23+
private static array $actionToFormatMap = [];
24+
2225
/**
2326
* @param string $actionPath The presenter class name joined with the name of the action method.
2427
* @return bool Returns whether the loose parameters of the action are cached.
@@ -28,6 +31,33 @@ public static function looseParametersCached(string $actionPath): bool
2831
return array_key_exists($actionPath, self::$actionToRequestParamDataMap);
2932
}
3033

34+
/**
35+
* @param string $actionPath The presenter class name joined with the name of the action method.
36+
* @return bool Returns whether the action Format attribute string was cached.
37+
*/
38+
public static function formatAttributeStringCached(string $actionPath): bool
39+
{
40+
return array_key_exists($actionPath, self::$actionToFormatMap);
41+
}
42+
43+
/**
44+
* @param string $actionPath The presenter class name joined with the name of the action method.
45+
* @param string $format The attribute format string.
46+
*/
47+
public static function cacheFormatAttributeString(string $actionPath, string $format)
48+
{
49+
self::$actionToFormatMap[$actionPath] = $format;
50+
}
51+
52+
/**
53+
* @param string $actionPath The presenter class name joined with the name of the action method.
54+
* @return bool Returns action Format attribute string.
55+
*/
56+
public static function getFormatAttributeString(string $actionPath): string
57+
{
58+
return self::$actionToFormatMap[$actionPath];
59+
}
60+
3161
/**
3262
* @param string $actionPath The presenter class name joined with the name of the action method.
3363
* @return array Returns the cached RequestParamData array of the loose attributes.

0 commit comments

Comments
 (0)