Add ResourceToIdentifierCacheableTransformer form data transformer#771
Add ResourceToIdentifierCacheableTransformer form data transformer#771senghe wants to merge 6 commits intoSylius:1.9from
Conversation
| /** | ||
| * @psalm-suppress MissingParamType | ||
| * | ||
| * @param mixed $value |
There was a problem hiding this comment.
| * @param mixed $value | |
| * @param object|null $value |
| return PropertyAccess::createPropertyAccessor()->getValue($value, $this->identifier); | ||
| } | ||
|
|
||
| /** @param mixed $value */ |
There was a problem hiding this comment.
| /** @param mixed $value */ | |
| /** @param int|string|null $value */ |
| RepositoryInterface $repository, | ||
| ResourceInterface $resource, | ||
| ): void { | ||
| $this->clear(); |
There was a problem hiding this comment.
Creating a method just for the specs to work is a bad idea. Even more so since it's not in any interface. Normally for something like this, you could use ResetInterface, but in this particular case, it's useless since this is not a service living in the container and instead gets recreated all the time, it'd be confusing.
Remove the ::clear, and as for the test itself, you can use reflection to set the value of cache
|
|
||
| public function clear(): void | ||
| { | ||
| self::$cache = []; | ||
| } |
There was a problem hiding this comment.
| public function clear(): void | |
| { | |
| self::$cache = []; | |
| } |
| use Symfony\Component\PropertyAccess\PropertyAccess; | ||
| use Webmozart\Assert\Assert; | ||
|
|
||
| final class ResourceToIdentifierCacheableTransformer implements DataTransformerInterface |
There was a problem hiding this comment.
| final class ResourceToIdentifierCacheableTransformer implements DataTransformerInterface | |
| final class CachedResourceToIdentifierTransformer implements DataTransformerInterface |
Though that's just a matter of taste
There was a problem hiding this comment.
Suggested name is in line with the rest of the sylius, I vote for it as well
|
|
||
| private string $identifier; | ||
|
|
||
| /** @var array<ResourceInterface> */ |
There was a problem hiding this comment.
| /** @var array<ResourceInterface> */ | |
| /** @var array<int|string, ResourceInterface> */ |
Think it should work
| private string $identifier; | ||
|
|
||
| /** @var array<ResourceInterface> */ | ||
| private static array $cache; |
There was a problem hiding this comment.
| private static array $cache; | |
| private array $cache; |
Pretty sure making it static could potentially leak objects between different instances.
To leave it as static you'd need to prefix the key w/ a class name, make it a nested array or something like that.
There was a problem hiding this comment.
Is it possible or needed to make use of psr CacheInterface/symfony cache for this?
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Sylius\Bundle\ResourceBundle\Form\DataTransformer; |
There was a problem hiding this comment.
| namespace Sylius\Bundle\ResourceBundle\Form\DataTransformer; | |
| namespace Sylius\Resource\Symfony\Form\DataTransformer; |
So it should be placed into src/Component/src/Symfony/Form/DataTransformer directory.
|
Thx for your contribution, |
Hi all! :) I've prepared a PR with a new Form DataTransformer - it allows to cache the result to not to spam application with the same database queries. I hope you like it! 🖖