Welcome

This demo shows your an examples of AOP usage.

Please, choose one of available examples from navigation menu.
You can also try to run this code with XDebug.

Calling Before Interceptor for execution(Demo\Example\LoggingDemo->execute()) with arguments: ["LoggingTask"]
Calling Before Interceptor for execution(Demo\Example\LoggingDemo->perform()) with arguments: ["LoggingTask","first"]
Calling Before Interceptor for execution(Demo\Example\LoggingDemo->perform()) with arguments: ["LoggingTask","second"]
Calling Before Interceptor for execution(Demo\Example\LoggingDemo::runByName()) with arguments: ["StaticTask"]
Calling Before Interceptor for execution(Demo\Example\LoggingDemo->execute()) with arguments: ["StaticTask"]
Calling Before Interceptor for execution(Demo\Example\LoggingDemo->perform()) with arguments: ["StaticTask","first"]
Calling Before Interceptor for execution(Demo\Example\LoggingDemo->perform()) with arguments: ["StaticTask","second"]
  
<?php
/*
 * Go! AOP framework
 *
 * @copyright Copyright 2014, Lisachenko Alexander <lisachenko.it@gmail.com>
 *
 * This source file is subject to the license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Demo\Aspect;

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

/**
 * Logging aspect
 *
 * @see http://go.aopphp.com/blog/2013/07/21/implementing-logging-aspect-with-doctrine-annotations/
 */
class LoggingAspect implements Aspect
{

    
/**
     * This advice intercepts an execution of loggable methods
     *
     * We use "Before" type of advice to log only class name, method name and arguments before
     * method execution.
     * You can choose your own logger, for example, monolog or log4php.
     * Also you can choose "After" or "Around" advice to access an return value from method.
     *
     * To inject logger into this aspect you can look at Warlock framework with DI+AOP
     *
     * @param MethodInvocation $invocation Invocation
     *
     * @Before("@execution(Demo\Annotation\Loggable)")
     */
    
public function beforeMethodExecution(MethodInvocation $invocation)
    {
        echo 
'Calling Before Interceptor for ',
             
$invocation,
             
' with arguments: ',
             
json_encode($invocation->getArguments()),
             
PHP_EOL;
    }
}
<?php
/*
 * Go! AOP framework
 *
 * @copyright Copyright 2014, Lisachenko Alexander <lisachenko.it@gmail.com>
 *
 * This source file is subject to the license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Demo\Example;

use 
Demo\Annotation\Loggable;

/**
 * Example class to show how to use logging with AOP
 */
class LoggingDemo
{

    
/**
     * Executes a task and logs all incoming arguments
     *
     * @Loggable
     * @param mixed $task Some specific argument
     */
    
public function execute($task)
    {
        
$this->perform($task'first');
        
$this->perform($task'second');
    }

    
/**
     * Protected method can be also loggable
     *
     * @Loggable
     *
     * @param mixed $task Specific task
     * @param string $level
     */
    
protected function perform($task$level)
    {
        
// some logic here
    
}

    
/**
     * Everything is possible with AOP, so static methods can be intercepted too
     *
     * @Loggable
     *
     * @param string $task Some specific argument
     */
    
public static function runByName($task)
    {
        
$instance = new static(); // Go! AOP requires LSB to work correctly
        
$instance->execute($task);
    }
}