@@ -31,16 +31,11 @@ abstract class AbstractCopyPlugin implements CopyInterface
31
31
public array $ installedComposerPackages = [];
32
32
33
33
/**
34
- * Packages installad via NPM downloadd
34
+ * Packages installad via Unpkg downloadd
35
35
*
36
36
* @var array<string, array<string, string>>
37
37
*/
38
- public array $ installedNpmPackages = [];
39
-
40
- /**
41
- * Package version
42
- */
43
- public ?string $ version = null ;
38
+ public array $ installedUnpkgPackages = [];
44
39
45
40
/**
46
41
* Composer event
@@ -59,101 +54,112 @@ public function __construct(Event $event)
59
54
*/
60
55
public function processComposerInstall (): void
61
56
{
62
- $ copySourcePath = null ;
63
- if ($ this instanceof CopyFromComposerInterface) {
64
- $ package = $ this ->getComposerPackage ();
65
- if (!$ package ) {
66
- return ;
67
- }
68
- $ copySourcePath = $ this ->getCopySourcePath ();
57
+ if (!$ this instanceof CopyFromComposerInterface) {
58
+ return ;
69
59
}
70
60
61
+ $ package = $ this ->getComposerPackage ();
62
+ if (!$ package ) {
63
+ return ;
64
+ }
65
+
66
+ $ copySourcePath = sprintf (
67
+ '%s/%s/%s ' ,
68
+ $ this ->getVendorDirectoryFromComposer (),
69
+ $ this ->getComposerName (),
70
+ $ this ->getComposerSource (),
71
+ );
72
+
71
73
$ filesystem = new Filesystem ();
72
74
73
- if ($ this instanceof CopyFromNpmInterface && (! $ copySourcePath || ! $ filesystem ->exists ($ copySourcePath )) ) {
75
+ if (! $ filesystem ->exists ($ copySourcePath ) && $ this instanceof CopyFromUnpkgInterface ) {
74
76
if ($ this ->event ->getIO ()->isVerbose ()) {
75
- $ this ->event ->getIO ()->write (sprintf ('Fallback to NPM for %s ' , $ this ->getNpmPackageName ()));
77
+ $ this ->event ->getIO ()->write (sprintf (
78
+ 'Fallback to Unpkg %s for %s ' ,
79
+ $ this ->getUnpkgName (),
80
+ $ this ->getComposerName (),
81
+ ));
76
82
}
77
- $ this ->processNpmInstall ();
83
+ $ this ->processUnpkgInstall ();
78
84
return ;
79
85
}
80
86
81
- if ($ copySourcePath && $ this instanceof CopyFromComposerInterface) {
82
- $ finder = Finder::create ()
83
- ->files ()
84
- ->in ($ copySourcePath )
85
- ->name ($ this ->getFilesByName ());
87
+ $ finder = Finder::create ()
88
+ ->files ()
89
+ ->in ($ copySourcePath )
90
+ ->name ($ this ->getComposerFiles ());
86
91
87
- foreach ($ finder as $ file ) {
88
- $ copySource = $ file ->getPathname ();
89
- $ copytarget = $ this ->getCopyTargetPath () . '/ ' . $ file ->getRelativePathname ();
92
+ foreach ($ finder as $ file ) {
93
+ $ copySource = $ file ->getPathname ();
94
+ $ copytarget = $ this ->getCopyTargetPath () . '/ ' . $ file ->getRelativePathname ();
90
95
91
- try {
92
- $ filesystem ->copy ($ copySource , $ copytarget );
93
- if ($ this ->event ->getIO ()->isVeryVerbose ()) {
94
- $ this ->event ->getIO ()->write (sprintf ('Copy %s to %s ' , $ copySource , $ copytarget ));
95
- }
96
- } catch (IOException $ IOException ) {
97
- $ this ->event ->getIO ()->write ($ IOException ->getMessage ());
96
+ try {
97
+ $ filesystem ->copy ($ copySource , $ copytarget );
98
+ if ($ this ->event ->getIO ()->isVeryVerbose ()) {
99
+ $ this ->event ->getIO ()->write (sprintf ('Copy %s to %s ' , $ copySource , $ copytarget ));
98
100
}
101
+ } catch (IOException $ IOException ) {
102
+ $ this ->event ->getIO ()->write ($ IOException ->getMessage ());
99
103
}
100
104
}
101
105
}
102
106
103
107
/**
104
- * Copy files as defined in NPM copy-plugin
108
+ * Copy files as defined in Unpkg copy-plugin
105
109
*/
106
- public function processNpmInstall (): void
110
+ public function processUnpkgInstall (): void
107
111
{
108
- if ($ this instanceof CopyFromNpmInterface) {
109
- if (!$ this ->getVersion ()) {
110
- return ;
111
- }
112
+ if (!$ this instanceof CopyFromUnpkgInterface) {
113
+ return ;
114
+ }
115
+
116
+ if (!$ this ->getUnpkgVersion ()) {
117
+ return ;
118
+ }
112
119
113
- $ sourcePath = $ this ->getNpmFilePath ();
120
+ $ sourcePath = $ this ->getUnpkSourcePath ();
114
121
115
- if ($ this ->event ->getIO ()->isVerbose ()) {
116
- $ this ->event ->getIO ()->write (sprintf (
117
- 'Trying to download %s %s from %s ' ,
118
- $ this ->getNpmPackageName (),
119
- $ this ->getVersion (),
120
- $ sourcePath ,
121
- ));
122
- }
122
+ if ($ this ->event ->getIO ()->isVerbose ()) {
123
+ $ this ->event ->getIO ()->write (sprintf (
124
+ 'Trying to download %s %s from %s ' ,
125
+ $ this ->getUnpkgName (),
126
+ $ this ->getUnpkgVersion (),
127
+ $ sourcePath ,
128
+ ));
129
+ }
123
130
124
- foreach ($ this ->getNpmPackageFiles () as $ fileName ) {
125
- $ sourceFilePath = $ sourcePath . $ fileName ;
126
- try {
127
- $ content = file_get_contents ($ sourceFilePath );
128
- } catch (\ErrorException $ errorException ) {
129
- $ this ->event ->getIO ()->write ($ errorException ->getMessage ());
130
- return ;
131
- }
131
+ foreach ($ this ->getUnpkgFiles () as $ fileName ) {
132
+ $ sourceFilePath = $ sourcePath . $ fileName ;
133
+ try {
134
+ $ content = file_get_contents ($ sourceFilePath );
135
+ } catch (\ErrorException $ errorException ) {
136
+ $ this ->event ->getIO ()->write ($ errorException ->getMessage ());
137
+ return ;
138
+ }
132
139
133
- if (!$ content ) {
134
- $ this ->event ->getIO ()->write (sprintf ('Could not read from %s ' , $ sourceFilePath ));
135
- return ;
136
- }
140
+ if (!$ content ) {
141
+ $ this ->event ->getIO ()->write (sprintf ('Could not read from %s ' , $ sourceFilePath ));
142
+ return ;
143
+ }
137
144
138
- try {
139
- $ filesystem = new Filesystem ();
140
- $ targetFilePath = $ this ->getCopyTargetPath () . '/ ' . $ fileName ;
141
- $ filesystem ->dumpFile ($ targetFilePath , $ content );
142
- if ($ this ->event ->getIO ()->isVerbose ()) {
143
- $ this ->event ->getIO ()->write (sprintf ('Added %s ' , $ fileName ));
144
- }
145
- } catch (IOException $ IOException ) {
146
- $ this ->event ->getIO ()->write ($ IOException ->getMessage ());
147
- return ;
145
+ try {
146
+ $ filesystem = new Filesystem ();
147
+ $ targetFilePath = $ this ->getCopyTargetPath () . '/ ' . $ fileName ;
148
+ $ filesystem ->dumpFile ($ targetFilePath , $ content );
149
+ if ($ this ->event ->getIO ()->isVerbose ()) {
150
+ $ this ->event ->getIO ()->write (sprintf ('Added %s ' , $ targetFilePath ));
148
151
}
152
+ } catch (IOException $ IOException ) {
153
+ $ this ->event ->getIO ()->write ($ IOException ->getMessage ());
154
+ return ;
149
155
}
150
156
}
151
157
}
152
158
153
159
public function getComposerPackage (): ?BasePackage
154
160
{
155
161
if ($ this instanceof CopyFromComposerInterface) {
156
- $ vendorName = $ this ->getComposerPackageName ();
162
+ $ vendorName = $ this ->getComposerName ();
157
163
$ module = $ this ->getInstalledComposerPackage ($ vendorName );
158
164
if ($ module ) {
159
165
return $ module ;
@@ -175,87 +181,20 @@ public function getComposerPackage(): ?BasePackage
175
181
return null ;
176
182
}
177
183
178
- /**
179
- * @return array<string, string>|null
180
- */
181
- public function getNpmPackage (): ?array
182
- {
183
- if ($ this instanceof CopyFromNpmInterface) {
184
- $ vendorName = $ this ->getNpmPackageName ();
185
-
186
- $ locker = $ this ->event ->getComposer ()->getLocker ();
187
- $ repo = $ locker ->getLockedRepository ();
188
-
189
- $ packages = $ repo ->getPackages ();
190
- $ packages [] = $ this ->event ->getComposer ()->getPackage ();
191
-
192
- foreach ($ packages as $ package ) {
193
- /** @var array<string, string|array<string>> $extra */
194
- $ extra = $ package ->getExtra ();
195
-
196
- if (!isset ($ extra [self ::EXTRA_NPM_PACKAGES ][$ vendorName ])) {
197
- continue ;
198
- }
199
-
200
- $ packageData = $ extra [self ::EXTRA_NPM_PACKAGES ][$ vendorName ];
201
-
202
- if (!is_array ($ packageData )) {
203
- throw new Exception (sprintf ('Configuration is invalid for %s ' , $ vendorName ));
204
- }
205
-
206
- if (array_key_exists ('version ' , $ packageData ) && is_string ($ packageData ['version ' ])) {
207
- $ this ->setInstalledNpmPackage ($ vendorName , $ packageData );
208
- if ($ this ->event ->getIO ()->isVerbose ()) {
209
- $ this ->event ->getIO ()->write (sprintf (
210
- '%s found with version %s ' ,
211
- $ vendorName ,
212
- $ packageData ['version ' ],
213
- ));
214
- }
215
- return $ this ->getInstalledNpmPackage ($ vendorName );
216
- }
217
- }
218
- }
219
- return null ;
220
- }
221
-
222
184
/**
223
185
* Get path to NPM dist
224
186
*/
225
- protected function getNpmFilePath (): string
187
+ protected function getUnpkSourcePath (): string
226
188
{
227
- if ($ this instanceof CopyFromNpmInterface ) {
189
+ if ($ this instanceof CopyFromUnpkgInterface ) {
228
190
$ search = ['{{package}} ' , '{{version}} ' ];
229
- $ replace = [$ this ->getNpmPackageName (), $ this ->getVersion ()];
230
- return str_replace ($ search , $ replace , CopyFromNpmInterface::NPM_FALLBACK_URL );
191
+ $ replace = [$ this ->getUnpkgName (), $ this ->getUnpkgVersion ()];
192
+ $ path = str_replace ($ search , $ replace , CopyFromUnpkgInterface::UNPKG_URL );
193
+ return $ path . ($ this ->getUnpkgSource () ? $ this ->getUnpkgSource () . '/ ' : '' );
231
194
}
232
195
return '' ;
233
196
}
234
197
235
- /**
236
- * Get package version
237
- */
238
- private function getVersion (): string
239
- {
240
- if (is_null ($ this ->version )) {
241
- $ version = '' ;
242
- switch (true ) {
243
- case $ this instanceof CopyFromComposerInterface:
244
- $ package = $ this ->getComposerPackage ();
245
- $ version = $ package ? $ package ->getPrettyVersion () : '' ;
246
- break ;
247
- case $ this instanceof CopyFromNpmInterface:
248
- $ package = $ this ->getNpmPackage ();
249
- $ version = $ package ? $ package ['version ' ] : '' ;
250
- break ;
251
- }
252
-
253
- $ this ->version = ltrim ($ version , 'v ' );
254
- }
255
-
256
- return $ this ->version ;
257
- }
258
-
259
198
/**
260
199
* Get current working directory
261
200
*/
@@ -294,20 +233,7 @@ protected function getMageRootDirectoryFromComposer(): string
294
233
return $ magentoRootDir ;
295
234
}
296
235
297
- protected function getCopySourcePath (): string
298
- {
299
- if ($ this instanceof CopyFromComposerInterface) {
300
- return sprintf (
301
- '%s/%s/%s ' ,
302
- $ this ->getVendorDirectoryFromComposer (),
303
- $ this ->getComposerPackageName (),
304
- $ this ->getCopySource (),
305
- );
306
- }
307
- return '' ;
308
- }
309
-
310
- protected function getCopyTargetPath (): string
236
+ private function getCopyTargetPath (): string
311
237
{
312
238
return sprintf (
313
239
'%s/%s%s ' ,
@@ -326,20 +252,4 @@ private function setInstalledComposerPackage(string $vendorName, BasePackage $pa
326
252
{
327
253
$ this ->installedComposerPackages [$ vendorName ] = $ package ;
328
254
}
329
-
330
- /**
331
- * @return array<string, string>|null
332
- */
333
- protected function getInstalledNpmPackage (string $ vendorName ): ?array
334
- {
335
- return $ this ->installedNpmPackages [$ vendorName ] ?? null ;
336
- }
337
-
338
- /**
339
- * @param array<string, string> $package
340
- */
341
- private function setInstalledNpmPackage (string $ vendorName , array $ package ): void
342
- {
343
- $ this ->installedNpmPackages [$ vendorName ] = $ package ;
344
- }
345
255
}
0 commit comments