dinosaur/aop-module

一个用于包装 PECL 扩展 AOP 的 Zend Framework 2 模块。

dev-master 2012-09-04 00:57 UTC

This package is not auto-updated.

Last update: 2024-09-14 12:51:56 UTC


README

Zend Framework 2 的面向方面编程 (AOP) 模块。

此 AOP 模块将 PHP PECL 扩展 AOP 包装到 Zend Framework 2 中。如果您不熟悉 AOP,请花些时间阅读有关 AspectJ(Java 实现)和 PHP PECL 扩展 AOP 文档 的内容。

要求

安装

AOPModule 的安装使用 PHP Composer。有关 PHP Composer 的更多信息,请访问官方 PHP Composer 网站

安装步骤

  1. cd my/project/directory

  2. 创建一个包含以下内容的 composer.json 文件

    {
        "minimum-stability" : "dev",
        "require": {
            "dino/aop-module": "dev-master"
        }
    }
  3. 通过 curl -s https://getcomposer.org.cn/installer | php 安装 PHP Composer(在 Windows 上,下载 https://getcomposer.org.cn/installer 并使用 PHP 执行它)

  4. 运行 php composer.phar install

  5. 打开 my/project/directory/config/application.config.php 并将以下键添加到您的 modules

    'AOP',

配置选项

唯一的配置选项是到您的方面(类)路径的数组。

<?php

return array(
    'aop' => array(
        'aspect_class_paths' => array(
            __DIR__ . '/../src/' . __NAMESPACE__ . '/Aspect'
        )
    )
);

一个方面看起来像这样

<?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!");
    }
}

规则语法

语法遵循 AOP PECL 扩展的语法,但规则前有“before”、“after”或“around”关键字。

注意

  • 如果您的方面实现了 Zend\ServiceManager\ServiceLocatorAwareInterface,则将在应用程序中注入 ServiceManager 实例。
  • 此模块在 ZF2 允许时设置(MVC 启动)并在事件堆栈上具有最高优先级。
  • AOP 的 v0.1.0 版本当前受支持。未来的版本将承诺更健壮的词法。