13
13
14
14
use Composer \Package \BasePackage ;
15
15
use Composer \Script \Event ;
16
+ use ErrorException ;
16
17
use Exception ;
17
18
use Symfony \Component \Filesystem \Exception \IOException ;
18
19
use Symfony \Component \Filesystem \Filesystem ;
@@ -28,21 +29,24 @@ abstract class AbstractCopyPlugin implements CopyInterface
28
29
*
29
30
* @var BasePackage[]
30
31
*/
31
- public array $ installedComposerPackages = [];
32
+ public array $ composerPackages = [];
32
33
33
34
/**
34
35
* Packages installad via Unpkg downloadd
35
36
*
36
37
* @var array<string, array<string, string>>
37
38
*/
38
- public array $ installedUnpkgPackages = [];
39
+ public array $ unpkgPackages = [];
39
40
40
41
/**
41
42
* Composer event
42
43
*/
43
- protected Event $ event ;
44
+ protected ? Event $ event ;
44
45
45
- public function __construct (Event $ event )
46
+ /**
47
+ * @codeCoverageIgnore
48
+ */
49
+ public function __construct (?Event $ event )
46
50
{
47
51
$ this ->event = $ event ;
48
52
}
@@ -54,12 +58,8 @@ public function __construct(Event $event)
54
58
*/
55
59
public function processComposerInstall (): void
56
60
{
57
- if (!$ this instanceof CopyFromComposerInterface) {
58
- return ;
59
- }
60
-
61
61
$ package = $ this ->getComposerPackage ();
62
- if (!$ package ) {
62
+ if (!$ package || ! $ this instanceof CopyFromComposerInterface ) {
63
63
return ;
64
64
}
65
65
@@ -70,11 +70,12 @@ public function processComposerInstall(): void
70
70
$ this ->getComposerSource (),
71
71
);
72
72
73
- $ filesystem = new Filesystem ();
73
+ $ event = $ this ->getEvent ();
74
+ $ filesystem = $ this ->getFileSystem ();
74
75
75
76
if (!$ filesystem ->exists ($ copySourcePath ) && $ this instanceof CopyFromUnpkgInterface) {
76
- if ($ this -> event ->getIO ()->isVerbose ()) {
77
- $ this -> event ->getIO ()->write (sprintf (
77
+ if ($ event && $ event ->getIO ()->isVerbose ()) {
78
+ $ event ->getIO ()->write (sprintf (
78
79
'Fallback to Unpkg %s for %s ' ,
79
80
$ this ->getUnpkgName (),
80
81
$ this ->getComposerName (),
@@ -95,11 +96,13 @@ public function processComposerInstall(): void
95
96
96
97
try {
97
98
$ filesystem ->copy ($ copySource , $ copytarget );
98
- if ($ this ->event ->getIO ()->isVeryVerbose ()) {
99
- $ this ->event ->getIO ()->write (sprintf ('Copy %s to %s ' , $ copySource , $ copytarget ));
99
+ if ($ event && $ event ->getIO ()->isVeryVerbose ()) {
100
+ $ event ->getIO ()->write (sprintf ('Copy %s to %s ' , $ copySource , $ copytarget ));
101
+ }
102
+ } catch (IOException $ exception ) {
103
+ if ($ event ) {
104
+ $ event ->getIO ()->write ($ exception ->getMessage ());
100
105
}
101
- } catch (IOException $ IOException ) {
102
- $ this ->event ->getIO ()->write ($ IOException ->getMessage ());
103
106
}
104
107
}
105
108
}
@@ -109,18 +112,15 @@ public function processComposerInstall(): void
109
112
*/
110
113
public function processUnpkgInstall (): void
111
114
{
112
- if (!$ this instanceof CopyFromUnpkgInterface) {
113
- return ;
114
- }
115
-
116
- if (!$ this ->getUnpkgVersion ()) {
115
+ if (!$ this instanceof CopyFromUnpkgInterface || !$ this ->getUnpkgVersion ()) {
117
116
return ;
118
117
}
119
118
119
+ $ event = $ this ->getEvent ();
120
120
$ sourcePath = $ this ->getUnpkSourcePath ();
121
121
122
- if ($ this -> event ->getIO ()->isVerbose ()) {
123
- $ this -> event ->getIO ()->write (sprintf (
122
+ if ($ event && $ event ->getIO ()->isVerbose ()) {
123
+ $ event ->getIO ()->write (sprintf (
124
124
'Trying to download %s %s from %s ' ,
125
125
$ this ->getUnpkgName (),
126
126
$ this ->getUnpkgVersion (),
@@ -132,57 +132,69 @@ public function processUnpkgInstall(): void
132
132
$ sourceFilePath = $ sourcePath . $ fileName ;
133
133
try {
134
134
$ content = file_get_contents ($ sourceFilePath );
135
- } catch (\ErrorException $ errorException ) {
136
- $ this ->event ->getIO ()->write ($ errorException ->getMessage ());
135
+ } catch (ErrorException $ errorException ) {
136
+ if ($ event ) {
137
+ $ event ->getIO ()->write ($ errorException ->getMessage ());
138
+ }
137
139
return ;
138
140
}
139
141
140
142
if (!$ content ) {
141
- $ this ->event ->getIO ()->write (sprintf ('Could not read from %s ' , $ sourceFilePath ));
143
+ if ($ event ) {
144
+ $ event ->getIO ()->write (sprintf ('Could not read from %s ' , $ sourceFilePath ));
145
+ }
142
146
return ;
143
147
}
144
148
145
149
try {
146
- $ filesystem = new Filesystem ();
147
150
$ targetFilePath = $ this ->getCopyTargetPath () . '/ ' . $ fileName ;
148
- $ filesystem ->dumpFile ($ targetFilePath , $ content );
149
- if ($ this ->event ->getIO ()->isVerbose ()) {
150
- $ this ->event ->getIO ()->write (sprintf ('Added %s ' , $ targetFilePath ));
151
+ $ this ->getFileSystem ()->dumpFile ($ targetFilePath , $ content );
152
+ if ($ event && $ event ->getIO ()->isVerbose ()) {
153
+ $ event ->getIO ()->write (sprintf ('Added %s ' , $ targetFilePath ));
154
+ }
155
+ } catch (IOException $ exception ) {
156
+ if ($ event ) {
157
+ $ event ->getIO ()->write ($ exception ->getMessage ());
151
158
}
152
- } catch (IOException $ IOException ) {
153
- $ this ->event ->getIO ()->write ($ IOException ->getMessage ());
154
159
return ;
155
160
}
156
161
}
157
162
}
158
163
159
164
public function getComposerPackage (): ?BasePackage
160
165
{
161
- if ($ this instanceof CopyFromComposerInterface) {
162
- $ vendorName = $ this ->getComposerName ();
163
- $ module = $ this ->getInstalledComposerPackage ($ vendorName );
164
- if ($ module ) {
165
- return $ module ;
166
- }
166
+ if (!$ this instanceof CopyFromComposerInterface) {
167
+ return null ;
168
+ }
169
+
170
+ $ vendorName = $ this ->getComposerName ();
171
+ $ module = $ this ->getInstalledComposerPackage ($ vendorName );
172
+ if ($ module ) {
173
+ return $ module ;
174
+ }
175
+
176
+ $ event = $ this ->getEvent ();
177
+ if (!$ event ) {
178
+ return null ;
179
+ }
167
180
168
- $ locker = $ this -> event ->getComposer ()->getLocker ();
169
- $ repo = $ locker ->getLockedRepository ();
181
+ $ locker = $ event ->getComposer ()->getLocker ();
182
+ $ repo = $ locker ->getLockedRepository ();
170
183
171
- foreach ($ repo ->getPackages () as $ package ) {
172
- if ($ package ->getName () === $ vendorName ) {
173
- $ this ->setInstalledComposerPackage ($ vendorName , $ package );
174
- if ($ this ->event ->getIO ()->isVerbose ()) {
175
- $ this ->event ->getIO ()->write (sprintf ('%s found with version %s ' , $ vendorName , $ package ->getVersion ()));
176
- }
177
- return $ this ->getInstalledComposerPackage ($ vendorName );
184
+ foreach ($ repo ->getPackages () as $ package ) {
185
+ if ($ package ->getName () === $ vendorName ) {
186
+ $ this ->setInstalledComposerPackage ($ vendorName , $ package );
187
+ if ($ event ->getIO ()->isVerbose ()) {
188
+ $ event ->getIO ()->write (sprintf ('%s found with version %s ' , $ vendorName , $ package ->getVersion ()));
178
189
}
190
+ return $ this ->getInstalledComposerPackage ($ vendorName );
179
191
}
180
192
}
181
193
return null ;
182
194
}
183
195
184
196
/**
185
- * Get path to NPM dist
197
+ * Get path to Unpkg dist
186
198
*/
187
199
protected function getUnpkSourcePath (): string
188
200
{
@@ -212,8 +224,13 @@ protected function getCwd(): string
212
224
*/
213
225
protected function getVendorDirectoryFromComposer (): string
214
226
{
227
+ $ event = $ this ->getEvent ();
228
+ if (!$ event ) {
229
+ return '' ;
230
+ }
231
+
215
232
/** @var string $vendorDir */
216
- $ vendorDir = $ this -> event ->getComposer ()->getConfig ()->get (self ::VENDOR_DIR );
233
+ $ vendorDir = $ event ->getComposer ()->getConfig ()->get (self ::VENDOR_DIR );
217
234
return $ vendorDir ;
218
235
}
219
236
@@ -222,7 +239,12 @@ protected function getVendorDirectoryFromComposer(): string
222
239
*/
223
240
protected function getMageRootDirectoryFromComposer (): string
224
241
{
225
- $ composerExtra = $ this ->event ->getComposer ()->getPackage ()->getExtra ();
242
+ $ event = $ this ->getEvent ();
243
+ if (!$ event ) {
244
+ return '' ;
245
+ }
246
+
247
+ $ composerExtra = $ event ->getComposer ()->getPackage ()->getExtra ();
226
248
$ magentoRootDir = '' ;
227
249
228
250
if (array_key_exists (self ::EXTRA_MAGENTO_ROOT_DIR , $ composerExtra ) &&
@@ -245,11 +267,21 @@ private function getCopyTargetPath(): string
245
267
246
268
protected function getInstalledComposerPackage (string $ vendorName ): ?BasePackage
247
269
{
248
- return $ this ->installedComposerPackages [$ vendorName ] ?? null ;
270
+ return $ this ->composerPackages [$ vendorName ] ?? null ;
249
271
}
250
272
251
273
private function setInstalledComposerPackage (string $ vendorName , BasePackage $ package ): void
252
274
{
253
- $ this ->installedComposerPackages [$ vendorName ] = $ package ;
275
+ $ this ->composerPackages [$ vendorName ] = $ package ;
276
+ }
277
+
278
+ public function getFileSystem (): Filesystem
279
+ {
280
+ return new Filesystem ();
281
+ }
282
+
283
+ public function getEvent (): ?Event
284
+ {
285
+ return $ this ->event ;
254
286
}
255
287
}
0 commit comments