Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Improve fixtures 3 #636

Merged
merged 1 commit into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 46 additions & 61 deletions src/Sonata/Bundle/DemoBundle/DataFixtures/ORM/LoadMediaData.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use Faker\Generator;
use Sonata\ClassificationBundle\Model\Category;
use Sonata\MediaBundle\Model\GalleryInterface;
use Sonata\MediaBundle\Model\GalleryManagerInterface;
use Sonata\MediaBundle\Model\Media;
use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Model\MediaManagerInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\File\File;

class LoadMediaData extends AbstractFixture implements OrderedFixtureInterface
{
Expand Down Expand Up @@ -55,81 +57,64 @@ public function getOrder(): int

public function load(ObjectManager $manager): void
{
$gallery = $this->galleryManager->create();
$homepageGallery = $this->galleryManager->create();

$canada = Finder::create()->name('IMG_3587*.jpg')->in(__DIR__.'/../data/files/gilles-canada');
$paris = Finder::create()->name('IMG_3008*.jpg')->in(__DIR__.'/../data/files/hugo-paris');
$switzerland = Finder::create()->name('switzerland_2012-05-19_006.jpg')->in(__DIR__.'/../data/files/sylvain-switzerland');
$media = $this->createMedia('gilles-canada/IMG_3587.jpg', 'Canada', 'Gilles Rosenbaum', $this->getReference('travels_quebec_category'));
$this->addMediaToGallery($media, $homepageGallery);
$this->setReference('sonata-media-0', $media);

$i = 0;
foreach ($canada as $file) {
wbloszyk marked this conversation as resolved.
Show resolved Hide resolved
$media = $this->mediaManager->create();
$media->setBinaryContent(__DIR__.'/../data/files/gilles-canada/'.$file->getRelativePathname());
$media->setEnabled(true);
$media->setName('Canada');
$media->setDescription('Canada');
$media->setAuthorName('Gilles Rosenbaum');
$media->setCopyright('CC BY-NC-SA 4.0');
$media->setCategory($this->getReference('travels_quebec_category'));
$media = $this->createMedia('hugo-paris/IMG_3008.jpg', 'Paris', 'Hugo Briand', $this->getReference('travels_paris_category'));
$this->addMediaToGallery($media, $homepageGallery);

$this->addReference('sonata-media-'.($i++), $media);
$media = $this->createMedia('sylvain-switzerland/switzerland_2012-05-19_006.jpg', 'Switzerland', 'Sylvain Deloux', $this->getReference('travels_switzerland_category'));
$this->addMediaToGallery($media, $homepageGallery);

$this->mediaManager->save($media, 'default', 'sonata.media.provider.image');
// disabled media
$media = $this->createMedia('hugo-paris/IMG_2571.jpg', 'Paris 2', 'Hugo Briand', $this->getReference('travels_paris_category'), false);
$this->addMediaToGallery($media, $homepageGallery);

$this->addMediaToGallery($media, $gallery);
}
// disabled GalleryHasMedia
$media = $this->createMedia('hugo-paris/IMG_2577.jpg', 'Paris 3', 'Hugo Briand', $this->getReference('travels_paris_category'));
$this->addMediaToGallery($media, $homepageGallery, false);

foreach ($paris as $file) {
$media = $this->mediaManager->create();
$media->setBinaryContent(__DIR__.'/../data/files/hugo-paris/'.$file->getRelativePathname());
$media->setEnabled(true);
$media->setName('Paris');
$media->setDescription('Paris');
$media->setAuthorName('Hugo Briand');
$media->setCopyright('CC BY-NC-SA 4.0');
$media->setCategory($this->getReference('travels_paris_category'));
$homepageGallery->setEnabled(true);
$homepageGallery->setName('Homepage gallery');
$homepageGallery->setDefaultFormat('small');
$homepageGallery->setContext('default');

$this->addReference('sonata-media-'.($i++), $media);
$this->galleryManager->update($homepageGallery);

$this->mediaManager->save($media, 'default', 'sonata.media.provider.image');

$this->addMediaToGallery($media, $gallery);
}

foreach ($switzerland as $file) {
$media = $this->mediaManager->create();
$media->setBinaryContent(__DIR__.'/../data/files/sylvain-switzerland/'.$file->getRelativePathname());
$media->setEnabled(true);
$media->setName('Switzerland');
$media->setDescription('Switzerland');
$media->setAuthorName('Sylvain Deloux');
$media->setCopyright('CC BY-NC-SA 4.0');
$media->setCategory($this->getReference('travels_switzerland_category'));

$this->addReference('sonata-media-'.($i++), $media);

$this->mediaManager->save($media, 'default', 'sonata.media.provider.image');

$this->addMediaToGallery($media, $gallery);
}

$gallery->setEnabled(true);
$gallery->setName($this->faker->sentence(4));
$gallery->setDefaultFormat('small');
$gallery->setContext('default');

$this->galleryManager->update($gallery);

$this->addReference('media-gallery', $gallery);
$this->addReference('media-homepage-gallery', $homepageGallery);
}

public function addMediaToGallery(MediaInterface $media, GalleryInterface $gallery): void
public function addMediaToGallery(MediaInterface $media, GalleryInterface $gallery, bool $enabled = true): void
{
$galleryHasMedia = new GalleryHasMedia();
$galleryHasMedia->setMedia($media);
$galleryHasMedia->setPosition(\count($gallery->getGalleryHasMedias()) + 1);
$galleryHasMedia->setEnabled(true);
$galleryHasMedia->setEnabled($enabled);

$gallery->addGalleryHasMedias($galleryHasMedia);
}

/**
* @param string|File $file File pathname or `Symfony\Component\HttpFoundation\File` object
*
* @return Media
*/
protected function createMedia($file, string $name, string $autor, Category $category, bool $enabled = true, string $copyright = 'CC BY-NC-SA 4.0')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this method be private?

Suggested change
protected function createMedia($file, string $name, string $autor, Category $category, bool $enabled = true, string $copyright = 'CC BY-NC-SA 4.0')
protected function createMedia(string $file, string $name, string $autor, Category $category, bool $enabled = true, string $copyright = 'CC BY-NC-SA 4.0')

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$file can be string or File like setBinaryContext.

IMHO we should keep Api open as much as it is possible in Demo. This will allow user to people to test it and understand how sonata work.

Copy link
Member

@phansys phansys Jul 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ok then. Could you please add a docblock for this argument?

{
$media = $this->mediaManager->create();
$media->setBinaryContent(__DIR__.'/../data/files/'.$file);
$media->setEnabled($enabled);
$media->setName($name);
$media->setDescription($name);
$media->setAuthorName($autor);
$media->setCopyright($copyright);
$media->setCategory($category);

$this->mediaManager->save($media, 'default', 'sonata.media.provider.image');

return $media;
}
}
29 changes: 10 additions & 19 deletions src/Sonata/Bundle/DemoBundle/DataFixtures/ORM/LoadNewsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Sonata\NewsBundle\Model\CommentInterface;
use Sonata\NewsBundle\Model\CommentManagerInterface;
use Sonata\NewsBundle\Model\PostManagerInterface;
use Twig\Environment;

