Last active
January 9, 2023 09:14
-
-
Save kunicmarko20/5be4b0ed2a63f5179b9df1e8b5c94412 to your computer and use it in GitHub Desktop.
Transfer old images into Sonata Media Bundle. This command is used for adding sonata media gallery to new structure from old database.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace YourBundle\Command; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Application\Sonata\MediaBundle\Entity\Media; | |
use Application\Sonata\MediaBundle\Entity\Gallery; | |
use Application\Sonata\MediaBundle\Entity\GalleryHasMedia; | |
use Sonata\MediaBundle\Entity\MediaManager; | |
use Sonata\MediaBundle\Entity\GalleryManager; | |
use Doctrine\ORM\EntityManager; | |
class MigrateImagesCommand extends ContainerAwareCommand | |
{ | |
private $output; | |
private $em; | |
/* | |
* Sonata Media Manager | |
*/ | |
private $mediaManager; | |
/* | |
* Sonata Gallery Manager | |
*/ | |
private $galleryManager; | |
/* | |
* Directory of old images | |
*/ | |
private $uploadDirectory; | |
public function __construct(EntityManager $em, MediaManager $mediaManager, GalleryManager $galleryManager, $uploadDirectory, $name = null) { | |
$this->em = $em; | |
$this->mediaManager = $mediaManager; | |
$this->galleryManager = $galleryManager; | |
$this->uploadDirectory = $uploadDirectory; | |
parent::__construct($name); | |
} | |
protected function configure() | |
{ | |
$this | |
->setName('migrate:images') | |
->setDescription('Migrates old images to sonata media bundle.') | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$output->writeln('<comment>Running Image Migration...</comment>'); | |
$this->output = $output; | |
/* | |
* Decreases memory use when dealing with a lot of data | |
*/ | |
$this->em->getConnection()->getConfiguration()->setSQLLogger(null); | |
$this->migrateImages(); | |
$output->writeln('<comment>Done!</comment>'); | |
} | |
private function migrateImages(){ | |
$this->output->writeln('<comment>Starting</comment>'); | |
$items = $this->em->getRepository('YourBundle:Items')->findAll(); | |
$count = count($items); | |
$iteration = 0; | |
gc_enable(); | |
try{ | |
foreach($items as $item){ | |
$gallery = $this->createGallery($item); | |
if(!$gallery) | |
continue; | |
$item->setGallery($gallery); | |
$this->em->persist($item); | |
$tempObjects[] = $item; | |
if (++$iteration % 10 == 0) { | |
$this->em->flush(); | |
$count -=10; | |
$this->output->writeln("<comment>Flushed, $count items left</comment>"); | |
// IMPORTANT - clean entities | |
foreach($tempObjects as $tempObject) { | |
$this->em->detach($tempObject); | |
} | |
$tempObjects = null; | |
gc_collect_cycles(); | |
} | |
} | |
$this->em->flush(); | |
$this->output->writeln('<info>SUCCESS</info>'); | |
} catch (\Exception $e) { | |
$this->output->writeln('<error>'.$e->getMessage().'</error>'); | |
} | |
} | |
private function createGallery($item){ | |
/* | |
* This is array of images, write code for grabing images depending on your needs | |
* Check out multiple db connections for symfony and use that logic | |
*/ | |
$images; | |
if(count($images) === 0) | |
return false; | |
$gallery = $this->createSonataGallery($item->getName()); | |
foreach($images as $image){ | |
$media = $this->createSonataMedia($image); | |
$galleryHasMedia = $this->createSonataGalleryHasMedia($media, $image->getPosition()); | |
$gallery->addGalleryHasMedias($galleryHasMedia); | |
} | |
$this->galleryManager->save($gallery); | |
return $gallery; | |
} | |
private function createSonataGallery($name){ | |
$gallery = new Gallery(); | |
$gallery->setName($name); | |
$gallery->setContext('default'); //your context | |
$gallery->setDefaultFormat('default_small'); //your format | |
$gallery->setEnabled(0); //required | |
return $gallery; | |
} | |
private function createSonataMedia($image){ | |
$media = new Media(); | |
$media->setBinaryContent($this->uploadDirectory.$image->getName()); | |
$media->setContext('default'); //your context | |
$media->setProviderName('sonata.media.provider.image');//sonata image provider | |
$media->setName($image->getName()); | |
$this->mediaManager->save($media); | |
return $media; | |
} | |
private function createSonataGalleryHasMedia($media,$position){ | |
$galleryHasMedia = new GalleryHasMedia(); | |
$galleryHasMedia->setMedia($media); | |
$galleryHasMedia->setPosition($position); | |
return $galleryHasMedia; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parameters: | |
upload_directory: '%kernel.root_dir%/../web/uploads/' | |
services: | |
app.migrate.images.command: | |
class: YourBundle\Command\MigrateImagesCommand | |
arguments: ["@doctrine.orm.entity_manager","@sonata.media.manager.media","@sonata.media.manager.gallery","%upload_directory%"] | |
tags: | |
- { name: console.command } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment