@@ -143,64 +143,67 @@ func Install(args []string) {
143143 }
144144
145145 // check if .pvm folder exists
146- if _ , err := os .Stat (homeDir + "/.pvm" ); os .IsNotExist (err ) {
146+ pvmPath := filepath .Join (homeDir , ".pvm" )
147+ if _ , err := os .Stat (pvmPath ); os .IsNotExist (err ) {
147148 theme .Info ("Creating .pvm folder in home directory" )
148- os .Mkdir (homeDir + "/.pvm" , 0755 )
149+ os .Mkdir (pvmPath , 0755 )
149150 }
150151
151152 // check if .pvm/versions folder exists
152- if _ , err := os .Stat (homeDir + "/.pvm/versions" ); os .IsNotExist (err ) {
153+ versionsPath := filepath .Join (pvmPath , "versions" )
154+ if _ , err := os .Stat (versionsPath ); os .IsNotExist (err ) {
153155 theme .Info ("Creating .pvm/versions folder in home directory" )
154- os .Mkdir (homeDir + "/.pvm/versions" , 0755 )
156+ os .Mkdir (versionsPath , 0755 )
155157 }
156158
157159 theme .Info ("Downloading" )
158160
159- // Get the data
160- downloadResponse , err := http .Get ("https://windows.php.net" + desiredVersion .Url )
161- if err != nil {
162- log .Fatalln (err )
163- }
164-
165- defer downloadResponse .Body .Close ()
166-
167161 // zip filename from url
162+ zipUrl := "https://windows.php.net" + desiredVersion .Url
168163 zipFileName := strings .Split (desiredVersion .Url , "/" )[len (strings .Split (desiredVersion .Url , "/" ))- 1 ]
164+ zipPath := filepath .Join (versionsPath , zipFileName )
169165
170166 // check if zip already exists
171- if _ , err := os .Stat (homeDir + "/.pvm/versions/" + zipFileName ); err == nil {
167+ if _ , err := os .Stat (zipPath ); err == nil {
172168 theme .Error (fmt .Sprintf ("PHP %s already exists" , desiredVersion ))
173169 return
174170 }
175171
176- // Create the file
177- out , err := os .Create (homeDir + "/.pvm/versions/" + zipFileName )
178- if err != nil {
179- log .Fatalln (err )
180- }
181-
182- // Write the body to file
183- _ , err = io .Copy (out , downloadResponse .Body )
184-
185- if err != nil {
186- out .Close ()
187- log .Fatalln (err )
172+ // Get the data
173+ if _ , err := downloadFile (zipUrl , zipPath ); err != nil {
174+ log .Fatalf ("Error while downloading PHP from %v: %v!" , zipUrl , err )
188175 }
189176
190- // Close the file
191- out .Close ()
192-
193177 // extract the zip file to a folder
178+ phpFolder := strings .Replace (zipFileName , ".zip" , "" , - 1 )
179+ phpPath := filepath .Join (versionsPath , phpFolder )
194180 theme .Info ("Unzipping" )
195- Unzip (homeDir + "/.pvm/versions/" + zipFileName , homeDir + "/.pvm/versions/" + strings . Replace ( zipFileName , ".zip" , "" , - 1 ) )
181+ Unzip (zipPath , phpPath )
196182
197183 // remove the zip file
198184 theme .Info ("Cleaning up" )
199- err = os .Remove (homeDir + "/.pvm/versions/" + zipFileName )
185+ err = os .Remove (zipPath )
200186 if err != nil {
201187 log .Fatalln (err )
202188 }
203189
190+ // install composer
191+ composerFolderPath := filepath .Join (phpPath , "composer" )
192+ if _ , err := os .Stat (composerFolderPath ); os .IsNotExist (err ) {
193+ theme .Info ("Creating .pvm/versions folder in home directory" )
194+ os .Mkdir (composerFolderPath , 0755 )
195+ }
196+
197+ composerPath := filepath .Join (composerFolderPath , "composer.phar" )
198+ composerUrl := "https://getcomposer.org/download/latest-stable/composer.phar"
199+ if desiredVersion .LessThan (common.Version {Major : 7 , Minor : 2 }) {
200+ composerUrl = "https://getcomposer.org/download/latest-2.2.x/composer.phar"
201+ }
202+
203+ if _ , err := downloadFile (composerUrl , composerPath ); err != nil {
204+ log .Fatalf ("Error while downloading Composer from %v: %v!" , composerUrl , err )
205+ }
206+
204207 theme .Success (fmt .Sprintf ("Finished installing PHP %s" , desiredVersion ))
205208}
206209
@@ -316,3 +319,30 @@ func FindLatestMinor(versions []common.Version, major int, threadSafe bool) comm
316319
317320 return latestMinor
318321}
322+
323+ func downloadFile (fileUrl string , filePath string ) (bool , error ) {
324+ downloadResponse , err := http .Get (fileUrl )
325+ if err != nil {
326+ return false , err
327+ }
328+
329+ defer downloadResponse .Body .Close ()
330+
331+ // Create the file
332+ out , err := os .Create (filePath )
333+ if err != nil {
334+ return false , err
335+ }
336+
337+ // Write the body to file
338+ _ , err = io .Copy (out , downloadResponse .Body )
339+
340+ if err != nil {
341+ out .Close ()
342+ return false , err
343+ }
344+
345+ // Close the file
346+ out .Close ()
347+ return true , nil
348+ }
0 commit comments