5
5
6
6
use Plaisio \Console \Helper \Assets \AssetsPlaisioXmlHelper ;
7
7
use Plaisio \Console \Helper \Assets \AssetsStore ;
8
+ use Plaisio \Console \Helper \ConfigException ;
8
9
use Plaisio \Console \Helper \PlaisioXmlUtility ;
9
- use SetBased \Exception \RuntimeException ;
10
10
use Symfony \Component \Console \Input \InputInterface ;
11
11
use Symfony \Component \Console \Output \OutputInterface ;
12
12
use Webmozart \PathUtil \Path ;
17
17
class AssetsCommand extends PlaisioCommand
18
18
{
19
19
//--------------------------------------------------------------------------------------------------------------------
20
- /**
21
- * All possible assets types.
22
- *
23
- * @var string[]
24
- */
25
- private $ assetTypes = ['css ' , 'images ' , 'js ' ];
26
-
27
20
/**
28
21
* File count.
29
22
*
@@ -63,19 +56,29 @@ protected function execute(InputInterface $input, OutputInterface $output)
63
56
{
64
57
$ this ->io ->title ('Plaisio: Assets ' );
65
58
66
- $ this ->readResourceDir ();
67
- $ this ->createStore ();
68
- $ this ->findAssets ();
69
- $ this ->readCurrentAssets ();
70
- $ this ->assetsRemoveObsolete ();
71
- $ this ->assetsUpdateCurrent ();
72
- $ this ->assetsAddNew ();
73
- $ this ->writeCurrentAssets ();
59
+ try
60
+ {
61
+ $ this ->readResourceDir ();
62
+ $ this ->createStore ();
63
+ $ this ->findAssets ();
64
+ $ this ->readCurrentAssets ();
65
+ $ this ->assetsRemoveObsolete ();
66
+ $ this ->assetsUpdateCurrent ();
67
+ $ this ->assetsAddNew ();
68
+ $ this ->writeCurrentAssets ();
69
+
70
+ $ this ->io ->text (sprintf ('Removed %d, updated %d, and added %d assets ' ,
71
+ $ this ->count ['old ' ],
72
+ $ this ->count ['current ' ],
73
+ $ this ->count ['new ' ]));
74
+ }
75
+ catch (ConfigException $ exception )
76
+ {
77
+ $ this ->io ->error ($ exception ->getMessage ());
78
+ $ this ->io ->logVerbose ($ exception ->getTraceAsString ());
74
79
75
- $ this ->io ->text (sprintf ('Removed %d, updated %d, and added %d assets ' ,
76
- $ this ->count ['old ' ],
77
- $ this ->count ['current ' ],
78
- $ this ->count ['new ' ]));
80
+ return -1 ;
81
+ }
79
82
80
83
return 0 ;
81
84
}
@@ -89,8 +92,8 @@ private function assetsAddNew(): void
89
92
$ assets = $ this ->store ->plsDiffNew ();
90
93
foreach ($ assets as $ asset )
91
94
{
92
- $ pathSource = Path:: join ($ asset[ ' ass_base_dir ' ], $ asset [ ' ass_path ' ] );
93
- $ pathDest = Path:: join ( $ this ->rootAssetDir , $ asset[ ' ass_type ' ], $ asset [ ' ass_path ' ] );
95
+ $ pathSource = $ this -> composeSourcePath ($ asset );
96
+ $ pathDest = $ this ->composeDestPath ( $ asset );
94
97
95
98
$ this ->io ->logVerbose ('Adding asset <fso>%s</fso> ' , $ pathDest );
96
99
@@ -99,6 +102,7 @@ private function assetsAddNew(): void
99
102
$ this ->store ->insertRow ('PLS_CURRENT ' , ['cur_id ' => null ,
100
103
'cur_type ' => $ asset ['ass_type ' ],
101
104
'cur_base_dir ' => $ asset ['ass_base_dir ' ],
105
+ 'cur_to_dir ' => $ asset ['ass_to_dir ' ],
102
106
'cur_path ' => $ asset ['ass_path ' ]]);
103
107
$ this ->count ['new ' ]++;
104
108
}
@@ -113,7 +117,7 @@ private function assetsRemoveObsolete(): void
113
117
$ assets = $ this ->store ->plsDiffObsolete ();
114
118
foreach ($ assets as $ asset )
115
119
{
116
- $ path = Path:: join ( $ this ->rootAssetDir , $ asset[ ' ass_type ' ], $ asset [ ' ass_path ' ] );
120
+ $ path = $ this ->composeDestPath ( $ asset );
117
121
if (file_exists ($ path ))
118
122
{
119
123
$ this ->io ->logVerbose ('Removing obsolete asset <fso>%s</fso> ' , $ path );
@@ -123,7 +127,10 @@ private function assetsRemoveObsolete(): void
123
127
$ this ->count ['old ' ]++;
124
128
}
125
129
126
- $ this ->store ->plsCurrentAssetsDeleteAsset ($ asset ['ass_type ' ], $ asset ['ass_base_dir ' ], $ asset ['ass_path ' ]);
130
+ $ this ->store ->plsCurrentAssetsDeleteAsset ($ asset ['ass_type ' ],
131
+ $ asset ['ass_base_dir ' ],
132
+ $ asset ['ass_to_dir ' ],
133
+ $ asset ['ass_path ' ]);
127
134
}
128
135
}
129
136
@@ -136,8 +143,8 @@ private function assetsUpdateCurrent(): void
136
143
$ assets = $ this ->store ->plsDiffCurrent ();
137
144
foreach ($ assets as $ asset )
138
145
{
139
- $ pathSource = Path:: join ($ asset[ ' ass_base_dir ' ], $ asset [ ' ass_path ' ] );
140
- $ pathDest = Path:: join ( $ this ->rootAssetDir , $ asset[ ' ass_type ' ], $ asset [ ' ass_path ' ] );
146
+ $ pathSource = $ this -> composeSourcePath ($ asset );
147
+ $ pathDest = $ this ->composeDestPath ( $ asset );
141
148
142
149
$ source = file_get_contents ($ pathSource );
143
150
if (file_exists ($ pathDest ))
@@ -160,6 +167,32 @@ private function assetsUpdateCurrent(): void
160
167
}
161
168
}
162
169
170
+ //--------------------------------------------------------------------------------------------------------------------
171
+ /**
172
+ * Composes the destination path of an asset.
173
+ *
174
+ * @param array $asset The details of the asset.
175
+ *
176
+ * @return string
177
+ */
178
+ private function composeDestPath (array $ asset ): string
179
+ {
180
+ return Path::join ($ this ->rootAssetDir , $ asset ['ass_type ' ], $ asset ['ass_to_dir ' ] ?? '' , $ asset ['ass_path ' ]);
181
+ }
182
+
183
+ //--------------------------------------------------------------------------------------------------------------------
184
+ /**
185
+ * Composes the source path of an asset.
186
+ *
187
+ * @param array $asset The details of the asset.
188
+ *
189
+ * @return string
190
+ */
191
+ private function composeSourcePath (array $ asset ): string
192
+ {
193
+ return Path::join ($ asset ['ass_base_dir ' ], $ asset ['ass_path ' ]);
194
+ }
195
+
163
196
//--------------------------------------------------------------------------------------------------------------------
164
197
/**
165
198
* Copies a file.
@@ -190,30 +223,53 @@ private function createStore(): void
190
223
//--------------------------------------------------------------------------------------------------------------------
191
224
/**
192
225
* Finds all assets all packages (using plaisio-assets.xml files).
226
+ *
227
+ * @throws ConfigException
193
228
*/
194
229
private function findAssets (): void
195
230
{
196
231
$ plaisioXmlList = PlaisioXmlUtility::findPlaisioXmlPackages ('assets ' );
197
232
foreach ($ plaisioXmlList as $ plaisioConfigPath )
198
233
{
199
- $ helper = new AssetsPlaisioXmlHelper ($ plaisioConfigPath );
200
- foreach ($ this ->assetTypes as $ assetType )
234
+ $ helper = new AssetsPlaisioXmlHelper ($ plaisioConfigPath );
235
+ $ collections = $ helper ->queryAssetFileList ();
236
+ if (!empty ($ collections ))
201
237
{
202
- $ files = $ helper ->queryFileList ($ assetType );
203
- if (!empty ($ files ))
238
+ foreach ($ collections as $ collection )
204
239
{
205
- foreach ($ files ['files ' ] as $ file )
240
+ foreach ($ collection ['files ' ] as $ file )
206
241
{
207
- $ this ->io ->logVerbose ('Found asset <fso>%s</fso> ' , Path::join ($ files ['base-dir ' ], $ file ));
242
+ $ this ->io ->logVerbose ('Found asset <fso>%s</fso> ' , Path::join ($ collection ['base-dir ' ], $ file ));
208
243
209
244
$ this ->store ->insertRow ('PLS_ASSET ' , ['ass_id ' => null ,
210
- 'ass_type ' => $ files ['type ' ],
211
- 'ass_base_dir ' => $ files ['base-dir ' ],
245
+ 'ass_type ' => $ collection ['type ' ],
246
+ 'ass_base_dir ' => $ collection ['base-dir ' ],
247
+ 'ass_to_dir ' => $ collection ['to-dir ' ],
212
248
'ass_path ' => $ file ]);
213
249
}
214
250
}
215
251
}
216
252
}
253
+
254
+ $ plaisioConfigPath = PlaisioXmlUtility::plaisioXmlPath ('assets ' );
255
+ $ helper = new AssetsPlaisioXmlHelper ($ plaisioConfigPath );
256
+ $ collections = $ helper ->queryOtherAssetFileList ();
257
+ if (!empty ($ collections ))
258
+ {
259
+ foreach ($ collections as $ collection )
260
+ {
261
+ foreach ($ collection ['files ' ] as $ file )
262
+ {
263
+ $ this ->io ->logVerbose ('Found asset <fso>%s</fso> ' , Path::join ($ collection ['base-dir ' ], $ file ));
264
+
265
+ $ this ->store ->insertRow ('PLS_ASSET ' , ['ass_id ' => null ,
266
+ 'ass_type ' => $ collection ['type ' ],
267
+ 'ass_base_dir ' => $ collection ['base-dir ' ],
268
+ 'ass_to_dir ' => $ collection ['to-dir ' ],
269
+ 'ass_path ' => $ file ]);
270
+ }
271
+ }
272
+ }
217
273
}
218
274
219
275
//--------------------------------------------------------------------------------------------------------------------
@@ -232,7 +288,8 @@ private function readCurrentAssets(): void
232
288
$ this ->store ->insertRow ('PLS_CURRENT ' , ['cur_id ' => null ,
233
289
'cur_type ' => $ data [0 ],
234
290
'cur_base_dir ' => $ data [1 ],
235
- 'cur_path ' => $ data [2 ]]);
291
+ 'cur_to_dir ' => $ data [2 ],
292
+ 'cur_path ' => $ data [3 ]]);
236
293
}
237
294
fclose ($ handle );
238
295
}
@@ -250,7 +307,7 @@ private function readResourceDir(): void
250
307
251
308
if (!file_exists ($ this ->rootAssetDir ))
252
309
{
253
- throw new RuntimeException ("Asset root directory '%s' does not exists " , $ this ->rootAssetDir );
310
+ throw new ConfigException ("Asset root directory '%s' does not exists " , $ this ->rootAssetDir );
254
311
}
255
312
}
256
313
@@ -268,7 +325,7 @@ private function writeCurrentAssets(): void
268
325
$ handle = fopen ($ path1 , 'w ' );
269
326
foreach ($ assets as $ asset )
270
327
{
271
- fputcsv ($ handle , [$ asset ['cur_type ' ], $ asset ['cur_base_dir ' ], $ asset ['cur_path ' ]]);
328
+ fputcsv ($ handle , [$ asset ['cur_type ' ], $ asset ['cur_base_dir ' ], $ asset ['cur_to_dir ' ], $ asset [ ' cur_path ' ]]);
272
329
}
273
330
274
331
$ new = file_get_contents ($ path1 );
0 commit comments