11<?php
22
3+ error_reporting (-1 );
4+ set_error_handler (function ($ type , $ message , $ file , $ line ) {
5+ if (error_reporting ()) {
6+ throw new \ErrorException ($ message , 0 , $ type , $ file , $ line );
7+ }
8+ });
39array_shift ($ _SERVER ['argv ' ]);
410$ dirs = $ _SERVER ['argv ' ];
511
@@ -59,6 +65,8 @@ function getContentHash(array $composerJson)
5965 $ composerJsons [$ composerJson ['name ' ]] = array ($ dir , $ composerLock ['packages ' ], getRelevantContent ($ composerJson ));
6066}
6167
68+ $ referencedCommits = array ();
69+
6270foreach ($ composerJsons as list ($ dir , $ lockedPackages )) {
6371 foreach ($ lockedPackages as $ lockedJson ) {
6472 if (0 !== strpos ($ version = $ lockedJson ['version ' ], 'dev- ' ) && '-dev ' !== substr ($ version , -4 )) {
@@ -83,5 +91,61 @@ function getContentHash(array $composerJson)
8391 @unlink ($ dir .'/composer.lock ' );
8492 continue 2 ;
8593 }
94+
95+ $ referencedCommits [$ name ][$ lockedJson ['source ' ]['reference ' ]][] = $ dir ;
96+ }
97+ }
98+
99+ if (!$ referencedCommits || (isset ($ _SERVER ['TRAVIS_PULL_REQUEST ' ]) && 'false ' !== $ _SERVER ['TRAVIS_PULL_REQUEST ' ])) {
100+ // cached commits cannot be stale for PRs
101+ return ;
102+ }
103+
104+ @mkdir ($ _SERVER ['HOME ' ].'/.cache/composer/repo/https---repo.packagist.org ' , 0777 , true );
105+
106+ $ ch = null ;
107+ $ mh = curl_multi_init ();
108+ $ sh = curl_share_init ();
109+ curl_share_setopt ($ sh , CURLSHOPT_SHARE , CURL_LOCK_DATA_COOKIE );
110+ curl_share_setopt ($ sh , CURLSHOPT_SHARE , CURL_LOCK_DATA_DNS );
111+ curl_share_setopt ($ sh , CURLSHOPT_SHARE , CURL_LOCK_DATA_SSL_SESSION );
112+ $ chs = array ();
113+
114+ foreach ($ referencedCommits as $ name => $ dirsByCommit ) {
115+ $ chs [] = $ ch = array (curl_init (), fopen ($ _SERVER ['HOME ' ].'/.cache/composer/repo/https---repo.packagist.org/provider- ' .strtr ($ name , '/ ' , '$ ' ).'.json ' , 'wb ' ));
116+ curl_setopt ($ ch [0 ], CURLOPT_URL , 'https://repo.packagist.org/p/ ' .$ name .'.json ' );
117+ curl_setopt ($ ch [0 ], CURLOPT_FILE , $ ch [1 ]);
118+ curl_setopt ($ ch [0 ], CURLOPT_SHARE , $ sh );
119+ curl_multi_add_handle ($ mh , $ ch [0 ]);
120+ }
121+
122+ do {
123+ curl_multi_exec ($ mh , $ active );
124+ curl_multi_select ($ mh );
125+ } while ($ active );
126+
127+ foreach ($ chs as list ($ ch , $ fd )) {
128+ curl_multi_remove_handle ($ mh , $ ch );
129+ curl_close ($ ch );
130+ fclose ($ fd );
131+ }
132+
133+ foreach ($ referencedCommits as $ name => $ dirsByCommit ) {
134+ $ repo = file_get_contents ($ _SERVER ['HOME ' ].'/.cache/composer/repo/https---repo.packagist.org/provider- ' .strtr ($ name , '/ ' , '$ ' ).'.json ' );
135+ $ repo = json_decode ($ repo , true );
136+
137+ foreach ($ repo ['packages ' ][$ name ] as $ version ) {
138+ unset($ referencedCommits [$ name ][$ version ['source ' ]['reference ' ]]);
139+ }
140+ }
141+
142+ foreach ($ referencedCommits as $ name => $ dirsByCommit ) {
143+ foreach ($ dirsByCommit as $ dirs ) {
144+ foreach ($ dirs as $ dir ) {
145+ if (file_exists ($ dir .'/composer.lock ' )) {
146+ echo "$ dir/composer.lock references old commit for $ name. \n" ;
147+ @unlink ($ dir .'/composer.lock ' );
148+ }
149+ }
86150 }
87151}
0 commit comments