yurunsoft/macro
Composer 安装命令:
composer require yurunsoft/macro
包简介
支持在 PHP 代码中使用类似 C/C++ 中的宏,进行代码预编译。可以方便兼容不同版本和环境下运行的 PHP 代码。
README 文档
README
介绍
支持在 PHP 代码中使用类似 C/C++ 中的宏,进行代码预编译。可以方便兼容不同版本和环境下运行的 PHP 代码。
使用
安装:composer require yurunsoft/macro
支持的宏
常量
宏:#define、#const、#ifdef、#ifndef
例子:
#ifndef IN_SWOOLE # define IN_SWOOLE extension_loaded('swoole') #endif #ifdef IN_SWOOLE #if IN_SWOOLE \Co\run(function(){ echo 'hello world'; }); #endif #endif
注意:使用宏定义的常量,仅在生成代码时有效,运行时无效
条件语句
宏:#if、#else、#elif、#endif
例子:
<?php #if version_compare(\PHP_VERSION, '8.0', '>=') function test(): string|false #else /** * @return string|false */ function test() #endif { return 'hello world'; }
PHP >= 8.0 环境下生成的代码:
<?php function test(): string|false { return 'hello world'; }
PHP < 8.0 环境下生成的代码:
<?php /** * @return string|false */ function test() { return 'hello world'; }
MacroParser 方法
类:\Yurun\Macro\MacroParser
setTmpPath
设置生成 PHP 代码的临时目录
MacroParser::setTmpPath(string $tmpPath): void
getTmpPath
获取生成 PHP 代码的临时目录
MacroParser::getTmpPath(): string
includeFile
直接加载带有宏代码的文件,内部会自动预编译并加载最终的文件
MacroParser::includeFile(string $file, string $destFile = '', bool $deleteFile = true): mixed
这个最为常用
convert
将带有宏的代码,转换为预编译后的 PHP 代码
MacroParser::parse(string $content): string
convertFile
将带有宏代码的文件,转换为预编译后的 PHP 代码并保存到目标文件。
方法返回值是预编译后的 PHP 代码。
MacroParser::convertFile(string $srcFile, string $destFile = ''): string
parse
将带有宏的代码,编译成预编译的 PHP 代码
MacroParser::parse(string $content): string
execParsedCode
执行预编译的 PHP 代码,返回预编译后的 PHP 代码
MacroParser::execParsedCode(string $code): string
注意事项
- 你的代码的 PHP 字符串中不能出现:
<?php、?>。如果有可以拆开使用拼接的方式
例子:
<?php echo '<?php echo "hello world"; ?>'; // 错误的写法 echo '<?' . 'php echo "hello world"; ?' . '>'; // 正确的写法
yurunsoft/macro 适用场景与选型建议
yurunsoft/macro 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 83.6k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2021 年 11 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 yurunsoft/macro 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yurunsoft/macro 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 83.6k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 2
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-27