-
Notifications
You must be signed in to change notification settings - Fork 9
fix(media): restore Media state on unserialize from Doctrine cache #88
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,13 +3,22 @@ | |||||
| namespace JoliCode\MediaBundle\Model; | ||||||
|
|
||||||
| use JoliCode\MediaBundle\Binary\Binary; | ||||||
| use JoliCode\MediaBundle\Exception\MediaNotFoundException; | ||||||
| use JoliCode\MediaBundle\Library\Library; | ||||||
| use JoliCode\MediaBundle\Resolver\Resolver; | ||||||
| use JoliCode\MediaBundle\Storage\OriginalStorage; | ||||||
| use JoliCode\MediaBundle\Variation\Variation; | ||||||
| use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||||||
|
|
||||||
| class Media implements StorableInterface | ||||||
| { | ||||||
| /** | ||||||
| * @var callable(): Resolver|null | ||||||
| */ | ||||||
| public static $resolverInitializer; | ||||||
|
|
||||||
| private static Resolver $resolver; | ||||||
|
|
||||||
| /** | ||||||
| * @var array<string, MediaVariation> | ||||||
| */ | ||||||
|
|
@@ -26,7 +35,35 @@ public function __construct( | |||||
|
|
||||||
| public function __serialize(): array | ||||||
| { | ||||||
| return [$this->path, $this->storage->getLibrary()->getName()]; | ||||||
| return [ | ||||||
| 'path' => $this->path, | ||||||
| 'library' => $this->storage->getLibrary()->getName(), | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that'd be cool to serialize the storage to be able to deserialize the media without the resolver.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hum the serialize method on the storage seems not ok... |
||||||
| ]; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * @param array<int|string, mixed> $data | ||||||
| */ | ||||||
| public function __unserialize(array $data): void | ||||||
| { | ||||||
| $path = $data['path'] ?? $data[0] ?? null; | ||||||
| $libraryName = $data['library'] ?? $data[1] ?? null; | ||||||
|
|
||||||
| if (!\is_string($path) || !\is_string($libraryName)) { | ||||||
| throw new \UnexpectedValueException('Invalid serialized media payload.'); | ||||||
| } | ||||||
|
|
||||||
| try { | ||||||
| $resolvedMedia = $this->getResolver()->resolveMedia($path, $libraryName); | ||||||
| } catch (MediaNotFoundException) { | ||||||
| $resolvedMedia = $this->getResolver()->createUnresolvedMedia($path, $libraryName); | ||||||
| } | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xavierlacot Any opinion about this resolver embedded in the DTO? 🤔 I'm not sure we need to force those resolution when unserializing. Having the resolver attached to the DTO feels weird.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When we store something with a state, there is no reason to retrieve the same object from cache with a different state. It should be the same state, the same object. Introducing a resolver here leads to unexpected output from the cache. For example, when you get your doctrine entity from the cache, there is no refresh with the original entity from the table, you get what you stored.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could serialize the storage into the if (!isset($data['storage']) {
throw Invalid...
}
$this->path = $data['path'];
$this->storage = $data['storage']; |
||||||
|
|
||||||
| $this->path = $resolvedMedia->path; | ||||||
| $this->storage = $resolvedMedia->storage; | ||||||
| $this->binary = null; | ||||||
| $this->stored = null; | ||||||
| $this->variations = []; | ||||||
| } | ||||||
|
|
||||||
| public function addVariation(MediaVariation $variation): void | ||||||
|
|
@@ -214,4 +251,17 @@ public function store(?Binary $binary = null): void | |||||
| $this->storage->createMediaFromBinary($this->path, $this->binary); | ||||||
| $this->stored = true; | ||||||
| } | ||||||
|
|
||||||
| private function getResolver(): Resolver | ||||||
| { | ||||||
| if (!isset(self::$resolver)) { | ||||||
| if (!isset(self::$resolverInitializer)) { | ||||||
| throw new \LogicException('Resolver Initializer is not set.'); | ||||||
| } | ||||||
|
|
||||||
| self::$resolver = (self::$resolverInitializer)(); | ||||||
| } | ||||||
|
|
||||||
| return self::$resolver; | ||||||
| } | ||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.