Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ngmy committed Apr 7, 2024
0 parents commit 954db66
Show file tree
Hide file tree
Showing 39 changed files with 1,417 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
APP_UID=1000
APP_GID=1000
16 changes: 16 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.env.example export-ignore
/.gitattributes export-ignore
/.github/ export-ignore
/.gitignore export-ignore
/.php-cs-fixer.dist.php export-ignore
/CHANGELOG.md export-ignore
/docker/ export-ignore
/docker-compose.yml export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/tests/ export-ignore
/vendor-bin/ export-ignore
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github: ngmy
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: https://flattr.com/@ngmy
50 changes: 50 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Lint

on:
push:
pull_request:

jobs:
lint:
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
php:
- '8.1'
- '8.2'
- '8.3'
laravel:
- 9
- 10
- 11
exclude:
- php: '8.1'
laravel: 11
name: PHP ${{ matrix.php }} + Laravel ${{ matrix.laravel }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up PHP ${{ matrix.php }}
run: sudo update-alternatives --set php /usr/bin/php${{ matrix.php }}

- name: Update Composer to latest version
run: sudo composer self-update

- name: Validate composer.json
run: composer validate

- name: Install Composer dependencies
run: |
composer install --no-interaction
if [[ "${{ matrix.laravel }}" == 9 ]]; then
composer update --no-interaction orchestra/testbench:^7.0 --with-all-dependencies
elif [[ "${{ matrix.laravel }}" == 10 ]]; then
composer update --no-interaction orchestra/testbench:^8.0 --with-all-dependencies
elif [[ "${{ matrix.laravel }}" == 11 ]]; then
composer update --no-interaction orchestra/testbench:^9.0 --with-all-dependencies
fi
- name: Run lint
run: composer lint
65 changes: 65 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: Test

on:
push:
pull_request:

jobs:
test:
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
php:
- '8.1'
- '8.2'
- '8.3'
laravel:
- 9
- 10
- 11
exclude:
- php: '8.1'
laravel: 11
name: PHP ${{ matrix.php }} + Laravel ${{ matrix.laravel }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up PHP ${{ matrix.php }}
run: sudo update-alternatives --set php /usr/bin/php${{ matrix.php }}

- name: Update Composer to latest version
run: sudo composer self-update

- name: Validate composer.json
run: composer validate

- name: Install Composer dependencies
run: |
composer install --no-interaction
if [[ "${{ matrix.laravel }}" == 9 ]]; then
composer update --no-interaction orchestra/testbench:^7.0 --with-all-dependencies
elif [[ "${{ matrix.laravel }}" == 10 ]]; then
composer update --no-interaction orchestra/testbench:^8.0 --with-all-dependencies
elif [[ "${{ matrix.laravel }}" == 11 ]]; then
composer update --no-interaction orchestra/testbench:^9.0 --with-all-dependencies
fi
- name: Run tests
run: |
if [[ "${{ matrix.php }}" == '8.1' && "${{ matrix.laravel }}" == 10 ]]; then
composer test-coverage
else
composer test
fi
- name: Upload coverage results to Coveralls
if: matrix.php == '8.1' && matrix.laravel == 10
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
uses: nick-fields/retry@v3
with:
timeout_minutes: 10
max_attempts: 3
command: vendor-bin/php-coveralls/vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Dotenv
/.env

# Composer
/composer.lock
/vendor/
/vendor-bin/**/composer.lock
/vendor-bin/**/vendor/

# Build
/build/

# PHPUnit
/.phpunit.cache/
/phpunit.xml

# PHPStan
/phpstan.neon

# PHP CS Fixer
/.php-cs-fixer.php
/.php-cs-fixer.cache
21 changes: 21 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
;

$config = new PhpCsFixer\Config();

return $config->setRules([
'@PHP80Migration:risky' => true,
'@PHP81Migration' => true,
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' => true,
'@PHPUnit100Migration:risky' => true,
/** @link https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/4157 */
'return_assignment' => false,
])
->setFinder($finder)
;
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Release Notes

## 0.1.0 - 2024-04-09

- Initial release.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Yuta Nagamiya

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
154 changes: 154 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# Laravel.Aop

[![Latest Stable Version](https://img.shields.io/packagist/v/ngmy/laravel.aop.svg?style=flat-square&label=stable)](https://packagist.org/packages/ngmy/laravel.aop)
[![Test Status](https://img.shields.io/github/actions/workflow/status/ngmy/laravel.aop/test.yml?style=flat-square&label=test)](https://github.com/ngmy/Laravel.aop/actions/workflows/test.yml)
[![Lint Status](https://img.shields.io/github/actions/workflow/status/ngmy/laravel.aop/lint.yml?style=flat-square&label=lint)](https://github.com/ngmy/Laravel.aop/actions/workflows/lint.yml)
[![Code Coverage](https://img.shields.io/coverallsCoverage/github/ngmy/Laravel.Aop?style=flat-square)](https://coveralls.io/github/ngmy/Laravel.Aop)
[![Total Downloads](https://img.shields.io/packagist/dt/ngmy/laravel.aop.svg?style=flat-square)](https://packagist.org/packages/ngmy/laravel.aop)

Laravel.Aop integrates Ray.Aop with Laravel. It provides fast AOP by static weaving.

## Installation

First, you should install Laravel.Aop via the Composer package manager:

```bash
composer require ngmy/laravel.aop
```

You will be asked if you trust the `olvlvl/composer-attribute-collector` package, so you should press `y`.

Next, you should configure the `olvlvl/composer-attribute-collector` package.

> [!TIP]
> Please see the [composer-attribute-collector documentation](https://github.com/olvlvl/composer-attribute-collector)
> to learn how to configure the `olvlvl/composer-attribute-collector` package.
Then, you should publish the Laravel.Aop configuration file using the `vendor:publish` Artisan command. This command
will publish the `aop.php` configuration file to your application's `config` directory:

```bash
php artisan vendor:publish --provider="Ngmy\LaravelAop\ServiceProvider"
```

Finally, you should add the `@php artisan aop:compile --ansi` script to the `post-autoload-dump` event hook of the
`composer.json` file:

```json
{
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi",
"@php artisan aop:compile --ansi"
]
}
}
```

## Usage

First, you should define the attribute.
For example, let's define the `Transactional` attribute:

```php
<?php

declare(strict_types=1);

namespace App\Attributes;

#[\Attribute(\Attribute::TARGET_METHOD)]
class Transactional {}
```

Next, you should define the interceptor.
For example, let's define the `TransactionalInterceptor` interceptor:

```php
<?php

declare(strict_types=1);

namespace App\Interceptors;

use Illuminate\Support\Facades\DB;
use Ray\Aop\MethodInterceptor;
use Ray\Aop\MethodInvocation;

class Transactional implements MethodInterceptor
{
public function invoke(MethodInvocation $invocation): mixed
{
return DB::transaction(static fn (): mixed => $invocation->proceed());
}
}
```

> [!TIP]
> Please see the [Ray.Aop documentation](https://github.com/ray-di/Ray.Aop?tab=readme-ov-file#interceptor) to learn more
> about the interceptor.
Then, you should register the attribute and the interceptor in the `intercept` configuration option of the
`config/aop.php` configuration file.
For example, let's register the `Transactional` attribute and the `TransactionalInterceptor` interceptor:

```php
use App\Attributes\Transactional;
use App\Interceptors\TransactionalInterceptor;

'intercept' => [
Transactional::class => [
TransactionalInterceptor::class,
],
],
```

Then, you should annotate the methods that you want to intercept with the attribute.
For example, let's annotate the `createUser` method of the `UserService` class with the `Transactional` attribute:

```php
<?php

declare(strict_types=1);

namespace App\Services;

use App\Attributes\Transactional;
use App\Models\User;

class UserService
{
#[Transactional]
public function createUser(string $name): void
{
User::create(['name' => $name]);
}
}
```

Finally, you should run the `dump-autoload` Composer command to compile the AOP classes:

```bash
composer dump-autoload
```

> [!IMPORTANT]
> After changing the `intercept` configuration option or changing the annotation of the methods, you should compile
> the AOP classes again.
Now, the methods annotated with the attribute will be intercepted by the interceptor.
In this example, the `createUser` method of the `UserService` class will be intercepted by the
`TransactionalInterceptor` and will be executed in a transaction.

> [!IMPORTANT]
> The methods annotated with the attribute are intercepted by the interceptor only when the class instance is
> dependency resolved from the service container. If a class instance is created directly, it is not intercepted.
## Changelog

Please see the [changelog](CHANGELOG.md).

## License

Laravel.Aop is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).
Loading

0 comments on commit 954db66

Please sign in to comment.