{"id":21451333,"url":"https://github.com/anizoptera/azaphpgen","last_synced_at":"2026-03-09T19:32:22.481Z","repository":{"id":7124358,"uuid":"8419318","full_name":"Anizoptera/AzaPhpGen","owner":"Anizoptera","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.","archived":false,"fork":false,"pushed_at":"2013-05-28T13:21:16.000Z","size":184,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-24T20:41:39.639Z","etag":null,"topics":["anizoptera-cmf","array-dump","code-generation","composer","packagist","php","php-library","serializer","travis"],"latest_commit_sha":null,"homepage":"https://github.com/Anizoptera/AzaPhpGen","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Anizoptera.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-02-25T22:06:26.000Z","updated_at":"2017-01-24T22:57:57.000Z","dependencies_parsed_at":"2022-08-26T10:11:41.432Z","dependency_job_id":null,"html_url":"https://github.com/Anizoptera/AzaPhpGen","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anizoptera%2FAzaPhpGen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anizoptera%2FAzaPhpGen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anizoptera%2FAzaPhpGen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anizoptera%2FAzaPhpGen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Anizoptera","download_url":"https://codeload.github.com/Anizoptera/AzaPhpGen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225999107,"owners_count":17557522,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["anizoptera-cmf","array-dump","code-generation","composer","packagist","php","php-library","serializer","travis"],"created_at":"2024-11-23T04:20:35.047Z","updated_at":"2026-03-09T19:32:22.427Z","avatar_url":"https://github.com/Anizoptera.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"AzaPhpGen\n=========\n\nAnizoptera CMF PHP code generation (dump, serialization) component.\n\nhttps://github.com/Anizoptera/AzaPhpGen\n\n[![Build Status][TravisImage]][Travis]\n\n\nTable of Contents\n-----------------\n\n1. [Introduction](#introduction)\n2. [Requirements](#requirements)\n3. [Installation](#installation)\n4. [Examples](#examples)\n   * [Simple dump](#example-1---simple-dump)\n   * [Array dump](#example-2---array-dump)\n   * [Traversable dump](#example-3---traversable-dump)\n   * [Closure (anonymous function) example](#example-4---closure-anonymous-function-example)\n   * [Custom object dumping with IPhpGenerable interface](#example-5---custom-object-dumping-with-iphpgenerable-interface)\n   * [Bundled CustomCode class usage](#example-6---bundled-customcode-class-usage)\n   * [Custom object dumping with defined handlers](#example-7---custom-object-dumping-with-defined-handlers)\n   * [AzaPhpGen customization](#example-8---azaphpgen-customization)\n5. [Tests](#tests)\n6. [Credits](#credits)\n7. [License](#license)\n8. [Links](#links)\n\n\nIntroduction\n------------\n\nAllows to dump complex arrays, objects, closures and basic data types as php code.\nIn part, this can be called a some sort of serialization.\nAnd you can customize your dumped php code as you wish.\n\nIt is very usefull for code compilation (usually for caching purposes).\n\n\n**Features:**\n\n- Supports all scalar values (bool, int, float, string), nulls, arrays, serializable objects;\n- [Traversable](http://php.net/traversable) support (dumped as array, see usage in [Example #3](#example-3---traversable-dump));\n- Closures support (closures with \"use\", several closures on the same line are not supported!) (see usage and more info in [Example #4](#example-4---closure-anonymous-function-example));\n- Custom object dumping with [IPhpGenerable interface](IPhpGenerable.php) (see usage in [Example #5](#example-5---custom-object-dumping-with-iphpgenerable-interface));\n- Bundled simple [CustomCode class](CustomCode.php) (see usage in [Example #6](#example-6---bundled-customcode-class-usage));\n- Custom object dumping with defined handlers/hooks (see usage in [Example #7](#example-7---custom-object-dumping-with-defined-handlers));\n- Very flexible configuration (9 code building options, see in [PhpGen class code](PhpGen.php#L19));\n- Automatic recognition of binary strings;\n- Convenient, fully documented and test covered API;\n\n\n**Benefits over `var_export()`:**\n\n- `var_export` does not support Closures dumping;\n- `var_export` supports only objects with `__set_state` function. AzaPhpGen supports all serializable objects;\n- AzaPhpGen dumps Traversable objects as arrays (via `iterator_to_array`);\n- For binary strings `var_export` generates very ugly code that is awkward to use and can be easily corrupted;\n- For objects `var_export` generates code that can not be evaluated in namespace;\n- AzaPhpGen give you full control over objects dumping with custom handlers and `IPhpGenerable` interface;\n- With AzaPhpGen you can flexibly customize formatting of your code (useful for arrays);\n- AzaPhpGen can generate code with or without trailing semicolon. `var_export` never outputs it :)\n- Some detailed comparisons you can see in [Tests/PhpGenBenchmarkTest.php](Tests/PhpGenBenchmarkTest.php#L647);\n\n\nRequirements\n------------\n\n* PHP 5.3.3 (or later);\n* SPL and Reflection extensions for closures support (both bundled with PHP by default);\n\n\nInstallation\n------------\n\nThe recommended way to install AzaPhpGen is [through composer](http://getcomposer.org).\nYou can see [package information on Packagist][ComposerPackage].\n\n```JSON\n{\n\t\"require\": {\n\t\t\"aza/phpgen\": \"~1.0\"\n\t}\n}\n```\n\n\nExamples\n--------\n\nYou can use [examples/example.php](examples/example.php) to run all examples.\n\n#### Example #1 - Simple dump\n\n```php\n// Get singleton instance of PhpGen (fast and simple variant)\n$phpGen = PhpGen::instance();\n// Integer\necho $phpGen-\u003egetCode(123456789) . PHP_EOL; // 123456789;\n// String (binary strings are supported as well)\necho $phpGen-\u003egetCode('some string' . ' example') . PHP_EOL; // \"some string example\";\n// Float without trailing semicolon\necho $phpGen-\u003egetCodeNoTail(12.345) . PHP_EOL; // 12.345\n// Simple serializable objects\n$var = new stdClass();\necho $phpGen-\u003egetCode($var) . PHP_EOL; // unserialize(\"O:8:\\\"stdClass\\\":0:{}\");\n// Another object example\n$var = new DateTime('2013-02-23 00:49:36', new DateTimeZone('UTC'));\necho $phpGen-\u003egetCode($var) . PHP_EOL; // unserialize(\"O:8:\\\"DateTime\\\":3:{s:4:\\\"date\\\";s:19:\\\"2013-02-23 00:49:36\\\";s:13:\\\"timezone_type\\\";i:3;s:8:\\\"timezone\\\";s:3:\\\"UTC\\\";}\");\n```\n\n#### Example #2 - Array dump\n\n```php\n// AzaPhpGen will use short array syntax if possible by default (PHP \u003e= 5.4)\necho $phpGen-\u003egetCode(array(\n\ttrue, false, null\n)) . PHP_EOL;\n/*\n[\n\ttrue,\n\tfalse,\n\tnull,\n];\n */\n\n// Build code without formatting\necho $phpGen-\u003egetCodeNoFormat(array(\n\ttrue, false, null\n)) . PHP_EOL;\n/*\n[true,false,null];\n */\n\n// Complex array (some sort of config for example)\n$array = array(\n\t'key1'     =\u003e 'value',\n\t'long_key' =\u003e 'value',\n\t'array'    =\u003e array(\n\t\t'short_value'\n\t),\n\t'array2' =\u003e array(\n\t\t'very very very very very very very very very very very very long value'\n\t),\n\t'other',\n\t123456789\n);\necho $phpGen-\u003egetCode($array) . PHP_EOL;\n/*\n[\n\t\"key1\"     =\u003e \"value\",\n\t\"long_key\" =\u003e \"value\",\n\t\"array\"    =\u003e [\"short_value\"],\n\t\"array2\"   =\u003e [\n\t\t\"very very very very very very very very very very very very long value\",\n\t],\n\t0 =\u003e \"other\",\n\t1 =\u003e 123456789,\n];\n */\n\n// And wothout formatting\necho $phpGen-\u003egetCodeNoFormat($array) . PHP_EOL;\n/*\n[\"key1\"=\u003e\"value\",\"long_key\"=\u003e\"value\",\"array\"=\u003e[\"short_value\"],\"array2\"=\u003e[\"very very very very very very very very very very very very long value\"],0=\u003e\"other\",1=\u003e123456789];\n */\n```\n\n#### Example #3 - Traversable dump\n\nAzaPhpGen treat all Traversable objects as arrays (with [iterator_to_array](http://php.net/iterator-to-array)).\n\n```php\n$var = new SplFixedArray(3);\n$var[0] = 'a';\n$var[1] = 'b';\necho $phpGen-\u003egetCodeNoFormat($var) . PHP_EOL; // [\"a\",\"b\",null];\n```\n\n#### Example #4 - Closure (anonymous function) example\n\n**WARNING:** Closures are dumped as is. So complex closures are not supported:\n* Closures with \"use\" statement (closures that inherit variables from the parent scope);\n* Several closures on the same line;\n* Usage of non-qualified class name (with importing) in closure;\n* Closures with `$this` variable usage;\n\n```php\n$closure = function($a, $b) {\n\treturn round($a, $b) . \"example\\t\\n\";\n};\necho $phpGen-\u003egetCode($closure) . PHP_EOL;\n/*\nfunction($a, $b) {\n\treturn round($a, $b) . \"example\\t\\n\";\n};\n */\necho $phpGen-\u003egetCode(array('key' =\u003e $closure)) . PHP_EOL;\n/*\n[\n\t\"key\" =\u003e function($a, $b) {\n\treturn round($a, $b) . \"example\\t\\n\";\n},\n];\n */\n```\n\n#### Example #5 - Custom object dumping with IPhpGenerable interface\n\nYou can customize dumping of your classes by implementing the `IPhpGenerable` interface.\n\n```php\nclass ExampleCustomCode implements IPhpGenerable\n{\n\tpublic function generateCode()\n\t{\n\t\treturn '32434 + 5678';\n\t}\n}\n\n$var = new ExampleCustomCode();\n\necho $phpGen-\u003egetCode($var) . PHP_EOL; // 32434 + 5678;\n\necho $phpGen-\u003egetCode(array($var)) . PHP_EOL; // [32434 + 5678];\n```\n\n#### Example #6 - Bundled CustomCode class usage\n\nFor the simpliest varint of `IPhpGenerable` interface usages you can use bundled class - `CustomCode`.\nIt just takes the required code as a constructor argument.\n\n```php\n$var = new CustomCode('\"some code\" . PHP_EOL');\n\necho $phpGen-\u003egetCode($var) . PHP_EOL; // \"some code\" . PHP_EOL;\n\necho $phpGen-\u003egetCode(array($var)) . PHP_EOL; // [\"some code\" . PHP_EOL];\n```\n\n#### Example #7 - Custom object dumping with defined handlers\n\nSecond varint of resulting code customization - usage of defined handlers (hooks) for the classes.\nThis way you can customize dump of any possible class!\n\n```php\n// Set custom handler for DateTime type\n$phpGen-\u003eaddCustomHandler('DateTime', function($data) use ($phpGen) {\n\t/** @var $data \\DateTime */\n\treturn $phpGen-\u003egetCodeNoTail(\n\t\t$data-\u003eformat(\"Y-m-dO\")\n\t);\n});\n// Build code\n$var = new DateTime('2013-02-23 00:49:36', new DateTimeZone('UTC'));\necho $phpGen-\u003egetCode($var) . PHP_EOL; // \"2013-02-23+0000\";\n```\n\n#### Example #8 - AzaPhpGen customization\n\nAzaPhpGen has many options. So it's very simple to configure your resulting code\nfor your special needs (code style for example).\nYou can see all available options in the [PhpGen class code](PhpGen.php#L19).\n\n```php\n// Disable short array syntax and use 6 spaces for indentation\n$phpGen-\u003eshortArraySyntax = false;\n$phpGen-\u003euseSpaces        = true;\n$phpGen-\u003etabLength        = 6;\n$var = array(array(array(23 =\u003e 'example')));\necho $phpGen-\u003egetCode($var) . PHP_EOL;\n/*\narray(\n      array(\n            array(\n                  23 =\u003e \"example\",\n            ),\n      ),\n);\n */\n```\n\n\nTests\n-----\n\nTests are in the `Tests` folder and reach 100% code-coverage.\nTo run them, you need PHPUnit.\nExample:\n\n    $ phpunit --configuration phpunit.xml.dist\n\nOr with coverage report:\n\n    $ phpunit --configuration phpunit.xml.dist --coverage-html code_coverage/\n\n\nCredits\n-------\n\nAzaPhpGen is a part of [Anizoptera CMF][], written by [Amal Samally][] (amal.samally at gmail.com) and [AzaGroup][] team.\n\n\nLicense\n-------\n\nReleased under the [MIT](LICENSE.md) license.\n\n\nLinks\n-----\n\n* [Composer package][ComposerPackage]\n* [Last build on the Travis CI][Travis]\n* [Project profile on the Ohloh](https://www.ohloh.net/p/AzaPhpGen)\n* Other Anizoptera CMF components on the [GitHub][Anizoptera CMF] / [Packagist](https://packagist.org/packages/aza)\n* (RU) [AzaGroup team blog][AzaGroup]\n\n\n\n[Anizoptera CMF]:  https://github.com/Anizoptera\n[Amal Samally]:    http://azagroup.ru/about/#amal\n[AzaGroup]:        http://azagroup.ru/\n[ComposerPackage]: https://packagist.org/packages/aza/phpgen\n[TravisImage]:     https://secure.travis-ci.org/Anizoptera/AzaPhpGen.png?branch=master\n[Travis]:          http://travis-ci.org/Anizoptera/AzaPhpGen\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanizoptera%2Fazaphpgen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanizoptera%2Fazaphpgen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanizoptera%2Fazaphpgen/lists"}