goaop/goaop-laminas-module

Go! AOP 框架的集成模块

v3.2.0 2021-09-25 19:33 UTC

README

GitHub release Minimum PHP Version License

GoAopModule 通过 Go! AOP 框架为 Laminas 框架 2 应用程序添加了对面向方面编程(AOP)的支持。

概述

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

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

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

您可以在不同的来源中了解更多关于 AOP 的信息,Java 语言有一些很好的文章,它们也可以应用于 PHP,因为这是一种通用范式。安装

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

$ composer require goaop/goaop-laminas-module

Go\Laminas\Framework 添加到 config/application.config.php 文件中的 modules 数组中的模块列表。

// config/application.config.php

    // This should be an array of module namespaces used in the application.
    'modules' => array(
        'Go\Laminas\Framework',
        'Application',
    ),

配置

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,
];

定义新的方面

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

定义具有 pointcut 和记录 advice 的方面类

<?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 文件中创建适当的服务定义。

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

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

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

    'goaop_aspect' => [
        LoggingAspect::class
    ]

许可

此模块采用 MIT 许可证。请参阅根目录中的完整 LICENSE。