-
Notifications
You must be signed in to change notification settings - Fork 155
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
Add ResourceToIdentifierCacheableTransformer form data transformer #771
base: 1.9
Are you sure you want to change the base?
Add ResourceToIdentifierCacheableTransformer form data transformer #771
Conversation
/** | ||
* @psalm-suppress MissingParamType | ||
* | ||
* @param mixed $value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @param mixed $value | |
* @param object|null $value |
return PropertyAccess::createPropertyAccessor()->getValue($value, $this->identifier); | ||
} | ||
|
||
/** @param mixed $value */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** @param mixed $value */ | |
/** @param int|string|null $value */ |
RepositoryInterface $repository, | ||
ResourceInterface $resource, | ||
): void { | ||
$this->clear(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** @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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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! 🖖