Go! Aspect-Oriented Framework

Aspect-Oriented Programming in PHP.

Fork me on GitHub

Initial Configuration

Initial configuration consists of several steps:

  1. Create an application aspect kernel
  2. Configure the aspect kernel in the front controller
  3. Create an aspect
  4. Register the aspect in the aspect kernel

Step 1: Create an application aspect kernel

The aim of this library is to provide easy AOP integration to your application. Your first step then is to create the AspectKernel class for your application. This class will manage all aspects of your application in one place.

The library provides base class to make it easier to create your own kernel. To create your application kernel extend the abstract class Go\Core\AspectKernel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
// app/ApplicationAspectKernel.php

use Go\Core\AspectKernel;
use Go\Core\AspectContainer;

/**
 * Application Aspect Kernel
 */
class ApplicationAspectKernel extends AspectKernel
{

    /**
     * Configure an AspectContainer with advisors, aspects and pointcuts
     *
     * @param AspectContainer $container
     *
     * @return void
     */
    protected function configureAop(AspectContainer $container)
    {
    }
}

2. Configure the aspect kernel in the front controller

To configure the aspect kernel, call init() method of kernel instance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
// front-controller, for Symfony2 application it's web/app_dev.php

// Initialize an application aspect container
$applicationAspectKernel = ApplicationAspectKernel::getInstance();
$applicationAspectKernel->init(array(
        'debug' => true, // Use 'false' for production mode
        // Cache directory
        'cacheDir' => __DIR__ . './../cache/', // Adjust this path if needed
        // Include paths restricts the directories where aspects should be applied, or empty for all source files
        'includePaths' => array(
            __DIR__ . '/../src/'
        )
));

3. Create an aspect

Aspect is the key element of AOP philosophy. And Go! library just uses simple PHP classes for declaring aspects! Therefore it’s possible to use all features of OOP for aspect classes. As an example let’s intercept all the methods and display their names:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
// Aspect/MonitorAspect.php

namespace Aspect;

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

/**
 * Monitor aspect
 */
class MonitorAspect implements Aspect
{

    /**
     * Method that will be called before real method
     *
     * @param MethodInvocation $invocation Invocation
     * @Before("execution(public Example->*(*))")
     */
    public function beforeMethodExecution(MethodInvocation $invocation)
    {
        $obj = $invocation->getThis();
        echo 'Calling Before Interceptor for method: ',
             is_object($obj) ? get_class($obj) : $obj,
             $invocation->getMethod()->isStatic() ? '::' : '->',
             $invocation->getMethod()->getName(),
             '()',
             ' with arguments: ',
             json_encode($invocation->getArguments()),
             "<br>\n";
    }
}

Easy, isn’t it? We declared here that we want to install a hook before the execution of all dynamic public methods in the class Example. This is done with the help of annotation @Before("execution(public Example->*(*))") Hooks can be of any types, you will see them later. But we doesn’t change any code in the class Example! I can feel you astonishment now )

4. Register the aspect in the aspect kernel

To register the aspect just add an instance of it in the configureAop() method of the kernel:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// app/ApplicationAspectKernel.php

use Aspect/MonitorAspect;

//...

    protected function configureAop(AspectContainer $container)
    {
        $container->registerAspect(new MonitorAspect());
    }

//...

Now you are ready to use the power of aspects! Feel free to change anything everywhere.

Comments