@@ -88,29 +88,17 @@ public function process(ContainerBuilder $container): void
88
88
*/
89
89
private function handleThumbnailService (NodeFinder $ nodeFinder , array $ ast ): void
90
90
{
91
- $ createThumbnailsForSizesNode = $ this ->getClassMethod ($ nodeFinder , 'createThumbnailsForSizes ' , $ ast );
92
-
93
- // we don't need to generate the files, so we just return the array
94
- $ createThumbnailsForSizesNode ->stmts = (new ParserFactory ())->create (ParserFactory::PREFER_PHP7 )
95
- ->parse ('<?php if ($thumbnailSizes === null) {
96
- return [];
97
- }
98
-
99
- if ($thumbnailSizes->count() === 0) {
100
- return [];
101
- }
102
-
103
- $savedThumbnails = [];
104
-
105
- foreach ($thumbnailSizes as $size) {
106
- $savedThumbnails[] = [
107
- \'mediaId \' => $media->getId(),
108
- \'width \' => $size->getWidth(),
109
- \'height \' => $size->getHeight(),
110
- ];
111
- }
112
-
113
- return $savedThumbnails; ' );
91
+ try {
92
+ $ createThumbnailsForSizesNode = $ this ->getClassMethod ($ nodeFinder , 'createThumbnailsForSizes ' , $ ast );
93
+ $ this ->handleCreateThumbnailsForSizes ($ createThumbnailsForSizesNode );
94
+ } catch (\RuntimeException $ e ) {
95
+ if ($ e ->getMessage () === 'Method createThumbnailsForSizes in class Shopware\Core\Content\Media\Thumbnail\ThumbnailService is missing ' ) {
96
+ $ generateAndSaveNode = $ this ->getClassMethod ($ nodeFinder , 'generateAndSave ' , $ ast );
97
+ $ this ->handleGenerateAndSaveNode ($ generateAndSaveNode );
98
+ } else {
99
+ throw $ e ;
100
+ }
101
+ }
114
102
115
103
// the strict option is useless with this plugin, so this should always be false
116
104
$ updateThumbnailsNode = $ this ->getClassMethod ($ nodeFinder , 'updateThumbnails ' , $ ast );
@@ -232,4 +220,73 @@ private function getClassName(): string
232
220
233
221
return substr ($ lastOccur , 1 );
234
222
}
223
+
224
+ private function handleCreateThumbnailsForSizes (ClassMethod $ createThumbnailsForSizesNode ): void
225
+ {
226
+ // we don't need to generate the files, so we just return the array
227
+ $ createThumbnailsForSizesNode ->stmts = (new ParserFactory ())->create (ParserFactory::PREFER_PHP7 )
228
+ ->parse ('<?php if ($thumbnailSizes === null) {
229
+ return [];
230
+ }
231
+
232
+ if ($thumbnailSizes->count() === 0) {
233
+ return [];
234
+ }
235
+
236
+ $savedThumbnails = [];
237
+
238
+ foreach ($thumbnailSizes as $size) {
239
+ $savedThumbnails[] = [
240
+ \'mediaId \' => $media->getId(),
241
+ \'width \' => $size->getWidth(),
242
+ \'height \' => $size->getHeight(),
243
+ ];
244
+ }
245
+
246
+ return $savedThumbnails; ' );
247
+ }
248
+
249
+ private function handleGenerateAndSaveNode (ClassMethod $ generateAndSaveNode ): void
250
+ {
251
+ // we don't need to generate the files, so we just return the array
252
+ $ generateAndSaveNode ->stmts = (new ParserFactory ())->create (ParserFactory::PREFER_PHP7 )
253
+ ->parse ('<?php if ($sizes === null || $sizes->count() === 0) {
254
+ return [];
255
+ }
256
+
257
+ $records = [];
258
+
259
+ $type = $media->getMediaType();
260
+ if ($type === null) {
261
+ throw MediaException::mediaTypeNotLoaded($media->getId());
262
+ }
263
+
264
+ $mapped = [];
265
+ foreach ($sizes as $size) {
266
+ $id = Uuid::randomHex();
267
+
268
+ $mapped[$size->getId()] = $id;
269
+
270
+ $records[] = [
271
+ \'id \' => $id,
272
+ \'mediaId \' => $media->getId(),
273
+ \'width \' => $size->getWidth(),
274
+ \'height \' => $size->getHeight(),
275
+ ];
276
+ }
277
+
278
+ // write thumbnail records to trigger path generation afterward
279
+ $context->scope(Context::SYSTEM_SCOPE, function ($context) use ($records): void {
280
+ $context->addState(EntityIndexerRegistry::DISABLE_INDEXING);
281
+
282
+ $this->thumbnailRepository->create($records, $context);
283
+ });
284
+
285
+ $ids = \array_column($records, \'id \');
286
+
287
+ // triggers the path generation for the persisted thumbnails
288
+ $this->dispatcher->dispatch(new UpdateThumbnailPathEvent($ids));
289
+
290
+ return $records; ' );
291
+ }
235
292
}
0 commit comments