{"id":22793184,"url":"https://github.com/ngmy/laravel.aop","last_synced_at":"2025-04-16T18:49:14.602Z","repository":{"id":231890338,"uuid":"782962585","full_name":"ngmy/Laravel.Aop","owner":"ngmy","description":"Laravel.Aop integrates Ray.Aop with Laravel.","archived":false,"fork":false,"pushed_at":"2025-02-26T02:48:06.000Z","size":182,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T05:33:41.606Z","etag":null,"topics":["aop","aspect","laravel"],"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/ngmy.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","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":{"github":"ngmy","patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":"https://flattr.com/@ngmy"}},"created_at":"2024-04-06T14:47:10.000Z","updated_at":"2025-02-26T02:44:30.000Z","dependencies_parsed_at":"2024-07-25T19:23:50.639Z","dependency_job_id":"aa4f2b60-21d1-4b2a-a9f3-94fc45cbd0bf","html_url":"https://github.com/ngmy/Laravel.Aop","commit_stats":null,"previous_names":["ngmy/laravel.aop"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngmy%2FLaravel.Aop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngmy%2FLaravel.Aop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngmy%2FLaravel.Aop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ngmy%2FLaravel.Aop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ngmy","download_url":"https://codeload.github.com/ngmy/Laravel.Aop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248897906,"owners_count":21179675,"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":["aop","aspect","laravel"],"created_at":"2024-12-12T03:18:27.620Z","updated_at":"2025-04-16T18:49:14.575Z","avatar_url":"https://github.com/ngmy.png","language":"PHP","funding_links":["https://github.com/sponsors/ngmy","https://flattr.com/@ngmy"],"categories":[],"sub_categories":[],"readme":"# Laravel.Aop\n\n[![Latest Stable Version](https://img.shields.io/packagist/v/ngmy/laravel.aop.svg?style=flat-square\u0026label=stable)](https://packagist.org/packages/ngmy/laravel.aop)\n[![Test Status](https://img.shields.io/github/actions/workflow/status/ngmy/laravel.aop/test.yml?style=flat-square\u0026label=test)](https://github.com/ngmy/Laravel.aop/actions/workflows/test.yml)\n[![Lint Status](https://img.shields.io/github/actions/workflow/status/ngmy/laravel.aop/lint.yml?style=flat-square\u0026label=lint)](https://github.com/ngmy/Laravel.aop/actions/workflows/lint.yml)\n[![Code Coverage](https://img.shields.io/coverallsCoverage/github/ngmy/Laravel.Aop?style=flat-square)](https://coveralls.io/github/ngmy/Laravel.Aop)\n[![Total Downloads](https://img.shields.io/packagist/dt/ngmy/laravel.aop.svg?style=flat-square)](https://packagist.org/packages/ngmy/laravel.aop)\n\nLaravel.Aop integrates Ray.Aop with Laravel. It provides fast AOP by static weaving.\n\n## Installation\n\nFirst, you should install Laravel.Aop via the Composer package manager:\n\n```bash\ncomposer require ngmy/laravel.aop\n```\n\nYou will be asked if you trust the `olvlvl/composer-attribute-collector` package, so you should press `y`.\n\nNext, you should configure the `olvlvl/composer-attribute-collector` package.\n\n\u003e [!TIP]\n\u003e Please see the [composer-attribute-collector documentation](https://github.com/olvlvl/composer-attribute-collector)\n\u003e to learn how to configure the `olvlvl/composer-attribute-collector` package.\n\nThen, you should publish the Laravel.Aop configuration file using the `vendor:publish` Artisan command. This command\nwill publish the `aop.php` configuration file to your application's `config` directory:\n\n```bash\nphp artisan vendor:publish --provider=\"Ngmy\\LaravelAop\\ServiceProvider\"\n```\n\n## Usage\n\nFirst, you should define the attribute.\nFor example, let's define the `Transactional` attribute:\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Attributes;\n\n#[\\Attribute(\\Attribute::TARGET_METHOD)]\nclass Transactional {}\n```\n\nNext, you should define the interceptor.\nFor example, let's define the `TransactionalInterceptor` interceptor:\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Interceptors;\n\nuse Illuminate\\Support\\Facades\\DB;\nuse Ray\\Aop\\MethodInterceptor;\nuse Ray\\Aop\\MethodInvocation;\n\nclass Transactional implements MethodInterceptor\n{\n    public function invoke(MethodInvocation $invocation): mixed\n    {\n        return DB::transaction(static fn (): mixed =\u003e $invocation-\u003eproceed());\n    }\n}\n```\n\n\u003e [!TIP]\n\u003e Please see the [Ray.Aop documentation](https://github.com/ray-di/Ray.Aop) to learn more about the interceptor.\n\nThen, you should register the attribute and the interceptor in the `intercept` configuration option of the\n`config/aop.php` configuration file.\nFor example, let's register the `Transactional` attribute and the `TransactionalInterceptor` interceptor:\n\n```php\nuse App\\Attributes\\Transactional;\nuse App\\Interceptors\\TransactionalInterceptor;\nuse Ngmy\\LaravelAop\\Collections\\InterceptMap;\n\n'intercept' =\u003e InterceptMap::default()-\u003emerge([\n    Transactional::class =\u003e [\n        TransactionalInterceptor::class,\n    ],\n])-\u003etoArray(),\n```\n\nThen, you should annotate the methods that you want to intercept with the attribute.\nFor example, let's annotate the `createUser` method of the `UserService` class with the `Transactional` attribute:\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace App\\Services;\n\nuse App\\Attributes\\Transactional;\nuse App\\Models\\User;\n\nclass UserService\n{\n    #[Transactional]\n    public function createUser(string $name): void\n    {\n        User::create(['name' =\u003e $name]);\n    }\n}\n```\n\nFinally, you should run the `aop:compile` Artisan command to compile the AOP classes:\n\n```bash\nphp artisan aop:compile\n```\n\nYou must run the `dump-autoload` Composer command before running the `aop:compile` Artisan command.\nBy default, the `aop:compile` Artisan command runs the `dump-autoload` Composer command with the `--no-scripts` option\ninternally before compiling the AOP classes.\nIf you want to run the `dump-autoload` Composer command yourself, you should run the `aop:compile` Artisan\ncommand with the `--no-dump-autoload` option:\n\n```bash\ncomposer dump-autoload\nphp artisan aop:compile --no-dump-autoload\n```\n\n\u003e [!WARNING]\n\u003e If you want to avoid overwriting the Composer autoload files, such as when optimizing the Composer autoload files in\n\u003e a production environment, you should run the `aop:compile` Artisan command with the `--no-dump-autoload` option.\n\n\u003e [!IMPORTANT]\n\u003e After modifying the `intercept` configuration option or the method attributes, you should recompile the AOP classes.\n\u003e However, if you only modify the arguments of the method attributes, you do not need to recompile the AOP classes.\n\nNow, the methods annotated with the attribute will be intercepted by the interceptor.\nIn this example, the `createUser` method of the `UserService` class will be intercepted by the\n`TransactionalInterceptor` and will be executed in a transaction.\n\n\u003e [!IMPORTANT]\n\u003e The methods annotated with the attribute are intercepted by the interceptor only when the class instance is\n\u003e dependency resolved from the service container. If the class instance is created directly, the methods are not\n\u003e intercepted.\n\n## Watcher\n\nLaravel.Aop provides a watcher that watches the changes of the files and recompiles the AOP classes automatically.\nThis is useful when you are developing the application.\nYou may start the watcher using the `aop:watch` Artisan command:\n\n```bash\nphp artisan aop:watch\n```\n\n\u003e [!NOTE]\n\u003e The watcher runs the `dump-autoload` Composer command with the `--no-scripts` option internally before recompiling the\n\u003e AOP classes.\n\nYou may configure the files to watch in the `watcher.paths` configuration option of the `config/aop.php` configuration\nfile.\n\n## Tips\n\n### Auto compilation\n\nYou may easily compile the AOP classes automatically after the `dump-autoload` Composer command is run by adding the\n`@php artisan aop:compile --no-dump-autoload --ansi` script to the `post-autoload-dump` event hook of the\n`composer.json` file:\n\n```json\n{\n    \"scripts\": {\n        \"post-autoload-dump\": [\n            \"Illuminate\\\\Foundation\\\\ComposerScripts::postAutoloadDump\",\n            \"@php artisan package:discover --ansi\",\n            \"@php artisan aop:compile --no-dump-autoload --ansi\"\n        ]\n    }\n}\n```\n\n## Built-in attributes\n\n- [Cache](https://ngmy.github.io/Laravel.Aop/namespaces/ngmy-laravelaop-aspects-cache-attributes.html)\n- [Logging](https://ngmy.github.io/Laravel.Aop/namespaces/ngmy-laravelaop-aspects-logging-attributes.html)\n- [Transaction](https://ngmy.github.io/Laravel.Aop/namespaces/ngmy-laravelaop-aspects-transaction-attributes.html)\n- [Retry](https://ngmy.github.io/Laravel.Aop/namespaces/ngmy-laravelaop-aspects-retry-attributes.html)\n\n## Changelog\n\nPlease see the [changelog](CHANGELOG.md).\n\n## License\n\nLaravel.Aop is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fngmy%2Flaravel.aop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fngmy%2Flaravel.aop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fngmy%2Flaravel.aop/lists"}