coolephp/goaop

Bringing the goaop to Coole. - 将 goaop 集成到 Coole。

Fund package maintenance!
Wechat

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

This package is auto-updated.

Last update: 2024-11-08 16:07:44 UTC


README

Bringing the goaop to Coole. - 将 goaop 集成到 Coole。

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

Requirement

  • Coole >= 1.0

Installation

$ composer require coolephp/goaop -vvv

Usage

Configuration

  1. Copy goaop/config/goaop.php to coole-skeleton/config/goaop.php.
  2. Config \Coole\Goaop\GoAopServiceProvider::class service provider.
<?php

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

    /*
     * 第三方服务
     */
    'providers' => [
        \Coole\Goaop\GoAopServiceProvider::class
    ],
    
    ...
];
  1. Add a aspect configuration for config/goaop.php.
<?php

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

Create a class app\Service\LoggingService

<?php

namespace App\Service;

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

Create a aspect 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);
    }
}

Run App\Service\LoggingService::logging()

cat runtime/logging.log

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

Testing

$ composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.