nikolaposa/version

Symfony cache: Version object cannot be cached

partikule opened this issue · 7 comments

When using this library through shivas/versioning-bundle with Symfony 4.2, this error occurs:

WARNING   [cache] Failed to save key "version" (Version\Version)
[
  "key" => "version",
  "type" => "Version\Version",
  "exception" => Symfony\Component\Cache\Exception\InvalidArgumentException {
    #message: "Cache key "version" has non-serializable Version\Version value."
    #code: 0
    #file: "/var/www/symfony/vendor/symfony/cache/Traits/PhpFilesTrait.php"
    #line: 177
    -previous: Symfony\Component\VarExporter\Exception\NotInstantiableTypeException {
      #message: "Type "Version\Version" is not instantiable."
      #code: 0
      #file: "/var/www/symfony/vendor/symfony/var-exporter/Internal/Registry.php"
      #line: 76
      trace: {
        /var/www/symfony/vendor/symfony/var-exporter/Internal/Registry.php:76 { …}
        /var/www/symfony/vendor/symfony/var-exporter/Internal/Exporter.php:80 { …}
        /var/www/symfony/vendor/symfony/var-exporter/VarExporter.php:55 { …}
        /var/www/symfony/vendor/symfony/cache/Traits/PhpFilesTrait.php:175 { …}
        /var/www/symfony/vendor/symfony/cache/Adapter/AbstractAdapter.php:239 { …}
        /var/www/symfony/vendor/symfony/cache/Adapter/AbstractAdapter.php:208 { …}
        /var/www/symfony/vendor/symfony/cache/Adapter/TraceableAdapter.php:123 { …}
        /var/www/symfony/vendor/shivas/versioning-bundle/Service/VersionManager.php:153 { …}
        /var/www/symfony/vendor/shivas/versioning-bundle/Command/StatusCommand.php:56 { …}
        /var/www/symfony/vendor/symfony/console/Command/Command.php:255 { …}
        /var/www/symfony/vendor/symfony/console/Application.php:919 { …}
        /var/www/symfony/vendor/symfony/framework-bundle/Console/Application.php:89 { …}
        /var/www/symfony/vendor/symfony/console/Application.php:262 { …}
        /var/www/symfony/vendor/symfony/framework-bundle/Console/Application.php:75 { …}
        /var/www/symfony/vendor/symfony/console/Application.php:145 { …}
        /var/www/symfony/bin/console:39 {
          › $application = new Application($kernel);
          › $application->run($input);
          › 
          arguments: {
            $input: Symfony\Component\Console\Input\ArgvInput {#1 …}
          }
        }
      }
    }

As also described here : shivas/versioning-bundle#39

One solution should be to use the Nogrod/Version fork : Nogrod@56c9a7b

Do you plan to include this correction to your library ?

Hi @partikule,

Thanks for letting me know about this one. I'm not thrilled with the solution in the referenced fork. Making constructor public kinda breaks encapsulation I wanted to enforce in my Value Object.

Will look at this in more detail and I'll think about the best solution. 👍

Thanks for your quick answer.
Btw and in the mean time you find a better solution, the solution provided by @Nogrod works.

I believe this is something Symfony specific, because native serialization of a Version object works. I've added a test case to cover it: https://github.com/nikolaposa/version/blob/master/tests/VersionTest.php#L139

@nikolaposa Symfony checks before serialization if the object is Instantiable (with ReflectionClass::Instantiable()). Because Version has a private constructor, this check fails, which finally leads to the warning.

@jbtronics I see that, but is it worth to change the design of a class just to meet Symfony or any other framework's behaviour? This is a generic library intended to be used in any context. Also I haven't heard any similar complaints about using this library in the context of Laravel or Zend Framework caching facilities for example.

It seems that this bug was fixed by a fix in the symfony var-dumper component (symfony/symfony@e354d54#diff-bc7a0b9933d7688a76f788080f091a11).

So no more warning since 4.2.7

Originally posted by @jbtronics in shivas/versioning-bundle#39 (comment)

Nice, closing this one.