Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added doctrine orm batcher to implement less memory intensive providers #98

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 28 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,35 @@ extraction to a separate bundle.
## Installation

1. Run `composer require stefandoorn/sitemap-plugin`.
2. Add to `app/AppKernel.php`:

```
new SitemapPlugin\SitemapPlugin(),
```

3. Add to `app/config/config.yml`:

```
- { resource: "@SitemapPlugin/Resources/config/config.yml" }
```

4. Add to `app/config/routing.yml`:

```
sylius_sitemap:
resource: "@SitemapPlugin/Resources/config/routing.yml"
```
2. Then, enable the plugin by adding it to the list of registered plugins/bundles in the `config/bundles.php` file of your project:

```php
<?php

return [
// ...

Setono\DoctrineORMBatcherBundle\SetonoDoctrineORMBatcherBundle::class => ['all' => true],
SitemapPlugin\SitemapPlugin::class => ['all' => true],

// ...
];
```

3. Add to `config/packages/stefandoorn_sylius_sitemap.yaml`:

```
imports:
- { resource: "@SitemapPlugin/Resources/config/config.yaml" }
```

4. Add to `config/routes.yaml`:

```
stefandoorn_sylius_sitemap:
resource: "@SitemapPlugin/Resources/config/routing.yml"
```

## Usage

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"license": "MIT",
"require": {
"php": "^7.1",
"setono/doctrine-orm-batcher-bundle": "^0.2.0",
Copy link
Owner

Choose a reason for hiding this comment

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

When do you expect to have a stable release? I'm not a fan of adding 'non-stable' dependencies.

"sylius/sylius": "^1.0"
},
"require-dev": {
Expand Down
48 changes: 20 additions & 28 deletions spec/Provider/ProductUrlProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace spec\SitemapPlugin\Provider;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\QueryBuilder;
use PhpSpec\ObjectBehavior;
use Setono\DoctrineORMBatcher\Batch\CollectionBatchInterface;
use Setono\DoctrineORMBatcher\Batcher\Collection\CollectionBatcherInterface;
use Setono\DoctrineORMBatcher\Factory\BatcherFactoryInterface;
use SitemapPlugin\Factory\AlternativeUrlFactoryInterface;
use SitemapPlugin\Factory\UrlFactoryInterface;
use SitemapPlugin\Generator\ProductImagesToSitemapImagesCollectionGeneratorInterface;
Expand All @@ -34,9 +35,10 @@ function let(
UrlFactoryInterface $urlFactory,
AlternativeUrlFactoryInterface $alternativeUrlFactory,
LocaleContextInterface $localeContext,
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator,
BatcherFactoryInterface $batcherFactory
): void {
$this->beConstructedWith($repository, $router, $urlFactory, $alternativeUrlFactory, $localeContext, $productToImageSitemapArrayGenerator);
$this->beConstructedWith($repository, $router, $urlFactory, $alternativeUrlFactory, $localeContext, $productToImageSitemapArrayGenerator, $batcherFactory);
}

function it_is_initializable(): void
Expand All @@ -56,18 +58,18 @@ function it_generates_urls_for_the_unique_channel_locale(
AlternativeUrlFactoryInterface $alternativeUrlFactory,
LocaleContextInterface $localeContext,
LocaleInterface $locale,
Collection $products,
\Iterator $iterator,
ProductInterface $product,
ProductImageInterface $productImage,
ProductTranslation $productEnUSTranslation,
ProductTranslation $productNlNLTranslation,
UrlInterface $url,
AlternativeUrlInterface $alternativeUrl,
QueryBuilder $queryBuilder,
AbstractQuery $query,
ChannelInterface $channel,
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator,
BatcherFactoryInterface $batcherFactory,
CollectionBatcherInterface $batcher,
CollectionBatchInterface $collectionBatch
): void {
$now = new \DateTime();

Expand All @@ -86,15 +88,10 @@ function it_generates_urls_for_the_unique_channel_locale(
$queryBuilder->andWhere('o.enabled = :enabled')->willReturn($queryBuilder);
$queryBuilder->setParameter('channel', $channel)->willReturn($queryBuilder);
$queryBuilder->setParameter('enabled', true)->willReturn($queryBuilder);
$queryBuilder->getQuery()->willReturn($query);
$query->getResult()->willReturn($products);

$products->getIterator()->willReturn($iterator);
$iterator->valid()->willReturn(true, false);
$iterator->next()->shouldBeCalled();
$iterator->rewind()->shouldBeCalled();

$iterator->current()->willReturn($product);
$batcherFactory->createObjectCollectionBatcher($queryBuilder)->willReturn($batcher);
$batcher->getBatches()->willReturn([$collectionBatch]);
$collectionBatch->getCollection()->willReturn([$product]);

$productImage->getPath()->willReturn(null);

Expand Down Expand Up @@ -146,18 +143,18 @@ function it_generates_urls_for_all_channel_locales(
LocaleContextInterface $localeContext,
LocaleInterface $enUSLocale,
LocaleInterface $nlNLLocale,
Collection $products,
\Iterator $iterator,
ProductInterface $product,
ProductImageInterface $productImage,
ProductTranslation $productEnUSTranslation,
ProductTranslation $productNlNLTranslation,
UrlInterface $url,
AlternativeUrlInterface $alternativeUrl,
QueryBuilder $queryBuilder,
AbstractQuery $query,
ChannelInterface $channel,
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator,
BatcherFactoryInterface $batcherFactory,
CollectionBatcherInterface $batcher,
CollectionBatchInterface $collectionBatch
): void {
$now = new \DateTime();

Expand All @@ -178,15 +175,10 @@ function it_generates_urls_for_all_channel_locales(
$queryBuilder->andWhere('o.enabled = :enabled')->willReturn($queryBuilder);
$queryBuilder->setParameter('channel', $channel)->willReturn($queryBuilder);
$queryBuilder->setParameter('enabled', true)->willReturn($queryBuilder);
$queryBuilder->getQuery()->willReturn($query);
$query->getResult()->willReturn($products);

$products->getIterator()->willReturn($iterator);
$iterator->valid()->willReturn(true, false);
$iterator->next()->shouldBeCalled();
$iterator->rewind()->shouldBeCalled();

$iterator->current()->willReturn($product);
$batcherFactory->createObjectCollectionBatcher($queryBuilder)->willReturn($batcher);
$batcher->getBatches()->willReturn([$collectionBatch]);
$collectionBatch->getCollection()->willReturn([$product]);

$productImage->getPath()->willReturn(null);

Expand Down
28 changes: 22 additions & 6 deletions src/Provider/ProductUrlProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace SitemapPlugin\Provider;

use Doctrine\Common\Collections\Collection;
use Safe\Exceptions\StringsException;
use Setono\DoctrineORMBatcher\Batch\CollectionBatchInterface;
use Setono\DoctrineORMBatcher\Factory\BatcherFactoryInterface;
use SitemapPlugin\Factory\AlternativeUrlFactoryInterface;
use SitemapPlugin\Factory\UrlFactoryInterface;
use SitemapPlugin\Generator\ProductImagesToSitemapImagesCollectionGeneratorInterface;
Expand Down Expand Up @@ -49,20 +52,25 @@ final class ProductUrlProvider implements UrlProviderInterface
/** @var ProductImagesToSitemapImagesCollectionGeneratorInterface */
private $productToImageSitemapArrayGenerator;

/** @var BatcherFactoryInterface */
private $batcherFactory;

public function __construct(
ProductRepositoryInterface $productRepository,
RouterInterface $router,
UrlFactoryInterface $urlFactory,
AlternativeUrlFactoryInterface $urlAlternativeFactory,
LocaleContextInterface $localeContext,
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator
ProductImagesToSitemapImagesCollectionGeneratorInterface $productToImageSitemapArrayGenerator,
BatcherFactoryInterface $batcherFactory
) {
$this->productRepository = $productRepository;
$this->router = $router;
$this->urlFactory = $urlFactory;
$this->urlAlternativeFactory = $urlAlternativeFactory;
$this->localeContext = $localeContext;
$this->productToImageSitemapArrayGenerator = $productToImageSitemapArrayGenerator;
$this->batcherFactory = $batcherFactory;
}

public function getName(): string
Expand All @@ -71,7 +79,7 @@ public function getName(): string
}

/**
* {@inheritdoc}
* @throws StringsException
*/
public function generate(ChannelInterface $channel): iterable
{
Expand Down Expand Up @@ -102,19 +110,27 @@ private function localeInLocaleCodes(TranslationInterface $translation): bool
}

/**
* @return array|Collection|ProductInterface[]
* @throws StringsException
*
* @return iterable<ProductInterface>
*/
private function getProducts(): iterable
{
return $this->productRepository->createQueryBuilder('o')
$qb = $this->productRepository->createQueryBuilder('o')
->addSelect('translation')
->innerJoin('o.translations', 'translation')
->andWhere(':channel MEMBER OF o.channels')
->andWhere('o.enabled = :enabled')
->setParameter('channel', $this->channel)
->setParameter('enabled', true)
->getQuery()
->getResult();
;

$batcher = $this->batcherFactory->createObjectCollectionBatcher($qb);

/** @var CollectionBatchInterface $batch */
foreach ($batcher->getBatches() as $batch) {
yield from $batch->getCollection();
}
}

private function getLocaleCodes(): array
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services/providers/products.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<argument type="service" id="sylius.sitemap_url_alternative_factory" />
<argument type="service" id="sylius.context.locale" />
<argument type="service" id="sylius.sitemap.generator.product_images_to_sitemap_images_collection" />
<argument type="service" id="setono_doctrine_orm_batcher.factory.batcher" />
<tag name="sylius.sitemap_provider" />
</service>
</services>
Expand Down
1 change: 1 addition & 0 deletions tests/Application/config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true, 'test_cached' => true],
Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle::class => ['dev' => true, 'test' => true, 'test_cached' => true],
Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle::class => ['dev' => true, 'test' => true, 'test_cached' => true],
Setono\DoctrineORMBatcherBundle\SetonoDoctrineORMBatcherBundle::class => ['all' => true],
];