{"id":15374308,"url":"https://github.com/yurunsoft/php-macro","last_synced_at":"2025-04-15T12:31:37.191Z","repository":{"id":57088638,"uuid":"432345792","full_name":"Yurunsoft/php-macro","owner":"Yurunsoft","description":"支持在 PHP 代码中使用类似 C/C++ 中的宏，进行代码预编译。可以方便兼容不同版本和环境下运行的 PHP 代码。","archived":false,"fork":false,"pushed_at":"2022-11-15T08:39:14.000Z","size":44,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-12T21:46:42.571Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Yurunsoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-11-27T02:05:10.000Z","updated_at":"2024-12-16T05:35:47.000Z","dependencies_parsed_at":"2023-01-23T05:46:10.847Z","dependency_job_id":null,"html_url":"https://github.com/Yurunsoft/php-macro","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2Fphp-macro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2Fphp-macro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2Fphp-macro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2Fphp-macro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Yurunsoft","download_url":"https://codeload.github.com/Yurunsoft/php-macro/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249072245,"owners_count":21208143,"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":[],"created_at":"2024-10-01T13:58:18.663Z","updated_at":"2025-04-15T12:31:36.913Z","avatar_url":"https://github.com/Yurunsoft.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-macro\n\n[![Latest Version](https://img.shields.io/packagist/v/yurunsoft/macro.svg)](https://packagist.org/packages/yurunsoft/macro)\n[![Test](https://github.com/Yurunsoft/php-macro/actions/workflows/test.yml/badge.svg)](https://github.com/Yurunsoft/php-macro/actions/workflows/test.yml)\n[![Php Version](https://img.shields.io/badge/php-%3E=7.1-brightgreen.svg)](https://secure.php.net/)\n\n## 介绍\n\n支持在 PHP 代码中使用类似 C/C++ 中的宏，进行代码预编译。可以方便兼容不同版本和环境下运行的 PHP 代码。\n\n## 使用\n\n安装：`composer require yurunsoft/macro`\n\n### 支持的宏\n\n#### 常量\n\n**宏：**`#define`、`#const`、`#ifdef`、`#ifndef`\n\n**例子：**\n\n```php\n#ifndef IN_SWOOLE\n    # define IN_SWOOLE extension_loaded('swoole')\n#endif\n#ifdef IN_SWOOLE\n#if IN_SWOOLE\n\\Co\\run(function(){\n    echo 'hello world';\n});\n#endif\n#endif\n```\n\n\u003e 注意：使用宏定义的常量，仅在生成代码时有效，运行时无效\n\n#### 条件语句\n\n**宏：**`#if`、`#else`、`#elif`、`#endif`\n\n**例子：**\n\n```php\n\u003c?php\n#if version_compare(\\PHP_VERSION, '8.0', '\u003e=')\nfunction test(): string|false\n#else\n/**\n * @return string|false\n */\nfunction test()\n#endif\n{\n    return 'hello world';\n}\n```\n\nPHP \u003e= 8.0 环境下生成的代码：\n\n```php\n\u003c?php\nfunction test(): string|false\n{\n    return 'hello world';\n}\n```\n\nPHP \u003c 8.0 环境下生成的代码：\n\n```php\n\u003c?php\n/**\n * @return string|false\n */\nfunction test()\n{\n    return 'hello world';\n}\n```\n\n### MacroParser 方法\n\n类：`\\Yurun\\Macro\\MacroParser`\n\n#### setTmpPath\n\n设置生成 PHP 代码的临时目录\n\n`MacroParser::setTmpPath(string $tmpPath): void`\n\n#### getTmpPath\n\n获取生成 PHP 代码的临时目录\n\n`MacroParser::getTmpPath(): string`\n\n#### includeFile\n\n直接加载带有宏代码的文件，内部会自动预编译并加载最终的文件\n\n`MacroParser::includeFile(string $file, string $destFile = '', bool $deleteFile = true): mixed`\n\n\u003e 这个最为常用\n\n#### convert\n\n将带有宏的代码，转换为预编译后的 PHP 代码\n\n`MacroParser::parse(string $content): string`\n\n#### convertFile\n\n将带有宏代码的文件，转换为预编译后的 PHP 代码并保存到目标文件。\n\n方法返回值是预编译后的 PHP 代码。\n\n`MacroParser::convertFile(string $srcFile, string $destFile = ''): string`\n\n#### parse\n\n将带有宏的代码，编译成预编译的 PHP 代码\n\n`MacroParser::parse(string $content): string`\n\n#### execParsedCode\n\n执行预编译的 PHP 代码，返回预编译后的 PHP 代码\n\n`MacroParser::execParsedCode(string $code): string`\n\n## 注意事项\n\n* 你的代码的 PHP 字符串中不能出现：`\u003c?php`、`?\u003e`。如果有可以拆开使用拼接的方式\n\n**例子：**\n\n```php\n\u003c?php\necho '\u003c?php echo \"hello world\"; ?\u003e'; // 错误的写法\necho '\u003c?' . 'php echo \"hello world\"; ?' . '\u003e'; // 正确的写法\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyurunsoft%2Fphp-macro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyurunsoft%2Fphp-macro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyurunsoft%2Fphp-macro/lists"}