goaop/goaop-laravel-bridge

go-aop laravel bridge not working.

Closed this issue · 8 comments

config/app.php

    'providers' => [

        // Go! Aspect Service Provider
        Go\Laravel\GoAopBridge\GoAopServiceProvider::class,

        // AOP Service Provider
        \App\Providers\AopServiceProvider::class,

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,

Logging Aspects

<?php

namespace App\Aspects;

use Go\Aop\Aspect;
use Go\Aop\Intercept\MethodInvocation;
use Go\Lang\Annotation\Before;
use Illuminate\Support\Facades\Log;
use Psr\Log\LoggerInterface;

/**
 * Application logging aspect
 */
class LoggingAspect implements Aspect
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        Log::info('Logging aspect initialized....');
        $this->logger = $logger;
    }

    /**
     * Writes a log info before method execution
     *
     * @param MethodInvocation $invocation
     * @Before("execution(public **->*(*))")
     */
    public function beforeMethod(MethodInvocation $invocation)
    {
        Log::info('Logging before method invocation..');
        //$this->logger->info($invocation, $invocation->getArguments());
    }
}

ServieProvider

<?php

namespace App\Providers;

use App\Aspects\LoggingAspect;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Psr\Log\LoggerInterface;

class AopServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton(LoggingAspect::class, function (Application $app) {
            return new LoggingAspect($app->make(LoggerInterface::class));
        });

        $this->app->tag([LoggingAspect::class], 'goaop.aspect');
    }
}

Config file is default config for go_aop.
Version: "goaop/goaop-laravel-bridge": "^1.0"
PHP: PHP 7.0.10 (cli) (built: Aug 18 2016 09:48:53) ( ZTS )

and i can see in the laravel.log
[2017-03-21 16:12:52] local.INFO: Logging aspect initialized....
But the code in beforeMethod is not working.

Thanks

Hi, have you configured go_aop config in your application?

@lisachenko is there any other configuration other than you have mentioned on repo home page ?

Thanks

@rummykhan no, all options are documented. If AOP isn't working, just check these parameters:

  1. appDir should point to your application root directory, if any class won't be under this directory, then it won't be processed at all.
  2. cacheDir is required for correct work and optimization, please specify any valid directory with write rights
  3. Also check your includePaths and excludePaths, they are whitelist and blacklist of directories for processing. By default, only files from the app directory are processed by the AOP engine, if you want more classes, please adjust as needed.

If you will get an error about classes not found then it means that you should exclude AOP engine directories from the processing by adding them to the excludePaths option. (AOP tries to intercept own classes and it's not ok)

Ping, anything new?

Hi, I had the same problem, just want to share my solution:
As @lisachenko mentioned, the problem will most likely be in the config file.

My project structure is non-standard, so isntead of a laravel/app folder I have a laravel/api folder.

This is what my config file looks like (other values are default):
Note: app_path() will always point to laravel/app. It seems that there's no way to change this. So I used base_path() instead.

return [
    'appDir' => base_path(),
    'includePaths' => [
       base_path() . '/api'
    ],
    'excludePaths' => [
        base_path() . '/vendor/'
    ],
];

Closing by inactivity, probable it's related to the wrong configuration.

I'm sorry for long gone. I was not able to figure out things, But I solved the problem I was having differently due to app already in production. And I also think this was a problem due to wrong configuration.

@rummykhan great to known that everything is ok for you! Have a nice day!