Skip to content

Commit

Permalink
feature #28231 [VarExporter] a new component to serialize values to p…
Browse files Browse the repository at this point in the history
…lain PHP code (nicolas-grekas)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[VarExporter] a new component to serialize values to plain PHP code

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

This PR proposes moving what is currently the `PhpMarshaller` class in the Cache component to a separate component.

This component would provide only one public static method:
`VarExporter::export($value, bool &$isStaticValue = null): string`.

This method returns `$value` serialized as plain PHP code. Running this code creates the same exact data structure that `$value` contained. This is exactly like `serialize()` and `unserialize()`, from which all semantics are preserved (`__sleep`, `__wakeup` and `Serializable`).

The reason to use this method *vs* `serialize()` or even igbinary is performance: thanks to OPcache, the resulting code is significantly faster and more memory efficient than using `unserialize()` or `igbinary_unserialize()`.

Unlike `var_export()`, this works on any serializable PHP value.

It also provides a few improvements over `var_export()`/`serialize()`:
- the output is PSR-2 compatible
- the output can be re-indented without messing up with any `\r` or `\n` in the data
- missing classes throw a `ReflectionException` instead of being unserialized to a `PHP_Incomplete_Class` object
- references involving `SplObjectStorage`, `ArrayObject` or `ArrayIterator` instances are preserved
- `Reflection*`, `IteratorIterator` and `RecursiveIteratorIterator` classes throw an exception when being serialized (their unserialized version is broken anyway, see  https://bugs.php.net/76737.)

Commits
-------

7831ad7 [VarExporter] a new component to serialize values to plain PHP code
  • Loading branch information
fabpot committed Aug 27, 2018
2 parents ecff692 + 7831ad7 commit da0ef24
Show file tree
Hide file tree
Showing 47 changed files with 651 additions and 433 deletions.
2 changes: 1 addition & 1 deletion .php_cs.dist
Expand Up @@ -24,7 +24,6 @@ return PhpCsFixer\Config::create()
->exclude(array(
'Symfony/Bridge/ProxyManager/Tests/LazyProxy/PhpDumper/Fixtures',
// directories containing files with content that is autogenerated by `var_export`, which breaks CS in output code
'Symfony/Component/Cache/Tests/Marshaller/Fixtures',
'Symfony/Component/DependencyInjection/Tests/Fixtures',
'Symfony/Component/Routing/Tests/Fixtures/dumper',
// fixture templates
Expand All @@ -33,6 +32,7 @@ return PhpCsFixer\Config::create()
'Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Resources/Custom',
// generated fixtures
'Symfony/Component/VarDumper/Tests/Fixtures',
'Symfony/Component/VarExporter/Tests/Fixtures',
// resource templates
'Symfony/Bundle/FrameworkBundle/Resources/views/Form',
// explicit trigger_error tests
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -81,6 +81,7 @@
"symfony/twig-bundle": "self.version",
"symfony/validator": "self.version",
"symfony/var-dumper": "self.version",
"symfony/var-exporter": "self.version",
"symfony/web-link": "self.version",
"symfony/web-profiler-bundle": "self.version",
"symfony/web-server-bundle": "self.version",
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

21 changes: 0 additions & 21 deletions src/Symfony/Component/Cache/Tests/Marshaller/Fixtures/clone.php

This file was deleted.

31 changes: 0 additions & 31 deletions src/Symfony/Component/Cache/Tests/Marshaller/Fixtures/datetime.php

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

40 changes: 0 additions & 40 deletions src/Symfony/Component/Cache/Tests/Marshaller/Fixtures/private.php

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

31 changes: 0 additions & 31 deletions src/Symfony/Component/Cache/Tests/Marshaller/Fixtures/wakeup.php

This file was deleted.

0 comments on commit da0ef24

Please sign in to comment.