{"id":19001715,"url":"https://github.com/ledccn/macroable","last_synced_at":"2025-08-12T22:34:56.978Z","repository":{"id":185010148,"uuid":"672853794","full_name":"ledccn/macroable","owner":"ledccn","description":"通过PHP语言的trait特性，使用宏给一个类添加一个新方法","archived":false,"fork":false,"pushed_at":"2024-03-26T16:08:55.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-01T23:35:11.526Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"httpw://www.iyuu.cn","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ledccn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-31T10:11:30.000Z","updated_at":"2023-08-01T03:23:10.000Z","dependencies_parsed_at":"2024-11-08T18:12:33.271Z","dependency_job_id":"0bfd1b7d-bd48-41c4-b3cf-47855e4456e0","html_url":"https://github.com/ledccn/macroable","commit_stats":null,"previous_names":["ledccn/macroable"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledccn%2Fmacroable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledccn%2Fmacroable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledccn%2Fmacroable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ledccn%2Fmacroable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ledccn","download_url":"https://codeload.github.com/ledccn/macroable/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240021764,"owners_count":19735365,"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-11-08T18:12:24.970Z","updated_at":"2025-02-21T13:27:56.125Z","avatar_url":"https://github.com/ledccn.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 安装\n\n```\ncomposer require ledc/macroable\n```\n\n\n\n# 使用\n\n```\n\u003c?php\n\nuse Ledc\\Macroable\\Macro;\nuse Ledc\\Macroable\\Macroable;\n\nrequire_once __DIR__ . '/vendor/autoload.php';\nclass Tests\n{\n    //use Macroable;\n    use Macro;\n}\n\nclass Request\n{\n    //use Macroable;\n    use Macro;\n}\n\n$ts = new Tests();\n$ts-\u003emacro('hello', function () {\n    echo 'hello Tests' . PHP_EOL;\n});\n$ts-\u003ehello();\n\n$req = new Request();\n$req-\u003emacro('hello', function () {\n    echo 'hello Request' . PHP_EOL;\n});\n$req-\u003ehello();\n```\n\n## Usage\n\nYou can add a new method to a class using `macro`:\n\n```php\n$macroableClass = new class() {\n    use Ledc\\Macroable\\Macroable;\n};\n\n$macroableClass::macro('concatenate', function(... $strings) {\n   return implode('-', $strings);\n});\n\n$macroableClass-\u003econcatenate('one', 'two', 'three'); // returns 'one-two-three'\n```\n\nCallables passed to the `macro` function will be bound to the `class`\n\n```php\n$macroableClass = new class() {\n    protected $name = 'myName';\n    use Ledc\\Macroable\\Macroable;\n};\n\n$macroableClass::macro('getName', function() {\n   return $this-\u003ename;\n};\n\n$macroableClass-\u003egetName(); // returns 'myName'\n```\n\nYou can also add multiple methods in one go by using a mixin class. A mixin class contains methods that return callables. Each method from the mixin will be registered on the macroable class.\n\n```php\n$mixin = new class() {\n    public function mixinMethod()\n    {\n       return function() {\n          return 'mixinMethod';\n       };\n    }\n    \n    public function anotherMixinMethod()\n    {\n       return function() {\n          return 'anotherMixinMethod';\n       };\n    }\n};\n\n$macroableClass-\u003emixin($mixin);\n\n$macroableClass-\u003emixinMethod() // returns 'mixinMethod';\n\n$macroableClass-\u003eanotherMixinMethod() // returns 'anotherMixinMethod';\n```\n\n\n\n# 最佳实践\n\n- 宏指令方法不存在状态：用`Ledc\\Macroable\\Macroable`\n- 宏指令方法存在状态或依赖上下文时，用：`Ledc\\Macroable\\Macro`\n\n\n\n# 使用场景\n\n| 特性trait                  | 使用场景                                          |\n| -------------------------- | ------------------------------------------------- |\n| `Ledc\\Macroable\\Macroable` | PHP-FPM，或宏指令方法无状态                       |\n| `Ledc\\Macroable\\Macro`     | PHP-CLI，中间件内对请求对象注入方法(请求结束销毁) |\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fledccn%2Fmacroable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fledccn%2Fmacroable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fledccn%2Fmacroable/lists"}