14
14
use namespace HH\Lib\{C , Math , Str , Vec} ;
15
15
16
16
final class APIIndex {
17
+ private function __construct (
18
+ private ProductAPIIndexShape $index ,
19
+ ) {
20
+ }
21
+
17
22
<<__Memoize >>
18
- public static function getIndex (
23
+ public static function get (APIProduct $product ): APIIndex {
24
+ $idx = self :: getRawIndex();
25
+ switch ($product ) {
26
+ case APIProduct :: HACK :
27
+ return new self ($idx [APIProduct :: HACK ]);
28
+ case APIProduct :: HSL :
29
+ return new self ($idx [APIProduct :: HSL ]);
30
+ case APIProduct :: PHP :
31
+ invariant_violation (" Can't handle PHP index" );
32
+ }
33
+ }
34
+
35
+ public function getIndex (): ProductAPIIndexShape {
36
+ return $this -> index ;
37
+ }
38
+
39
+ public static function searchAllProducts (string $term ): vec <SearchResult > {
40
+ return Vec \concat (
41
+ self :: get(APIProduct :: HACK )-> search($term ),
42
+ self :: get(APIProduct :: HSL )-> search($term ),
43
+ );
44
+ }
45
+
46
+ <<__Memoize >>
47
+ private static function getRawIndex (
19
48
): APIIndexShape {
20
- $key = __CLASS__ . ' !' . BuildPaths :: BUILD_ID ;
49
+ $key = __CLASS__ . ' !' . LocalConfig :: getBuildID() ;
21
50
22
51
$success = false ;
23
52
$data = apc_fetch ($key , $success );
53
+ $success = false ;
24
54
if ($success ) {
25
55
return $data ;
26
56
}
@@ -33,44 +63,47 @@ public static function getIndex(
33
63
return $data ;
34
64
}
35
65
36
- public static function getIndexForType (
66
+ public function getIndexForType (
37
67
APIDefinitionType $type ,
38
68
): array <string , APIIndexEntry > {
39
- $index = Shapes :: toArray(self :: getIndex() );
69
+ $index = Shapes :: toArray($this -> index );
40
70
invariant (
41
71
array_key_exists ($type , $index ),
42
72
' invalid type: %s' ,
43
73
(string ) $type ,
44
74
);
45
- // UNSAFE
46
75
return $index [$type ];
47
76
}
48
77
49
- public static function getFunctionIndex (
78
+ public function getFunctionIndex (
50
79
): array <string , APIFunctionIndexEntry > {
51
- return self :: getIndex() [' function' ];
80
+ return $this -> index [' function' ];
52
81
}
53
82
54
- public static function getClassIndex (
83
+ public function getClassIndex (
55
84
APIDefinitionType $type ,
56
85
): array <string , APIClassIndexEntry > {
57
- invariant (
58
- $type !== APIDefinitionType :: FUNCTION_DEF ,
59
- ' functions are not classes' ,
60
- );
61
- // UNSAFE
62
- return self :: getIndex()[$type ];
86
+ switch ($type ) {
87
+ case APIDefinitionType :: FUNCTION_DEF :
88
+ invariant_violation (' functions are not classes' );
89
+ case APIDefinitionType :: CLASS_DEF :
90
+ return $this -> index [' class' ];
91
+ case APIDefinitionType :: INTERFACE_DEF :
92
+ return $this -> index [' interface' ];
93
+ case APIDefinitionType :: TRAIT_DEF :
94
+ return $this -> index [' trait' ];
95
+ }
63
96
}
64
97
65
- public static function search (
98
+ public function search (
66
99
string $term ,
67
100
): vec <SearchResult > {
68
101
return APIDefinitionType :: getValues()
69
- |> Vec \map ($$, $type ==> self :: searchEntries($term , $type ))
102
+ |> Vec \map ($$, $type ==> $this -> searchEntries($term , $type ))
70
103
|> Vec \flatten ($$);
71
104
}
72
105
73
- private static function getMethods (
106
+ private function getMethods (
74
107
APIIndexEntry $entry ,
75
108
): ?vec <APIMethodIndexEntry > {
76
109
$arr = Shapes :: toArray($entry );
@@ -87,14 +120,14 @@ private static function getMethods(
87
120
return null ;
88
121
}
89
122
90
- private static function searchEntries (
123
+ private function searchEntries (
91
124
string $term ,
92
125
APIDefinitionType $type ,
93
126
): vec <SearchResult > {
94
127
$terms = Str \split ($term , ' ' );
95
128
$results = vec [];
96
129
97
- $entries = self :: getIndexForType($type );
130
+ $entries = $this -> getIndexForType($type );
98
131
foreach ($entries as $_ => $entry ) {
99
132
$name = $entry [' name' ];
100
133
$type = Str \contains ($name , " HH\\ Lib\\ " )
@@ -106,7 +139,7 @@ private static function searchEntries (
106
139
$results [] = new SearchResult ($type , $score , $name , $entry [' urlPath' ]);
107
140
}
108
141
109
- $methods = self :: getMethods($entry );
142
+ $methods = $this -> getMethods($entry );
110
143
if ($methods === null ) {
111
144
continue ;
112
145
}
@@ -122,10 +155,10 @@ private static function searchEntries (
122
155
return $results ;
123
156
}
124
157
125
- public static function getDataForFunction (
158
+ public function getDataForFunction (
126
159
string $name ,
127
160
): APIFunctionIndexEntry {
128
- $index = self :: getIndex() ;
161
+ $index = $this -> index ;
129
162
invariant (
130
163
array_key_exists ($name , $index [' function' ]),
131
164
' function %s does not exist' ,
@@ -134,11 +167,11 @@ public static function getDataForFunction(
134
167
return $index [' function' ][$name ];
135
168
}
136
169
137
- public static function getDataForClass (
170
+ public function getDataForClass (
138
171
APIDefinitionType $class_type ,
139
172
string $class_name ,
140
173
): APIClassIndexEntry {
141
- $index = self :: getClassIndex($class_type );
174
+ $index = $this -> getClassIndex($class_type );
142
175
invariant (
143
176
array_key_exists ($class_name , $index ),
144
177
' class %s does not exist' ,
@@ -147,12 +180,12 @@ public static function getDataForClass(
147
180
return $index [$class_name ];
148
181
}
149
182
150
- public static function getDataForMethod (
183
+ public function getDataForMethod (
151
184
APIDefinitionType $class_type ,
152
185
string $class_name ,
153
186
string $method_name ,
154
187
): APIMethodIndexEntry {
155
- $index = self :: getClassIndex($class_type );
188
+ $index = $this -> getClassIndex($class_type );
156
189
invariant (
157
190
array_key_exists ($class_name , $index ),
158
191
' class %s does not exist' ,
0 commit comments