goaop/goaop-zend-expressive-middleware

一个用于加载 goaop 面向切面编程(AOP)功能的 Zend Expressive 中间件。

3.0.0 2018-03-27 11:12 UTC

README

Build Status

GoAop Expressive 中间件

GoAop 中间件通过 Go! AOP 框架为 Zend Expressive 应用程序添加了面向切面编程(AOP)的支持。

概览

面向切面范式允许通过特殊工具扩展标准面向对象范式,以有效地解决应用程序中的横切关注点。此类代码通常存在于应用程序的各个地方(例如,日志记录、缓存、监控等),没有 AOP 就无法轻易修复这些问题。

AOP 为开发者定义了新的工具,包括:

  • Joinpoint - 代码中可以用于拦截的位置,例如执行单个公共方法或访问单个对象的属性。
  • Pointcut 是一个与特殊正则表达式一起定义的 joinpoint 列表,用于您的源代码,例如具体类或命名空间中的所有公共和受保护方法。
  • Advice 是一个附加的回调,它将在具体 joinpoint 之前、之后或周围调用。对于 PHP,每个 advice 都表示为一个 \Closure 实例,封装在拦截器对象中。
  • Aspect 是一个特殊类,它将 pointcut 和 advice 结合在一起,每个 pointcut 都定义为注释,每个 advice 都是该 aspect 中的方法。

您可以在不同的来源中了解更多关于 AOP 的信息,Java 语言的优秀文章也可以应用于 PHP,因为它是一种通用范式。

安装

可以使用 composer 轻松安装 GoAopModule。只需运行以下命令请求 composer 下载带有依赖项的包:

$ composer require goaop/goaop-zend-expressive-middleware

\Go\Zend\Expressive\Middleware\AspectMiddleware 添加到您 config/pipeline.php 中的中间件列表。

$app->pipe(AspectMiddleware::class);

最好尽早添加中间件,以便 AOP 引擎可以在任何其他中间件应用之前采取行动。

配置

config/module.config.php 文件中的默认配置。将其复制到您的配置目录以修改值。

配置可用于进一步调整 AOP 内核和源代码的白名单/黑名单。

// config/module.config.php

$moduleConfig = [
    /*
     |--------------------------------------------------------------------------
     | AOP Debug Mode
     |--------------------------------------------------------------------------
     |
     | When AOP is in debug mode, then breakpoints in the original source code
     | will work. Also engine will refresh cache files if the original files were
     | changed.
     | For production mode, no extra filemtime checks and better integration with opcache
     |
     */
    'debug' => false,

    /*
     |--------------------------------------------------------------------------
     | Application root directory
     |--------------------------------------------------------------------------
     |
     | AOP will be applied only to the files in this directory, change it if needed
     */
    'appDir' => $basicDirectory,

    /*
     |--------------------------------------------------------------------------
     | AOP cache directory
     |--------------------------------------------------------------------------
     |
     | AOP engine will put all transformed files and caches in that directory
     */
    'cacheDir' => $basicDirectory . '/data/cache/aspect',

    /*
     |--------------------------------------------------------------------------
     | Cache file mode
     |--------------------------------------------------------------------------
     |
     | If configured then will be used as cache file mode for chmod
     */
    'cacheFileMode' => 0770 & ~umask(),

    /*
     |--------------------------------------------------------------------------
     | Controls miscellaneous features of AOP engine
     |--------------------------------------------------------------------------
     |
     | See \Go\Aop\Features enumeration for bit mask
     */
    'features' => 0,

    /*
     |--------------------------------------------------------------------------
     | White list of directories
     |--------------------------------------------------------------------------
     |
     | AOP will check this list to apply an AOP to selected directories only,
     | leave it empty if you want AOP to be applied to all files in the appDir
     */
    'includePaths' => [
        $basicDirectory . '/module'
    ],

    /*
     |--------------------------------------------------------------------------
     | Black list of directories
     |--------------------------------------------------------------------------
     |
     | AOP will check this list to disable AOP for selected directories
     */
    'excludePaths' => [],

    /**
     |--------------------------------------------------------------------------
     | AOP container class
     |--------------------------------------------------------------------------
     |
     | This option can be useful for extension and fine-tuning of services
     */
    'containerClass' => GoAspectContainer::class,
];

定义新方面

方面是 ZF2 应用程序中的服务,由集成模块在将它们放入 application.config.php 文件中的 goaop_aspect 部分后加载到 AOP 容器中。以下是一个实现模块/目录中公共方法调用的日志记录方面的示例。

定义带有 pointcut 和日志 advice 的 aspect 类

<?php
// module/Application/src/Application/Aspect/LoggingAspect.php

namespace Application\Aspect;

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

/**
 * Application logging aspect
 */
class LoggingAspect implements Aspect
{
    /**
     * Writes a log info before method execution
     *
     * @param MethodInvocation $invocation
     * @Before("execution(public Application\**->*(*))")
     */
    public function beforeMethod(MethodInvocation $invocation)
    {
        echo $invocation, json_encode($invocation->getArguments());
    }
}

要注册此应用程序方面,在 module.config.php 文件中为此服务创建相应的定义。

    'dependencies' => array(
        'invokables' => array(
            LoggingAspect::class => LoggingAspect::class,
        )
    ),

如果您需要向方面注入多个依赖项,则可以定义自己的工厂、初始化器等。

要自动在 AOP 内核中注册方面,将其服务名称添加到应用程序模块的 module.config.phpapplication.config.php 文件中的 goaop_aspect 配置部分。

    'goaop_aspect' => [
        LoggingAspect::class
    ]

许可证

此模块受 MIT 许可证约束。有关完整许可证信息,请参阅根目录中的 LICENSE 文件。