@@ -44,6 +44,72 @@ public static function createTable(): void {
4444 );
4545 }
4646
47+ /**
48+ * Adds deploy cache data to PathInfos that don't
49+ * already have it.
50+ *
51+ * @param \Iterator<PathInfo>
52+ * @return \Iterator<PathInfo>
53+ */
54+ public static function addCacheData (
55+ \Iterator $ path_infos ,
56+ ): \Iterator {
57+ global $ wpdb ;
58+
59+ $ chunks = Utils::chunkIterator ( $ path_infos , 200 );
60+ foreach ( $ chunks as $ chunk ) {
61+ $ to_lookup = [];
62+ foreach ( $ chunk as $ pi ) {
63+ if ( isset ( $ pi ->deploy_cache ) ) {
64+ yield $ pi ;
65+ } else {
66+ $ to_lookup [] = $ pi ;
67+ }
68+ }
69+
70+ if ( empty ( $ to_lookup ) ) {
71+ continue ;
72+ }
73+
74+ $ path_hashes = array_map (
75+ function ( PathInfo $ pi ): string {
76+ return $ pi ->getPathHash ();
77+ },
78+ $ to_lookup ,
79+ );
80+
81+ $ placeholders = implode ( ', ' , array_fill ( 0 , count ( $ path_hashes ), '%s ' ) );
82+ $ table_name = self ::getTableName ();
83+ $ sql = "SELECT path_hash,data_hash,namespace
84+ FROM $ table_name
85+ WHERE path_hash IN ( $ placeholders) " ;
86+ $ query = $ wpdb ->prepare ( $ sql , ...$ path_hashes );
87+ $ results = $ wpdb ->get_results ( $ query );
88+
89+ $ results_by_hash = [];
90+ foreach ( $ results as $ result ) {
91+ $ current = $ results_by_hash [ $ result ->path_hash ] ?? [];
92+ $ current [ $ result ->namespace ] = $ result ->data_hash ;
93+ $ results_by_hash [ $ result ->path_hash ] = $ current ;
94+ }
95+ foreach ( $ to_lookup as $ pi ) {
96+ $ cached = $ results_by_hash [ $ pi ->getPathHash () ] ?? null ;
97+
98+ if ( $ cached ) {
99+ if ( STATIC_DEPLOY_DEBUG ) {
100+ WsLog::d (
101+ 'Adding deploy cache data for '
102+ . $ pi ->path . ': ' . json_encode ( $ cached )
103+ );
104+ }
105+ yield $ pi ->withDeployCache ( $ cached );
106+ } else {
107+ yield $ pi ;
108+ }
109+ }
110+ }
111+ }
112+
47113 public static function addFile (
48114 string $ path ,
49115 string $ ns = self ::DEFAULT_NAMESPACE ,
0 commit comments