An Aspect Oriented Programming (AOP) Module for Zend Framework 2.
This AOP module wraps the PHP PECL extension AOP into Zend Framework 2. If you're not familiar with AOP, take some time to read up on AspectJ (the Java implementation) and the PHP PECL extension AOP documentation.
Installation of AOPModule uses PHP Composer. For more information about PHP Composer, please visit the official PHP Composer site.
-
cd my/project/directory
-
create a
composer.json
file with following contents:{ "minimum-stability" : "dev", "require": { "dino/aop-module": "dev-master" } }
-
install PHP Composer via
curl -s http://getcomposer.org/installer | php
(on windows, download http://getcomposer.org/installer and execute it with PHP) -
run
php composer.phar install
-
open
my/project/directory/config/application.config.php
and add the following key to yourmodules
:'AOP',
The only configuration option is an array of paths to where your aspects (classes) are.
<?php
return array(
'aop' => array(
'aspect_class_paths' => array(
__DIR__ . '/../src/' . __NAMESPACE__ . '/Aspect'
)
)
);
An aspect looks like this:
<?php
namespace Application\Aspect;
use AOP\Annotation\Pointcut;
class Security
{
/**
* The pointcut rule can be a standalone rule or an array of rules,
* denoted by the curly braces.
*
* @Pointcut(rule={
* "before Application\Controller\IndexController->*Action()",
* "before Application\Controller\AdminController->*Action()"
* })
*/
public function checkActionPrecondition(\AOPTriggeredJoinPoint $triggeredJoinPoint)
{
error_log("Check Access Precondition!");
}
/**
* Take note that the rule is not in array notation.
*
* @Pointcut(rule="before Application\Controller\IndexController->*Action()")
*/
public function checkFooBarPrecondition(\AOPTriggeredJoinPoint $triggeredJoinPoint)
{
error_log("Check Foo Bar Precondition!");
}
/**
* @Pointcut(rule="after Application\Controller\IndexController->*Action()")
*/
public function logActionDispatched(\AOPTriggeredJoinPoint $triggeredJoinPoint)
{
/**
* If ServiceLocatorAwareInterface was implemented, we could call:
*
* $this->getServiceLocator()
* ->get('logger')
* ->info('We dispatched an action.');
*/
error_log("My logging advice!");
}
}
The syntax follows that of the AOP PECL extension with the exception of the prepended "before", "after", or "around" keywords to the rule.
- If your aspect implements
Zend\ServiceManager\ServiceLocatorAwareInterface
, the ServiceManager instance on Application will be injected. - This module is set up as soon as ZF2 allows (MVC Bootstrap) and at the highest priority on the event stack.
- v0.1.0 of AOP is currently supported. Future releases promise a more robust lexicon.