Download the PHP package henzeb/var-export-wrapper without Composer
On this page you can find all versions of the php package henzeb/var-export-wrapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download henzeb/var-export-wrapper
More information about henzeb/var-export-wrapper
Files in henzeb/var-export-wrapper
Package var-export-wrapper
Short Description wrapping objects and closures for var_export
License AGPL-3.0-only
Homepage https://github.com/henzeb/laravel-console
Informations about the package var-export-wrapper
var_export wrapper
var_export is a good choice when caching config. This is how it is done in Laravel for example. But not every object is automatically exportable.
Imagine you want to make an ImageManipulationService
that resizes your images.
You want it to be configurable, so you get something like this:
``
You would need an option for each possible feature, or implement the new wanted feature whenever requested. You could work with invokable classes, but that won't make it readable.
What if you could do this:
``
With this package, you can export closures and objects that do not implement
__set_state
. You do not need laravel to use it, but when you do, it automatically
parses your configuration for artisan config:cache
Installation
Just install with the following command.
Usage
exportify
exportify
is what wraps the object or an array of objects
``
Note: exportify
also iterates through objects implementing Traversable
or ArrayAccess
.
__get_state
exportify
does not automatically wrap properties inside the object, and this is fine in most
cases. But sometimes you want to export objects such as closures inside an object, or specify what to
export. in order to do that, you can implement __get_state
on your object. This method should
return an array with the properties you want to restore with __get_state
.
``
Note: you do not need to use exportify here yourself, it is done automatically.
is_exportable
Validates the given object or array. If not exportable, it returns false.
``
var_export
var_export
is the supercharged version of the native function, but
under the hood it will automatically wrap everything that is not exportable
by default in a VarExportable
instance, before actually exporting the value.
``
var_export_file
var_export_file
is the same as var_export
, but it exports to a file instead of returning.
``
var_import
var_import
is useful when you want to import a var_exported string or file. This function
will automatically unwrap the VarExportable
instances. You can also pass an array that was
imported in another way, but still contains VarExportable
instances.
``
Laravel Config
When installed in a Laravel installation, you can just start using closures and objects
inside your configuration. When calling artisan config:cache
, var_export wrapper automatically
wraps them in a wrapper.
Closures under the hood
To be able to export closures, it has to serialize them. It uses laravel/serializable to achieve that. This means that if you've set a secret key, the closure is signed, otherwise it's natively serialized and thus unsigned.
You do not need to wrap closures before passing them to exportify
Testing
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
License
The GNU AGPLv. Please see License File for more information. ]()