9
9
*/
10
10
class DocumentUtils
11
11
{
12
+ private static $ cacheNodesOfKey = [];
13
+ private static $ cacheNodesOfKind = [];
14
+
12
15
/**
13
16
* Returns all FragmentDefinitions
14
17
* @param array $document
@@ -29,6 +32,10 @@ public static function getFragmentDefinitions(array $document): array
29
32
*/
30
33
public static function getAllNodesOfKind (array $ document , string $ kind ): array
31
34
{
35
+ $ hashKey = self ::getHashKey ($ document , $ kind );
36
+ if ((self ::$ cacheNodesOfKind [$ hashKey ] ?? null ) !== null )
37
+ return self ::$ cacheNodesOfKind [$ hashKey ];
38
+
32
39
$ keys = array_keys ($ document );
33
40
34
41
$ nodes = [];
@@ -42,6 +49,8 @@ public static function getAllNodesOfKind(array $document, string $kind): array
42
49
}
43
50
}
44
51
52
+ self ::$ cacheNodesOfKind [$ hashKey ] = $ nodes ;
53
+
45
54
return $ nodes ;
46
55
}
47
56
@@ -53,6 +62,10 @@ public static function getAllNodesOfKind(array $document, string $kind): array
53
62
*/
54
63
public static function getAllNodesOfKey (array $ document , string $ key ): array
55
64
{
65
+ $ hashKey = self ::getHashKey ($ document , $ key );
66
+ if ((self ::$ cacheNodesOfKey [$ hashKey ] ?? null ) !== null )
67
+ return self ::$ cacheNodesOfKey [$ hashKey ];
68
+
56
69
$ keys = array_keys ($ document );
57
70
58
71
$ nodes = [];
@@ -66,7 +79,25 @@ public static function getAllNodesOfKey(array $document, string $key): array
66
79
}
67
80
}
68
81
82
+ self ::$ cacheNodesOfKey [$ hashKey ] = $ nodes ;
83
+
69
84
return $ nodes ;
70
85
}
86
+
87
+ /**
88
+ * Builds an unique hash-key based on a document and an identifier.
89
+ *
90
+ * @param array $document
91
+ * @param string $identifier
92
+ * @return string
93
+ */
94
+ private static function getHashKey (array $ document , string $ identifier ): string
95
+ {
96
+ return crc32 (
97
+ serialize (
98
+ $ document
99
+ )
100
+ ) . $ identifier ;
101
+ }
71
102
}
72
103
0 commit comments