Aspect-Oriented programming becomes more popular for PHP, but it requires good knowledge of OOP and can be very cumbersome for beginners. So it’s important to have a working examples and manuals to have a possibility to run it locally with favorite framework. Go! AOP provides all the necessary functionality for configuration of AOP into any frameworks, but integration process can be very tricky, so configuration is not so easy. In this article we will configure a working example for Yii framework.
Let’s start! First of all, we need an empty yii-aspect
directory for our project:
1
|
|
Next step is to install dependencies: Yii and Go! AOP frameworks. Go! AOP is registered on packagist and can be easily installed with composer:
1
|
|
Latest version of Yii framework (1.1.14) is on packagist too! This means that we can install it without any hacks with local repositories:
1
|
|
This is a minimum list of dependencies for our first example. Following step is very easy, thanks to the yiic
console command. By default, all binaries are installed into ./vendor/bin
folder, so yiic
console should be there too.
Let’s create a web application:
1
|
|
Yii will generate a directory app
with default project structure and files. This project can be opened in the browser, but AOP isn’t enabled right now. To enable AOP we should prepare our application to have an ability to use it. To enable AOP we need to update the front controller of our application (./app/index.php
) in the following way:
Add this lines to the top of file before original content:
1 2 3 4 5 6 7 8 |
|
And replace initialization of Yii at the bottom of file from require_once($yii)
to require_once(FilterInjectorTransformer::rewrite($yii))
. This is needed to give a hook for the Go! AOP framework to weave aspects into classes. Aspects are defined as separated classes and included in the ./aspect.php
file. Let’s move to it and to the aspect kernel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
This is typical configuration of Go! AOP framework where we can adjust some directories and paths. I think that this config is pretty clear to understand. Only debug
parameter is really important. For production mode it should be false
, but for development mode it should be enabled to enable better debugging and cache refreshing.
In this file we also include a strange file ApplicationAspectKernel.php
. This file contains definition of aspect kernel for our application and it’s very simple:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
There is only one method to define: configureAop
that is used for AOP configuration. We can create an aspect now and register it in the kernel:
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 |
|
… and registration in the kernel:
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 |
|
That’s all )
Just have recreated https://t.co/FqlECETHO7 to use latest #yii 1.1.x and Go! AOP with #composer /cc @afdiaz @sam_dark
— Alexander Lisachenko (@lisachenko) September 26, 2013
Just refresh the page in the browser to see a result. All methods will be intercepted by our advice beforeMethodExecution
:
PS. If you want to create an empty project with single line you can run:
1
|
|