Last active
December 22, 2016 19:54
-
-
Save simshaun/e22d8977e509233c20c28d269fb0b714 to your computer and use it in GitHub Desktop.
Handling files with Alice/HautelookAliceBundle
This file contains 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
DataFixtures | |
ORM | |
theme.yml | |
Resources | |
Theme | |
1.jpg | |
2.jpg |
This file contains 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
AppBundle\Entity\Theme: | |
theme_1: | |
background: '1.jpg' |
This file contains 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 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