goaop / goaop-zf2-module
Go! AOP 框架的集成模块
Requires
- goaop/framework: ^1.0|^2.0
- zendframework/zend-modulemanager: ^2.0
Requires (Dev)
- phpunit/phpunit: ^5.0 | ^6.0
- symfony/console: ^3.0 | ^4.0
- zendframework/zend-console: ^2.0
- zendframework/zend-i18n: ^2.0
- zendframework/zend-log: ^2.0
- zendframework/zend-mvc: ^2.0 | ^3.0
- zendframework/zend-serializer: ^2.0
- zendframework/zend-view: ^2.0
Suggests
- symfony/console: Required to use goaop console warmup
README
GoAopModule 为 Zend Framework 2 应用程序添加了通过 Go! 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-zf2-module
将 Go\Zend\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\Zend\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, ];
定义新的 aspects
Aspect 是 ZF2 应用程序中的服务,在集成模块将它们放入 application.config.php 文件中的 goaop_aspect 部分后加载到 AOP 容器中。以下是如何实现一个记录模块/目录中公共方法调用的记录 aspect 的示例。
带有 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()); } }
要注册此应用程序 aspect 到容器中,在 module.config.php 文件中为该服务创建一个适当的定义
'service_manager' => array(
'invokables' => array(
LoggingAspect::class => LoggingAspect::class,
)
),
如果您需要将多个依赖项注入到您的 aspect 中,则可以定义自己的工厂、初始化器等
要自动在 AOP 内核中注册 aspect,将其服务名称添加到 Application 模块的 module.config.php 或 application.config.php 文件中的 goaop_aspect 配置部分
'goaop_aspect' => [
LoggingAspect::class
]
许可证
此模块受 MIT 许可证约束。请参阅根目录中的完整 LICENSE 文件