@@ -32,7 +32,7 @@ private function execute(Process $process): void
3232 });
3333
3434 if (0 !== $ process ->getExitCode ()) {
35- throw new \ RuntimeException ( sprintf ('Process exited unexpectedly. %s ' , $ process ->getCommandLine () ));
35+ throw new ComposerFailureException ( $ process -> getCommandLine (), sprintf ('Process exited unexpectedly with output: %s ' , $ process ->getErrorOutput ()), $ process -> getExitCode ( ));
3636 }
3737 }
3838
@@ -86,14 +86,76 @@ public function minimumStability(string $stability): void
8686 );
8787 }
8888
89+ private function clearVendor (): void
90+ {
91+ $ iterator = new \AppendIterator ();
92+
93+ try {
94+ $ iterator ->append (new \GlobIterator ($ this ->workdir .'/composer.* ' , \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::KEY_AS_PATHNAME ));
95+ $ iterator ->append (new \RecursiveIteratorIterator (
96+ new \RecursiveDirectoryIterator (
97+ $ this ->workdir .'/vendor ' ,
98+ \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS
99+ ),
100+ \RecursiveIteratorIterator::CHILD_FIRST ,
101+ ));
102+ } catch (\UnexpectedValueException $ e ) {
103+ $ this ->logger ->warning ($ e ->getMessage ());
104+ }
105+
106+ foreach ($ iterator as $ file ) {
107+ 'dir ' === $ file ->getType () ? rmdir ($ file ->getPathname ()) : unlink ($ file ->getPathname ());
108+ }
109+ }
110+
89111 public function init (string $ name ): void
90112 {
113+ if (file_exists ($ this ->workdir .'/composer.json ' )) {
114+ if (filesize ($ this ->workdir .'/composer.json ' ) <= 2 ) {
115+ $ this ->clearVendor ();
116+ } else {
117+ try {
118+ $ this ->allowPlugins ('php-http/discovery ' );
119+
120+ return ;
121+ } catch (ComposerFailureException ) {
122+ $ this ->clearVendor ();
123+ }
124+ }
125+ }
126+
91127 $ this ->command (
92128 'composer ' ,
93129 'init ' ,
94130 '--no-interaction ' ,
95131 sprintf ('--name=%s ' , $ name ),
96132 );
133+
134+ $ this ->allowPlugins ('php-http/discovery ' );
135+ }
136+
137+ public function allowPlugins (string ...$ plugins ): void
138+ {
139+ foreach ($ plugins as $ packageName ) {
140+ $ this ->command (
141+ 'composer ' ,
142+ 'config ' ,
143+ sprintf ('allow-plugins.%s ' , $ packageName ),
144+ 'true ' ,
145+ );
146+ }
147+ }
148+
149+ public function denyPlugins (string ...$ plugins ): void
150+ {
151+ foreach ($ plugins as $ packageName ) {
152+ $ this ->command (
153+ 'composer ' ,
154+ 'config ' ,
155+ sprintf ('allow-plugins.%s ' , $ packageName ),
156+ 'false ' ,
157+ );
158+ }
97159 }
98160
99161 /**
0 commit comments