{"id":18345819,"url":"https://github.com/hyperf/aop-integration","last_synced_at":"2025-07-14T03:06:18.388Z","repository":{"id":56987694,"uuid":"322997833","full_name":"hyperf/aop-integration","owner":"hyperf","description":"AOP Integration","archived":false,"fork":false,"pushed_at":"2023-02-09T14:49:52.000Z","size":15,"stargazers_count":9,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-30T02:49:00.084Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/hyperf.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},"funding":{"open_collective":"hyperf","custom":"https://hyperf.wiki/#/zh-cn/donate"}},"created_at":"2020-12-20T05:08:28.000Z","updated_at":"2024-12-17T14:50:38.000Z","dependencies_parsed_at":"2025-04-06T08:32:23.392Z","dependency_job_id":"a3e975b0-31eb-489f-995b-dda9c616662c","html_url":"https://github.com/hyperf/aop-integration","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/hyperf/aop-integration","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf%2Faop-integration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf%2Faop-integration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf%2Faop-integration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf%2Faop-integration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hyperf","download_url":"https://codeload.github.com/hyperf/aop-integration/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf%2Faop-integration/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265236722,"owners_count":23732497,"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-05T21:09:34.185Z","updated_at":"2025-07-14T03:06:18.285Z","avatar_url":"https://github.com/hyperf.png","language":"PHP","readme":"# Aop Integration\n\n![PHPUnit](https://github.com/hyperf/aop-integration/workflows/PHPUnit/badge.svg)\n\n## 安装\n\n```\ncomposer require hyperf/aop-integration\n```\n\n## 配置 AOP 到 ThinkPHP 框架\n\n1. 添加配置到 `config/config.php` 中\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\n! defined('BASE_PATH') \u0026\u0026 define('BASE_PATH', dirname(__DIR__, 1));\n\nreturn [\n    'annotations' =\u003e [\n        'scan' =\u003e [\n            'paths' =\u003e [\n                BASE_PATH . '/app',\n            ],\n            'ignore_annotations' =\u003e [\n                'mixin',\n            ],\n            'class_map' =\u003e [\n            ],\n        ],\n    ],\n    'aspects' =\u003e [\n        // 在此配置可用的 Aspect\n    ],\n];\n\n```\n\n2. 修改入口文件\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace think;\n\nuse Hyperf\\AopIntegration\\ClassLoader;\n\nrequire __DIR__ . '/../vendor/autoload.php';\n\n// 初始化 AOP\n! defined('BASE_PATH') \u0026\u0026 define('BASE_PATH', dirname(__DIR__, 1));\n\nClassLoader::init();\n\n// 省略其他代码\n\n```\n\n\n## 配置 AOP 到 Webman 框架\n\n### 增加 AOP 相关配置\n\n我们需要在 `config` 目录下，增加 `config.php` 配置\n\n```php\n\u003c?php\n\nuse Hyperf\\Di\\Annotation\\AspectCollector;\n\nreturn [\n    'annotations' =\u003e [\n        'scan' =\u003e [\n            'paths' =\u003e [\n                BASE_PATH . '/app',\n            ],\n            'ignore_annotations' =\u003e [\n                'mixin',\n            ],\n            'class_map' =\u003e [\n            ],\n            'collectors' =\u003e [\n                AspectCollector::class\n            ],\n        ],\n    ],\n    'aspects' =\u003e [\n        // 这里写入对应的 Aspect\n        app\\aspect\\DebugAspect::class,\n    ]\n];\n\n```\n\n### 配置入口文件 start.php\n\n\u003e 我们将初始化方法，放到 timezone 下方，以下省略其他代码\n\n```php\nuse Hyperf\\AopIntegration\\ClassLoader;\n\nif ($timezone = config('app.default_timezone')) {\n    date_default_timezone_set($timezone);\n}\n\n// 初始化\nClassLoader::init();\n```\n\n### 测试\n\n首先让我们编写待切入类\n\n```php\n\u003c?php\nnamespace app\\service;\n\nclass UserService\n{\n    public function first(): array\n    {\n        return ['id' =\u003e 1];\n    }\n}\n```\n\n其次新增对应的 `DebugAspect`\n\n```php\n\u003c?php\nnamespace app\\aspect;\n\nuse app\\service\\UserService;\nuse Hyperf\\Di\\Aop\\AbstractAspect;\nuse Hyperf\\Di\\Aop\\ProceedingJoinPoint;\n\nclass DebugAspect extends AbstractAspect\n{\n    public $classes = [\n        UserService::class . '::first',\n    ];\n\n    public function process(ProceedingJoinPoint $proceedingJoinPoint)\n    {\n        var_dump(11);\n        return $proceedingJoinPoint-\u003eprocess();\n    }\n}\n```\n\n接下来编辑控制器 `app\\controller\\Index`\n\n```php\n\u003c?php\nnamespace app\\controller;\n\nuse app\\service\\UserService;\nuse support\\Request;\n\nclass Index\n{\n    public function json(Request $request)\n    {\n        return json(['code' =\u003e 0, 'msg' =\u003e 'ok', 'data' =\u003e (new UserService())-\u003efirst()]);\n    }\n}\n```\n\n最后启动服务，并测试。\n\n```shell\nphp start.php start\ncurl  http://127.0.0.1:8787/index/json\n```\n","funding_links":["https://opencollective.com/hyperf","https://hyperf.wiki/#/zh-cn/donate"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyperf%2Faop-integration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhyperf%2Faop-integration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyperf%2Faop-integration/lists"}