11 use Symfony\Component\Console\Output\OutputInterface;
43 private $resourceConnection;
48 private $imagesGeneratorFactory;
63 private $attributeRepository;
68 private $dbConnection;
73 private $expressionFactory;
78 private $batchInsertFactory;
83 private $metadataPool;
88 private $attributeCodesCache = [];
93 private $imagesInsertBatchSize = 1000;
98 private $productsSelectBatchSize = 1000;
103 private $productsCountCache;
108 private $tableCache = [];
124 \
Magento\Setup\Fixtures\ImagesGenerator\ImagesGeneratorFactory $imagesGeneratorFactory,
128 \
Magento\Framework\DB\Sql\ColumnValueExpressionFactory $expressionFactory,
129 \
Magento\Setup\Model\BatchInsertFactory $batchInsertFactory,
134 $this->imagesGeneratorFactory = $imagesGeneratorFactory;
139 $this->expressionFactory = $expressionFactory;
140 $this->batchInsertFactory = $batchInsertFactory;
141 $this->metadataPool = $metadataPool;
150 if (!$this->checkIfImagesExists()) {
151 $this->createImageEntities();
152 $this->assignImagesToProducts();
161 return 'Generating images';
170 'product-images' =>
'Product Images' 180 $config = $this->fixtureModel->getValue(
'product-images', []);
185 if (!isset(
$config[
'images-count'])) {
187 __(
"The amount of images to generate wasn't specified. Enter the amount and try again.")
191 if (!isset(
$config[
'images-per-product'])) {
193 __(
"The amount of images per product wasn't specified. Enter the amount and try again.")
199 '<info> |- Product images: %s, %s per product</info>',
211 private function checkIfImagesExists()
213 return $this->getImagesCount() > 0;
221 private function createImageEntities()
224 $batchInsert = $this->batchInsertFactory->create([
225 'insertIntoTable' => $this->getTable(
'catalog_product_entity_media_gallery'),
226 'batchSize' => $this->imagesInsertBatchSize
229 foreach ($this->generateImageFilesGenerator() as $imageName) {
230 $batchInsert->insert([
231 'attribute_id' => $this->getAttributeId(
'media_gallery'),
232 'value' => $imageName,
236 $batchInsert->flush();
246 private function generateImageFilesGenerator()
249 $imagesGenerator = $this->imagesGeneratorFactory->create();
251 $productImagesDirectoryPath =
$mediaDirectory->getRelativePath($this->mediaConfig->getBaseMediaPath());
253 for (
$i = 1;
$i <= $this->getImagesToGenerate();
$i++) {
254 $imageName = md5(
$i) .
'.jpg';
255 $imageFullName = DIRECTORY_SEPARATOR . substr($imageName, 0, 1)
256 . DIRECTORY_SEPARATOR . substr($imageName, 1, 1)
257 . DIRECTORY_SEPARATOR . $imageName;
259 $imagePath = $imagesGenerator->generate([
260 'image-width' => 300,
261 'image-height' => 300,
262 'image-name' => $imageName
267 $productImagesDirectoryPath . $imageFullName
270 yield $imageFullName;
280 private function assignImagesToProducts()
283 $batchInsertCatalogProductEntityVarchar = $this->batchInsertFactory->create([
284 'insertIntoTable' => $this->getTable(
'catalog_product_entity_varchar'),
285 'batchSize' => $this->imagesInsertBatchSize
289 $batchInsertCatalogProductEntityMediaGalleryValue = $this->batchInsertFactory->create([
290 'insertIntoTable' => $this->getTable(
'catalog_product_entity_media_gallery_value'),
291 'batchSize' => $this->imagesInsertBatchSize
295 $batchInsertCatalogProductEntityMediaGalleryValueToEntity = $this->batchInsertFactory->create([
296 'insertIntoTable' => $this->getTable(
'catalog_product_entity_media_gallery_value_to_entity'),
297 'batchSize' => $this->imagesInsertBatchSize
300 $imageGenerator = $this->getImagesGenerator();
302 foreach ($this->getProductGenerator() as $productEntity) {
303 for ($imageNum = 1; $imageNum <= $this->getImagesPerProduct(); $imageNum++) {
304 $image = $imageGenerator->current();
305 $imageGenerator->next();
307 if ($imageNum === 1) {
308 $attributes = [
'image',
'small_image',
'thumbnail',
'swatch_image'];
310 $batchInsertCatalogProductEntityVarchar->insert([
311 $this->getProductLinkField() => $productEntity[$this->getProductLinkField()],
312 'attribute_id' => $this->getAttributeId(
$attr),
313 'value' =>
$image[
'value'],
319 $batchInsertCatalogProductEntityMediaGalleryValueToEntity->insert([
320 'value_id' =>
$image[
'value_id'],
321 $this->getProductLinkField() => $productEntity[$this->getProductLinkField()]
324 $batchInsertCatalogProductEntityMediaGalleryValue->insert([
325 'value_id' =>
$image[
'value_id'],
327 $this->getProductLinkField() => $productEntity[$this->getProductLinkField()],
328 'position' =>
$image[
'value_id'],
334 $batchInsertCatalogProductEntityVarchar->flush();
335 $batchInsertCatalogProductEntityMediaGalleryValue->flush();
336 $batchInsertCatalogProductEntityMediaGalleryValueToEntity->flush();
345 private function getProductGenerator()
349 $products = $this->getProducts($this->productsSelectBatchSize, $offset);
350 $offset += $this->productsSelectBatchSize;
356 $products = $this->getProducts($this->productsSelectBatchSize, $offset);
357 $offset += $this->productsSelectBatchSize;
374 private function getProducts($limit, $offset)
376 $select = $this->getDbConnection()
378 ->from([
'product_entity' => $this->getTable(
'catalog_product_entity')], [])
379 ->columns([$this->getProductLinkField()])
380 ->limit($limit, $offset);
382 return $this->getDbConnection()->fetchAssoc(
$select);
390 private function getImagesGenerator()
392 $select = $this->getDbConnection()
395 $this->getTable(
'catalog_product_entity_media_gallery'),
396 [
'value_id',
'value']
397 )->order(
'value_id desc')
398 ->limit($this->getProductsCount() * $this->getImagesPerProduct());
400 $images = $this->getDbConnection()->fetchAssoc(
$select);
403 yield current($images);
405 if (next($images) ===
false) {
416 private function getImagesToGenerate()
418 $config = $this->fixtureModel->getValue(
'product-images', []);
420 return $config[
'images-count'] ??
null;
428 private function getImagesPerProduct()
430 $config = $this->fixtureModel->getValue(
'product-images', []);
432 return $config[
'images-per-product'] ??
null;
440 private function getProductsCount()
442 if ($this->productsCountCache ===
null) {
445 ->from([
'product_entity' => $this->getTable(
'catalog_product_entity')], [])
447 'count' => $this->expressionFactory->create([
448 'expression' =>
'COUNT(*)' 452 $this->productsCountCache = (int) $this->getDbConnection()->fetchOne(
$select);
455 return $this->productsCountCache;
463 private function getImagesCount()
467 ->from([
'product_entity' => $this->getTable(
'catalog_product_entity_media_gallery')], [])
469 'count' => $this->expressionFactory->create([
470 'expression' =>
'COUNT(*)' 472 ])->where(
'media_type="image"');
474 return (
int) $this->getDbConnection()->fetchOne(
$select);
505 private function getDbConnection()
507 if ($this->dbConnection ===
null) {
508 $this->dbConnection = $this->resourceConnection->getConnection();
511 return $this->dbConnection;
538 private function getProductLinkField()
540 return $this->metadataPool
541 ->getMetadata(\
Magento\Catalog\Api\Data\ProductInterface::class)
__construct(FixtureModel $fixtureModel, \Magento\Framework\App\ResourceConnection $resourceConnection, \Magento\Setup\Fixtures\ImagesGenerator\ImagesGeneratorFactory $imagesGeneratorFactory, \Magento\Framework\Filesystem $filesystem, \Magento\Catalog\Model\Product\Media\Config $mediaConfig, \Magento\Eav\Model\AttributeRepository $attributeRepository, \Magento\Framework\DB\Sql\ColumnValueExpressionFactory $expressionFactory, \Magento\Setup\Model\BatchInsertFactory $batchInsertFactory, \Magento\Framework\EntityManager\MetadataPool $metadataPool)
printInfo(OutputInterface $output)