承接 aza/phpgen 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aza/phpgen

最新稳定版本:v1.1.1

Composer 安装命令:

composer require aza/phpgen

包简介

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.

README 文档

README

Anizoptera CMF PHP code generation (dump, serialization) component.

https://github.com/Anizoptera/AzaPhpGen

Build Status

Table of Contents

  1. Introduction
  2. Requirements
  3. Installation
  4. Examples
  5. Tests
  6. Credits
  7. License
  8. 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 IPhpGenerable interface (see usage in Example #5);
  • Bundled simple CustomCode class (see usage in 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.

{
	"require": {
		"aza/phpgen": "~1.0"
	}
}

Examples

You can use examples/example.php to run all examples.

Example #1 - Simple dump

// Get singleton instance of PhpGen (fast and simple variant)
$phpGen = PhpGen::instance();
// Integer
echo $phpGen->getCode(123456789) . PHP_EOL; // 123456789;
// String (binary strings are supported as well)
echo $phpGen->getCode('some string' . ' example') . PHP_EOL; // "some string example";
// Float without trailing semicolon
echo $phpGen->getCodeNoTail(12.345) . PHP_EOL; // 12.345
// Simple serializable objects
$var = new stdClass();
echo $phpGen->getCode($var) . PHP_EOL; // unserialize("O:8:\"stdClass\":0:{}");
// Another object example
$var = new DateTime('2013-02-23 00:49:36', new DateTimeZone('UTC'));
echo $phpGen->getCode($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\";}");

Example #2 - Array dump

// AzaPhpGen will use short array syntax if possible by default (PHP >= 5.4)
echo $phpGen->getCode(array(
	true, false, null
)) . PHP_EOL;
/*
[
	true,
	false,
	null,
];
 */

// Build code without formatting
echo $phpGen->getCodeNoFormat(array(
	true, false, null
)) . PHP_EOL;
/*
[true,false,null];
 */

// Complex array (some sort of config for example)
$array = array(
	'key1'     => 'value',
	'long_key' => 'value',
	'array'    => array(
		'short_value'
	),
	'array2' => array(
		'very very very very very very very very very very very very long value'
	),
	'other',
	123456789
);
echo $phpGen->getCode($array) . PHP_EOL;
/*
[
	"key1"     => "value",
	"long_key" => "value",
	"array"    => ["short_value"],
	"array2"   => [
		"very very very very very very very very very very very very long value",
	],
	0 => "other",
	1 => 123456789,
];
 */

// And wothout formatting
echo $phpGen->getCodeNoFormat($array) . PHP_EOL;
/*
["key1"=>"value","long_key"=>"value","array"=>["short_value"],"array2"=>["very very very very very very very very very very very very long value"],0=>"other",1=>123456789];
 */

Example #3 - Traversable dump

AzaPhpGen treat all Traversable objects as arrays (with iterator_to_array).

$var = new SplFixedArray(3);
$var[0] = 'a';
$var[1] = 'b';
echo $phpGen->getCodeNoFormat($var) . PHP_EOL; // ["a","b",null];

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;
$closure = function($a, $b) {
	return round($a, $b) . "example\t\n";
};
echo $phpGen->getCode($closure) . PHP_EOL;
/*
function($a, $b) {
	return round($a, $b) . "example\t\n";
};
 */
echo $phpGen->getCode(array('key' => $closure)) . PHP_EOL;
/*
[
	"key" => function($a, $b) {
	return round($a, $b) . "example\t\n";
},
];
 */

Example #5 - Custom object dumping with IPhpGenerable interface

You can customize dumping of your classes by implementing the IPhpGenerable interface.

class ExampleCustomCode implements IPhpGenerable
{
	public function generateCode()
	{
		return '32434 + 5678';
	}
}

$var = new ExampleCustomCode();

echo $phpGen->getCode($var) . PHP_EOL; // 32434 + 5678;

echo $phpGen->getCode(array($var)) . PHP_EOL; // [32434 + 5678];

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.

$var = new CustomCode('"some code" . PHP_EOL');

echo $phpGen->getCode($var) . PHP_EOL; // "some code" . PHP_EOL;

echo $phpGen->getCode(array($var)) . PHP_EOL; // ["some code" . PHP_EOL];

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!

// Set custom handler for DateTime type
$phpGen->addCustomHandler('DateTime', function($data) use ($phpGen) {
	/** @var $data \DateTime */
	return $phpGen->getCodeNoTail(
		$data->format("Y-m-dO")
	);
});
// Build code
$var = new DateTime('2013-02-23 00:49:36', new DateTimeZone('UTC'));
echo $phpGen->getCode($var) . PHP_EOL; // "2013-02-23+0000";

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.

// Disable short array syntax and use 6 spaces for indentation
$phpGen->shortArraySyntax = false;
$phpGen->useSpaces        = true;
$phpGen->tabLength        = 6;
$var = array(array(array(23 => 'example')));
echo $phpGen->getCode($var) . PHP_EOL;
/*
array(
      array(
            array(
                  23 => "example",
            ),
      ),
);
 */

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

aza/phpgen 适用场景与选型建议

aza/phpgen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.37k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2013 年 02 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「serialization」 「dump」 「code」 「generation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 aza/phpgen 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 aza/phpgen 我们能提供哪些服务?
定制开发 / 二次开发

基于 aza/phpgen 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.37k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 29
  • 依赖项目数: 3
  • 推荐数: 1

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-02-26