Skip to content

Instantly share code, notes, and snippets.

@simshaun
Last active December 22, 2016 19:54
Show Gist options
  • Save simshaun/e22d8977e509233c20c28d269fb0b714 to your computer and use it in GitHub Desktop.
Save simshaun/e22d8977e509233c20c28d269fb0b714 to your computer and use it in GitHub Desktop.
Handling files with Alice/HautelookAliceBundle
DataFixtures
ORM
theme.yml
Resources
Theme
1.jpg
2.jpg
AppBundle\Entity\Theme:
theme_1:
background: '1.jpg'
<?php
namespace AppBundle\DataFixtures\Processor;
use AppBundle\Entity\Theme;
use AppBundle\Entity\ThemeRepository;
use AppBundle\File\Theme\BackgroundFile;
use Nelmio\Alice\ProcessorInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class ThemeProcessor implements ProcessorInterface
{
private $bgFileHandler;
private $themeRepo;
public function __construct(BackgroundFile $bgFileHandler, ThemeRepository $themeRepository)
{
$this->bgFileHandler = $bgFileHandler;
$this->themeRepo = $themeRepository;
}
public function preProcess($object)
{
}
public function postProcess($object)
{
if (!$object instanceof Theme) {
return;
}
if ($object->getBackground()) {
$filePath = __DIR__.'/../Resources/Theme/'.$object->getBackground();
$object->setBackgroundFile(new UploadedFile(
$filePath,
$object->getBackground(),
mime_content_type($filePath),
filesize($filePath),
null,
true
));
$this->bgFileHandler->upload($object);
$this->themeRepo->save($object);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment