PHP code example of sotvokun / webman-aop

1. Go to this page and download the library: Download sotvokun/webman-aop library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sotvokun / webman-aop example snippets




return [
    'enable' => true,
    'class_path' => runtime_path('aop'),
    'scan_dirs' => [
        ...glob(base_path() . '/module/*/service'),
        ...glob(base_path() . '/module/*/query'),
    ],
];



use Sotvokun\Webman\Aop\Container;

return new Container();



namespace module\order\aspect;

use Attribute;
use module\order\interceptor\ExampleInterceptor;
use Sotvokun\Webman\Aop\Aspect;

#[Attribute(Attribute::TARGET_METHOD)]
final class ExampleAspect extends Aspect
{
    public static function interceptors(): array
    {
        return [ExampleInterceptor::class];
    }
}



namespace module\order\interceptor;

use Sotvokun\Webman\Aop\MethodInterceptor;
use Ray\Aop\MethodInvocation;

final class ExampleInterceptor implements MethodInterceptor
{
    public function invoke(MethodInvocation $invocation): mixed
    {
        // Before invoking the target method.
        $result = $invocation->proceed();
        // After invoking the target method.

        return $result;
    }
}



namespace module\order\service;

use module\order\aspect\ExampleAspect;

class OrderService
{
    #[ExampleAspect]
    public function create(array $input): void
    {
        // Business logic.
    }
}

final class OrderController
{
    public function __construct(private readonly OrderService $orderService)
    {
    }
}

final class ExampleInterceptor implements MethodInterceptor
{
    public function __construct(private readonly ConnectionInterface $connection)
    {
    }

    public function invoke(MethodInvocation $invocation): mixed
    {
        return $invocation->proceed();
    }
}

use Sotvokun\Webman\Aop\Attribute\Lazy;

final class ReportController
{
    public function __construct(#[Lazy] private ReportService $reports)
    {
    }
}

use module\order\aspect\ExampleAspect;

final class ReportController
{
    public function __construct(#[Lazy] private ReportService $reports)
    {
    }

    #[ExampleAspect]
    public function show(): array
    {
        return $this->reports->latest();
    }
}

use module\order\aspect\ExampleAspect;

final class ReportService
{
    public function __construct(private ReportClient $client)
    {
    }

    #[ExampleAspect]
    public function latest(): array
    {
        // ...
    }
}