@@ -36,33 +36,40 @@ public function locate( $quiet = false ) {
3636 return $ this ->path ;
3737 }
3838
39- $ cache = WP_CLI ::get_cache ();
40- $ version = null ;
41- foreach ( $ this ->versions ( $ cache )['stable ' ] ?? [] as $ release ) {
39+ $ cache = $ this ->cache ();
40+ $ version = null ;
41+ list ( $ versions , $ fresh ) = $ this ->versions ( $ cache );
42+ foreach ( $ versions ['stable ' ] ?? [] as $ release ) {
4243 if ( preg_match ( '/^2\.\d+\.\d+$/D ' , $ release ['version ' ] ) && $ release ['min-php ' ] <= PHP_VERSION_ID ) {
4344 $ version = $ release ['version ' ];
4445 break ;
4546 }
4647 }
47- if ( null === $ version ) {
48+
49+ if ( null !== $ version ) {
50+ $ path = $ cache ->has ( "composer/composer- {$ version }.phar " );
51+ if ( $ path ) {
52+ $ this ->path = $ path ;
53+ return $ path ;
54+ }
55+ }
56+
57+ if ( ! $ fresh ) {
4858 // Offline or getcomposer.org unreachable: reuse the newest Composer already in the cache.
4959 foreach ( self ::cached_versions ( $ cache ) as $ cached ) {
5060 $ path = $ cache ->has ( "composer/composer- {$ cached }.phar " ); // False once the cache's expiry has passed.
5161 if ( $ path ) {
5262 $ this ->path = $ path ;
53- WP_CLI ::debug ( "Using cached Composer {$ cached }; version list unavailable. " , 'packages ' );
63+ WP_CLI ::debug ( "Using cached Composer {$ cached }; version list unavailable or stale . " , 'packages ' );
5464 return $ path ;
5565 }
5666 }
57- $ version = 'latest-stable ' ;
5867 }
5968
60- $ key = "composer/composer- {$ version }.phar " ;
61- $ path = $ cache ->has ( $ key );
62- if ( $ path ) {
63- $ this ->path = $ path ;
64- return $ path ;
69+ if ( null === $ version ) {
70+ $ version = 'latest-stable ' ;
6571 }
72+ $ key = "composer/composer- {$ version }.phar " ;
6673
6774 $ temp_dir = Utils \get_temp_dir () . uniqid ( 'wp-cli-composer- ' , true );
6875 if ( ! mkdir ( $ temp_dir , 0700 ) ) {
@@ -89,10 +96,7 @@ static function () use ( $temp, $temp_dir ) {
8996 }
9097 chmod ( $ temp , 0755 );
9198 if ( $ cache ->is_enabled () ) {
92- if ( ! $ cache ->import ( $ key , $ temp ) ) {
93- throw new RuntimeException ( 'Could not cache Composer. ' );
94- }
95- chmod ( $ path , 0755 );
99+ $ this ->publish ( $ temp , $ path );
96100 }
97101 $ this ->path = $ path ;
98102 return $ path ;
@@ -102,14 +106,15 @@ static function () use ( $temp, $temp_dir ) {
102106 * The release list from getcomposer.org. A copy lives in the cache for a day, so a run of package
103107 * commands costs one request; when the network is down, a stale copy still names the version to use.
104108 *
105- * @return array Decoded list, empty when neither the network nor the cache has one.
109+ * @return array{0:array,1:bool} Decoded list (empty when neither the network nor the cache has one),
110+ * and whether it is current rather than a stale copy.
106111 */
107112 private function versions ( $ cache ) {
108113 $ key = 'composer/versions.json ' ;
109114 if ( $ cache ->is_enabled () ) {
110115 $ fresh = $ cache ->read ( $ key , self ::VERSIONS_TTL );
111116 if ( false !== $ fresh && is_array ( json_decode ( $ fresh , true ) ) ) {
112- return json_decode ( $ fresh , true );
117+ return [ json_decode ( $ fresh , true ), true ] ;
113118 }
114119 }
115120 try {
@@ -121,18 +126,43 @@ private function versions( $cache ) {
121126 if ( $ cache ->is_enabled () ) {
122127 $ cache ->write ( $ key , $ body );
123128 }
124- return $ versions ;
129+ return [ $ versions, true ] ;
125130 } catch ( \Exception $ e ) {
126131 WP_CLI ::debug ( $ e ->getMessage (), 'packages ' );
127132 }
128133 if ( $ cache ->is_enabled () ) {
129134 $ stale = $ cache ->read ( $ key );
130135 if ( false !== $ stale && is_array ( json_decode ( $ stale , true ) ) ) {
131136 WP_CLI ::debug ( 'Using the cached Composer version list; getcomposer.org is unreachable. ' , 'packages ' );
132- return json_decode ( $ stale , true );
137+ return [ json_decode ( $ stale , true ), false ];
138+ }
139+ }
140+ return [ [], false ];
141+ }
142+
143+ /**
144+ * Moves a verified download into the cache under a temporary name first, so that a concurrent
145+ * process never sees a partially copied Phar at the final path.
146+ */
147+ private function publish ( $ source , $ destination ) {
148+ $ dir = dirname ( $ destination );
149+ if ( ! is_dir ( $ dir ) && ! @mkdir ( $ dir , 0777 , true ) && ! is_dir ( $ dir ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
150+ throw new RuntimeException ( 'Could not cache Composer. ' );
151+ }
152+ $ partial = $ destination . '. ' . uniqid ( '' , true ) . '.part ' ;
153+ if ( ! copy ( $ source , $ partial ) ) {
154+ throw new RuntimeException ( 'Could not cache Composer. ' );
155+ }
156+ chmod ( $ partial , 0755 );
157+ if ( ! @rename ( $ partial , $ destination ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
158+ if ( file_exists ( $ partial ) ) {
159+ unlink ( $ partial );
160+ }
161+ // On Windows the rename fails while another process runs a copy it just published.
162+ if ( ! is_file ( $ destination ) ) {
163+ throw new RuntimeException ( 'Could not cache Composer. ' );
133164 }
134165 }
135- return [];
136166 }
137167
138168 /**
@@ -154,7 +184,14 @@ private static function cached_versions( $cache ) {
154184 return array_reverse ( $ versions );
155185 }
156186
157- private function request ( $ url , $ options = [] ) {
187+ /**
188+ * @return \WP_CLI\FileCache
189+ */
190+ protected function cache () {
191+ return WP_CLI ::get_cache ();
192+ }
193+
194+ protected function request ( $ url , $ options = [] ) {
158195 $ response = Utils \http_request (
159196 'GET ' ,
160197 $ url ,
0 commit comments