coolephp/goaop

将 goaop 集成到 Coole。 - 将 goaop 集成到 Coole。

资助包维护!
微信

v1.0.0 2021-01-08 07:47 UTC

This package is auto-updated.

Last update: 2024-09-08 15:44:05 UTC


README

将 goaop 集成到 Coole。 - 将 goaop 集成到 Coole。

Tests Check & fix styling codecov Latest Stable Version Total Downloads License

要求

  • Coole >= 1.0

安装

$ composer require coolephp/goaop -vvv

使用

配置

  1. goaop/config/goaop.php 复制到 coole-skeleton/config/goaop.php
  2. 配置服务提供者 \Coole\Goaop\GoAopServiceProvider::class
<?php

return [
    /*
     * App 名称
     */
    'name' => env('APP_NAME', 'Coole'),
    
    ...

    /*
     * 第三方服务
     */
    'providers' => [
        \Coole\Goaop\GoAopServiceProvider::class
    ],
    
    ...
];
  1. config/goaop.php 添加方面配置。
<?php

return [
    /*
     * AOP Debug Mode
     */
    'debug' => env('GOAOP_DEBUG', env('APP_DEBUG', false)),
    
    ...
    
    /*
     * Yours aspects
     */
    'aspects' => [
        \App\Aspect\LoggingServiceAspect::class,
    ],
];

创建一个类 app\Service\LoggingService

<?php

namespace App\Service;

class LoggingService
{
    public static function logging()
    {
        return true;
    }
}

创建一个方面 App\Aspect\LoggingServiceAspect

<?php

namespace App\Aspect;

use Go\Aop\Aspect;
use Go\Aop\Intercept\MethodInvocation;
use Go\Lang\Annotation\After;
use Go\Lang\Annotation\Before;

class LoggingServiceAspect implements Aspect
{
    /**
     * Method that will be called before real method.
     *
     * @param MethodInvocation $invocation Invocation
     * @Before("execution(public App\Service\LoggingService::logging(*))")
     */
    public function beforeMethodExecution(MethodInvocation $invocation)
    {
        file_put_contents(base_path('runtime/logging.log'), 'this is a before method testing.'.PHP_EOL, FILE_APPEND);
    }

    /**
     * Method that will be called after real method.
     *
     * @param MethodInvocation $invocation Invocation
     * @After("execution(public App\Service\LoggingService::logging(*))")
     */
    public function afterMethodExecution(MethodInvocation $invocation)
    {
        file_put_contents(base_path('runtime/logging.log'), 'this is a after method testing.'.PHP_EOL, FILE_APPEND);
    }
}

运行 App\Service\LoggingService::logging()

查看 runtime/logging.log

───────┬───────────────────────────────────────────────────────────────────
       │ File: runtime/logging.log
───────┼───────────────────────────────────────────────────────────────────
   1   │ this is a before method testing.
   2   │ this is a after method testing.
───────┴───────────────────────────────────────────────────────────────────

测试

$ composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献指南

安全漏洞

有关如何报告安全漏洞的详细信息,请参阅我们的安全策略

致谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅许可证文件