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

[API]Product image handling #12847

Merged
merged 3 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions UPGRADE-API-1.11.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# UPGRADE FROM `v1.10.X` TO `v1.11.0`

1. The product images should have a proper prefix added to the path, so the images could be resolved.
This is now done and response of `Product Image` resource has been extended with parameter `media_path`.

```diff
{
"@context": "/api/v2/contexts/ProductImage",
"@id": "/api/v2/shop/product-images/123",
"@type": "ProductImage",
"id": "123",
"type": "thumbnail",
"path": "uo/product.jpg",
+ "media_path": "/media/image/uo/product.jpg"
Copy link
Member

Choose a reason for hiding this comment

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

If I see correctly, there is no more media_path, you are updating path field

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have changed it a little now :)

}
```

To change the prefix you need to set parameter in ``app/config/packages/_sylius.yaml``:

```yaml
sylius_api:
product_image_prefix: 'media/image'
```

1. `Sylius\Bundle\ApiBundle\Doctrine\Filters\ExchangeRateFilter` and `Sylius\Bundle\ApiBundle\Doctrine\Filters\TranslationOrderNameAndLocaleFilter` has been moved to `Sylius\Bundle\ApiBundle\Doctrine\Filter\ExchangeRateFilter` and `Sylius\Bundle\ApiBundle\Doctrine\Filter\TranslationOrderNameAndLocaleFilter` respectively.

1. `Sylius\Bundle\ApiBundle\View\CartShippingMethodInterface` and `Sylius\Bundle\ApiBundle\View\CartShippingMethod` have been removed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultFalse()
->end()
->end()
->children()
->variableNode('product_image_prefix')
->defaultValue('media/image')
Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with defining this value as a parameter, however we should at least use another parameter, that will be used here as well:

data_root: "%sylius_core.public_dir%/media/image"

But once we will introduce the parameter there, I'm not sure we need it. Up to you.

->end()
->end()
;

return $treeBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function load(array $configs, ContainerBuilder $container): void
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));

$container->setParameter('sylius_api.enabled', $config['enabled']);
$container->setParameter('sylius_api.product_image_prefix', $config['product_image_prefix']);

$loader->load('services.xml');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<tag name="serializer.normalizer" priority="64" />
</service>

<service id="Sylius\Bundle\ApiBundle\Serializer\ProductImageNormalizer">
<argument type="string">%sylius_api.product_image_prefix%</argument>
<tag name="serializer.normalizer" priority="64" />
</service>

<service id="Sylius\Bundle\ApiBundle\Serializer\CommandNormalizer">
<argument type="service" id="serializer.normalizer.object" />
<tag name="serializer.normalizer" priority="64" />
Expand Down
72 changes: 72 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Serializer/ProductImageNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Serializer;

use Sylius\Component\Core\Model\ProductImageInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Webmozart\Assert\Assert;

/** @experimental */
class ProductImageNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;

private const ALREADY_CALLED = 'product_image_normalizer_already_called';

/** @var string */
private $prefix;

public function __construct(string $prefix)
{
$this->prefix = $this->validatePrefix($prefix);
}

public function normalize($object, $format = null, array $context = [])
{
Assert::isInstanceOf($object, ProductImageInterface::class);
Assert::keyNotExists($context, self::ALREADY_CALLED);

$context[self::ALREADY_CALLED] = true;

$data = $this->normalizer->normalize($object, $format, $context);

$data['media_path'] = $this->prefix . $data['path'];
arti0090 marked this conversation as resolved.
Show resolved Hide resolved

return $data;
}

public function supportsNormalization($data, $format = null, $context = []): bool
{
if (isset($context[self::ALREADY_CALLED])) {
return false;
}

return $data instanceof ProductImageInterface;
}

private function validatePrefix(string $prefix): string
{
if ('/' !== substr($prefix, 0, 1)) {
arti0090 marked this conversation as resolved.
Show resolved Hide resolved
$prefix = '/'.$prefix;
}

if ('/' === substr($prefix, -1)) {
return $prefix;
}

return $prefix . '/';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Bundle\ApiBundle\Serializer;

use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\ProductImageInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class ProductImageNormalizerSpec extends ObjectBehavior
{
function let(): void
{
$this->beConstructedWith('prefix');
}
arti0090 marked this conversation as resolved.
Show resolved Hide resolved

function it_implements_context_aware_normalizer_interface(): void
{
$this->shouldImplement(ContextAwareNormalizerInterface::class);
}

function it_supports_only_product_image_interface(ProductImageInterface $productImage, OrderInterface $order): void
{
$this->supportsNormalization($productImage)->shouldReturn(true);
$this->supportsNormalization($order)->shouldReturn(false);
}

function it_serializes_product_image_with_proper_prefix(
NormalizerInterface $normalizer,
ProductImageInterface $productImage
): void {
$this->setNormalizer($normalizer);

$normalizer->normalize($productImage, null, ['product_image_normalizer_already_called' => true])->willReturn(['path' => 'some_path']);

$this->normalize($productImage, null, [])->shouldReturn(['path' => 'some_path', 'media_path' => '/prefix/some_path']);
}
}
2 changes: 1 addition & 1 deletion tests/Api/DataFixtures/ORM/product_image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Sylius\Component\Core\Model\Product:
Sylius\Component\Core\Model\ProductImage:
product_thumbnail:
type: "thumbnail"
path: "/uo/product.jpg"
path: "uo/product.jpg"
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"@type": "ProductImage",
"id": "@integer@",
"type": "thumbnail",
"path": "\/uo\/product.jpg"
"path": "uo\/product.jpg",
"media_path": "\/media\/image\/uo\/product.jpg"
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"@type": "ProductImage",
"id": "@integer@",
"type": "thumbnail",
"path": "\/uo\/product.jpg"
"path": "uo\/product.jpg",
"media_path": "\/media\/image\/uo\/product.jpg"
}
],
"hydra:totalItems": 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"@type": "ProductImage",
"id": "@integer@",
"type": "thumbnail",
"path": "\/uo\/product.jpg"
"path": "uo\/product.jpg",
"media_path": "\/media\/image\/uo\/product.jpg"
}