class LoadNewsData extends AbstractFixture implements OrderedFixtureInterface
{
Expand Down Expand Up @@ -57,20 +58,27 @@ class LoadNewsData extends AbstractFixture implements OrderedFixtureInterface
*/
private $tagManager;

/**
* @var Environment
*/
private $twig;

public function __construct(
Generator $faker,
CollectionManagerInterface $collectionManager,
CommentManagerInterface $commentManager,
Pool $formatterPool,
PostManagerInterface $postManager,
TagManagerInterface $tagManager
TagManagerInterface $tagManager,
Environment $twig
) {
$this->faker = $faker;
$this->collectionManager = $collectionManager;
$this->commentManager = $commentManager;
$this->formatterPool = $formatterPool;
$this->postManager = $postManager;
$this->tagManager = $tagManager;
$this->twig = $twig;
}

public function getOrder(): int
Expand Down Expand Up @@ -113,25 +121,8 @@ public function load(ObjectManager $manager): void

$id = $this->getReference('sonata-media-0')->getId();

//TODO: fix raw
$raw = '';
/*
$raw = <<<RAW
### Gist Formatter

Now a specific gist from github

<% gist '1552362' 'gistfile1.txt' %>

### Media Formatter

Load a media from a <code>SonataMediaBundle</code> with a specific format

<% media $id, 'big' %>
$raw = $this->twig->render('@SonataDemo/fixtures/news_gist_formatter.md.twig', ['id' => $id]);

RAW
;
*/
$raw .= sprintf("### %s\n\n%s\n\n### %s\n\n%s",
$this->faker->sentence(random_int(3, 6)),
$this->faker->text(1000),
Expand Down
53 changes: 29 additions & 24 deletions src/Sonata/Bundle/DemoBundle/DataFixtures/ORM/LoadOrderData.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,7 @@ public function load(ObjectManager $manager): void
$basket->setCurrency($currency);
$basket->setProductPool($this->productPool);

$products = [
$this->getReference('php_plush_blue_goodie_product'),
$this->getReference('php_plush_green_goodie_product'),
$this->getReference('travel_japan_small_product'),
$this->getReference('travel_japan_medium_product'),
$this->getReference('travel_japan_large_product'),
$this->getReference('travel_japan_extra_large_product'),
$this->getReference('travel_quebec_small_product'),
$this->getReference('travel_quebec_medium_product'),
$this->getReference('travel_quebec_large_product'),
$this->getReference('travel_quebec_extra_large_product'),
$this->getReference('travel_london_small_product'),
$this->getReference('travel_london_medium_product'),
$this->getReference('travel_london_large_product'),
$this->getReference('travel_london_extra_large_product'),
$this->getReference('travel_paris_small_product'),
$this->getReference('travel_paris_medium_product'),
$this->getReference('travel_paris_large_product'),
$this->getReference('travel_paris_extra_large_product'),
$this->getReference('travel_switzerland_small_product'),
$this->getReference('travel_switzerland_medium_product'),
$this->getReference('travel_switzerland_large_product'),
$this->getReference('travel_switzerland_extra_large_product'),
];
$products = $this->getProductList();

$nbCustomers = $this->parameterBag->has('sonata.fixtures.customer.fake') ? (int) $this->parameterBag->get('sonata.fixtures.customer.fake') : 20;

Expand Down Expand Up @@ -395,4 +372,32 @@ protected function createTransaction(OrderInterface $order, ObjectManager $manag

return $transaction;
}

protected function getProductList()
{
return [
$this->getReference('php_plush_blue_goodie_product'),
$this->getReference('php_plush_green_goodie_product'),
$this->getReference('travel_japan_small_product'),
$this->getReference('travel_japan_medium_product'),
$this->getReference('travel_japan_large_product'),
$this->getReference('travel_japan_extra_large_product'),
$this->getReference('travel_quebec_small_product'),
$this->getReference('travel_quebec_medium_product'),
$this->getReference('travel_quebec_large_product'),
$this->getReference('travel_quebec_extra_large_product'),
$this->getReference('travel_london_small_product'),
$this->getReference('travel_london_medium_product'),
$this->getReference('travel_london_large_product'),
$this->getReference('travel_london_extra_large_product'),
$this->getReference('travel_paris_small_product'),
$this->getReference('travel_paris_medium_product'),
$this->getReference('travel_paris_large_product'),
$this->getReference('travel_paris_extra_large_product'),
$this->getReference('travel_switzerland_small_product'),
$this->getReference('travel_switzerland_medium_product'),
$this->getReference('travel_switzerland_large_product'),
$this->getReference('travel_switzerland_extra_large_product'),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public function createHomePage(SiteInterface $site): void
// Add media gallery block
$content->addChildren($gallery = $this->blockManager->create());
$gallery->setType('sonata.media.block.gallery');
$gallery->setSetting('galleryId', $this->getReference('media-gallery')->getId());
$gallery->setSetting('galleryId', $this->getReference('media-homepage-gallery')->getId());
$gallery->setSetting('context', 'default');
$gallery->setSetting('format', 'big');
$gallery->setPosition(1);
Expand Down
27 changes: 15 additions & 12 deletions src/Sonata/Bundle/DemoBundle/DataFixtures/ORM/LoadProductData.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
use Sonata\MediaBundle\Entity\MediaManager;
use Sonata\MediaBundle\Model\GalleryInterface;
use Sonata\MediaBundle\Model\GalleryManagerInterface;
use Sonata\MediaBundle\Model\Media;
use Sonata\MediaBundle\Model\MediaInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\File\File;

/**
* Product fixtures loader.
Expand Down Expand Up @@ -1114,19 +1116,20 @@ protected function addMediaToProduct(
}

/**
* @param string|null $author
* @param string|null $copyright
* @param string|File $file File pathname or `Symfony\Component\HttpFoundation\File` object
*
* @return Media
*/
protected function createMedia(
string $mediaFilename,
$file,
string $name,
string $description,
$author = null,
$copyright = null,
?string $author = null,
?string $copyright = null,
string $categoryReference = 'root_products_category'): MediaInterface
{
$media = $this->mediaManager->create();
$media->setBinaryContent($mediaFilename);
$media->setBinaryContent($file);
$media->setEnabled(true);
$media->setName($name);
$media->setDescription($description);
Expand Down Expand Up @@ -1157,7 +1160,7 @@ protected function addSwitzerlandGallery(ProductInterface $product): void
$pos = 0;
foreach ($files as $file) {
$media = $this->mediaManager->create();
$media->setBinaryContent(__DIR__.'/../data/files/sylvain-switzerland/'.$file->getRelativePathname());
$media->setBinaryContent($file->getPathname());
$media->setEnabled(true);
$media->setDescription('Switzerland');
$media->setName('Switzerland');
Expand Down Expand Up @@ -1196,7 +1199,7 @@ protected function addParisGallery(ProductInterface $product): void
$pos = 0;
foreach ($files as $file) {
$media = $this->mediaManager->create();
$media->setBinaryContent(__DIR__.'/../data/files/gilles-paris/'.$file->getRelativePathname());
$media->setBinaryContent($file->getPathname());
$media->setEnabled(true);
$media->setDescription('Paris');
$media->setName(sprintf('Paris %s', $a));
Expand All @@ -1221,7 +1224,7 @@ protected function addParisGallery(ProductInterface $product): void
$pos = 0;
foreach ($files as $file) {
$media = $this->mediaManager->create();
$media->setBinaryContent(__DIR__.'/../data/files/hugo-paris/'.$file->getRelativePathname());
$media->setBinaryContent($file->getPathname());
$media->setEnabled(true);
$media->setDescription('Paris');
$media->setName(sprintf('Paris %s', $b));
Expand Down Expand Up @@ -1260,7 +1263,7 @@ protected function addCanadaGallery(ProductInterface $product): void
$pos = 0;
foreach ($files as $file) {
$media = $this->mediaManager->create();
$media->setBinaryContent(__DIR__.'/../data/files/gilles-canada/'.$file->getRelativePathname());
$media->setBinaryContent($file->getPathname());
$media->setEnabled(true);
$media->setDescription('Canada');
$media->setName('Canada');
Expand All @@ -1281,7 +1284,7 @@ protected function addCanadaGallery(ProductInterface $product): void
$pos = 0;
foreach ($files as $file) {
$media = $this->mediaManager->create();
$media->setBinaryContent(__DIR__.'/../data/files/hugo-canada/'.$file->getRelativePathname());
$media->setBinaryContent($file->getPathname());
$media->setEnabled(true);
$media->setDescription('Canada');
$media->setName('Canada');
Expand Down Expand Up @@ -1318,7 +1321,7 @@ protected function addJapanGallery(ProductInterface $product): void
$pos = 0;
foreach ($files as $file) {
$media = $this->mediaManager->create();
$media->setBinaryContent(__DIR__.'/../data/files/maha-japan/'.$file->getRelativePathname());
$media->setBinaryContent($file->getPathname());
$media->setEnabled(true);
$media->setDescription('Japan');
$media->setName('Japan');
Expand Down
1 change: 1 addition & 0 deletions src/Sonata/Bundle/DemoBundle/Resources/config/fixtures.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<argument type="service" id="sonata.formatter.pool"/>
<argument type="service" id="sonata.news.manager.post"/>
<argument type="service" id="sonata.classification.manager.tag"/>
<argument type="service" id="twig"/>
</service>
<service id="sonata_bundle_demo.data_fixtures_orm.load_order_data" class="Sonata\Bundle\DemoBundle\DataFixtures\ORM\LoadOrderData">
<tag name="doctrine.fixture.orm"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
{#

This file is part of the Sonata package.

(c) Thomas Rabaix <[email protected]>

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

#}

<h1>Gallery List</h1>

<p>
Expand Down
Loading