Download the PHP package aza/phpgen without Composer
On this page you can find all versions of the php package aza/phpgen. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package phpgen
Short Description AzaPhpGen - Anizoptera CMF PHP code generation (dump) component. Allows to dump complex arrays, objects, closures and basic data types as php code. In part, this can be called a some sort of serialization. You can customize your dumped php code as you wish.
License MIT
Homepage https://github.com/Anizoptera/AzaPhpGen
Informations about the package phpgen
AzaPhpGen
Anizoptera CMF PHP code generation (dump, serialization) component.
https://github.com/Anizoptera/AzaPhpGen
Table of Contents
- Introduction
- Requirements
- Installation
- Examples
- Simple dump
- Array dump
- Traversable dump
- Closure (anonymous function) example
- Custom object dumping with IPhpGenerable interface
- Bundled CustomCode class usage
- Custom object dumping with defined handlers
- AzaPhpGen customization
- Tests
- Credits
- License
- Links
Introduction
Allows to dump complex arrays, objects, closures and basic data types as php code. In part, this can be called a some sort of serialization. And you can customize your dumped php code as you wish.
It is very usefull for code compilation (usually for caching purposes).
Features:
- Supports all scalar values (bool, int, float, string), nulls, arrays, serializable objects;
- Traversable support (dumped as array, see usage in Example #3);
- Closures support (closures with "use", several closures on the same line are not supported!) (see usage and more info in Example #4);
- Custom object dumping with Example #5);
- Bundled simple Example #6);
- Custom object dumping with defined handlers/hooks (see usage in Example #7);
- Very flexible configuration (9 code building options, see in PhpGen class code);
- Automatic recognition of binary strings;
- Convenient, fully documented and test covered API;
Benefits over var_export()
:
var_export
does not support Closures dumping;var_export
supports only objects with__set_state
function. AzaPhpGen supports all serializable objects;- AzaPhpGen dumps Traversable objects as arrays (via
iterator_to_array
); - For binary strings
var_export
generates very ugly code that is awkward to use and can be easily corrupted; - For objects
var_export
generates code that can not be evaluated in namespace; - AzaPhpGen give you full control over objects dumping with custom handlers and
IPhpGenerable
interface; - With AzaPhpGen you can flexibly customize formatting of your code (useful for arrays);
- AzaPhpGen can generate code with or without trailing semicolon.
var_export
never outputs it :) - Some detailed comparisons you can see in Tests/PhpGenBenchmarkTest.php;
Requirements
- PHP 5.3.3 (or later);
- SPL and Reflection extensions for closures support (both bundled with PHP by default);
Installation
The recommended way to install AzaPhpGen is through composer. You can see package information on Packagist.
Examples
You can use examples/example.php to run all examples.
Example #1 - Simple dump
Example #2 - Array dump
Example #3 - Traversable dump
AzaPhpGen treat all Traversable objects as arrays (with iterator_to_array).
Example #4 - Closure (anonymous function) example
WARNING: Closures are dumped as is. So complex closures are not supported:
- Closures with "use" statement (closures that inherit variables from the parent scope);
- Several closures on the same line;
- Usage of non-qualified class name (with importing) in closure;
- Closures with
$this
variable usage;
Example #5 - Custom object dumping with IPhpGenerable interface
You can customize dumping of your classes by implementing the IPhpGenerable
interface.
Example #6 - Bundled CustomCode class usage
For the simpliest varint of IPhpGenerable
interface usages you can use bundled class - CustomCode
.
It just takes the required code as a constructor argument.
Example #7 - Custom object dumping with defined handlers
Second varint of resulting code customization - usage of defined handlers (hooks) for the classes. This way you can customize dump of any possible class!
Example #8 - AzaPhpGen customization
AzaPhpGen has many options. So it's very simple to configure your resulting code for your special needs (code style for example). You can see all available options in the PhpGen class code.
Tests
Tests are in the Tests
folder and reach 100% code-coverage.
To run them, you need PHPUnit.
Example:
$ phpunit --configuration phpunit.xml.dist
Or with coverage report:
$ phpunit --configuration phpunit.xml.dist --coverage-html code_coverage/
Credits
AzaPhpGen is a part of Anizoptera CMF, written by Amal Samally (amal.samally at gmail.com) and AzaGroup team.
License
Released under the MIT license.
Links
- Composer package
- Last build on the Travis CI
- Project profile on the Ohloh
- Other Anizoptera CMF components on the GitHub / Packagist
- (RU) AzaGroup team blog