thieule/goaop-lumen
Go! AOP 框架的集成桥接器
Requires
- goaop/framework: ^1.0|^2.0
- laravel/lumen-framework: 5.4.*
This package is not auto-updated.
Last update: 2024-09-20 19:30:07 UTC
README
这是从 goaop/framework 和 goaop/goaop-laravel-bridge 复制和参考的代码,支持 Lumen。
GoAopLumen 为 Lumen 5.4.* 应用程序添加了对面向切面编程(AOP)的支持。
概述
面向切面范式允许通过特殊的工具扩展标准面向对象范式,以有效地解决应用程序中的横切关注点。此代码通常存在于您的应用程序的各个地方(例如,日志记录、缓存、监控等),没有 AOP 就很难修复这些问题。
AOP 为开发者定义了新的工具,包括
- 连接点 - 可以用于拦截的代码中的位置,例如执行单个公共方法或访问单个对象属性。
- 切入点是一组通过特殊正则表达式定义的连接点,用于您的源代码,例如,具体类或命名空间中的所有公共和受保护方法。
- 通知是一个额外的回调,将在具体的连接点之前、之后或周围调用。对于 PHP,每个通知都表示为一个
\Closure
实例,它被包装在拦截器对象中。 - 方面是一个特殊的类,它将切入点和建议结合起来,每个切入点都定义为一个注解,每个建议是这个方面内部的某个方法。
您可以在不同的来源中了解更多关于 AOP 的信息,关于 Java 语言的优秀文章也适用于 PHP,因为它是一个通用范式。或者,您还可以观看一个关于 Laravel 中 AOP 的很好的演示
安装
可以使用 composer 轻松安装 GoAopBridge。只需运行以下命令来请求 composer 下载包含依赖项的捆绑包
$ composer require thieule/goaop-lumen
将 Go\Lumen\GoAopBridge\GoAopServiceProvider
添加到您的 bootstrap/app.php 文件中的 providers
数组中
// bootstrap/app.php $app->register(Go\Lumen\GoAopBridge\GoAopServiceProvider::class);
确保此服务提供程序是这个列表中的 第一个项目。这是 AOP 引擎正确工作的必要条件。
配置
config/go_aop.php
文件中的默认配置。将此文件复制到您的配置目录以修改值。
配置可用于对 AOP 内核和源代码白名单/黑名单进行额外的调整。
// config/go_aop.php return [ /* |-------------------------------------------------------------------------- | 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' => env('APP_DEBUG', false), /* |-------------------------------------------------------------------------- | Application root directory |-------------------------------------------------------------------------- | | AOP will be applied only to the files in this directory, change it to app_path() | if needed */ 'appDir' => base_path(), /* |-------------------------------------------------------------------------- | AOP cache directory |-------------------------------------------------------------------------- | | AOP engine will put all transformed files and caches in that directory */ 'cacheDir' => storage_path('app/aspect'), /* |-------------------------------------------------------------------------- | Cache file mode |-------------------------------------------------------------------------- | | If configured then will be used as cache file mode for chmod */ 'cacheFileMode' => 511, /* |-------------------------------------------------------------------------- | 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' => [ __DIR__.'/../app' ], /* |-------------------------------------------------------------------------- | 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, ];
定义新方面
方面是 Lumen 应用程序中的服务,并通过收集容器中带有 goaop.aspect
标签的所有服务的服务提供程序加载到 AOP 容器中。以下是如何实现一个记录方面的示例,该方面将在 app/ 目录中记录公共方法调用的信息。
定义具有切入点和记录建议的方面类
<?php namespace App\Aspect; use Go\Aop\Aspect; use Go\Aop\Intercept\MethodInvocation; use Go\Lang\Annotation\Before; use Psr\Log\LoggerInterface; /** * Application logging aspect */ class LoggingAspect implements Aspect { /** * @var LoggerInterface */ private $logger; public function __construct(LoggerInterface $logger) { $this->logger = $logger; } /** * Writes a log info before method execution * * @param MethodInvocation $invocation * @Before("execution(public **->*(*))") */ public function beforeMethod(MethodInvocation $invocation) { $this->logger->info($invocation, $invocation->getArguments()); } }
将 LoggingAspect 服务添加到 App 服务提供程序中的 register()
方法中,添加方面的声明并使用 goaop.aspect
标签对其进行标记
namespace App\Providers; use App\Aspect\LoggingAspect; use Illuminate\Support\ServiceProvider; use Psr\Log\LoggerInterface; class AppServiceProvider extends ServiceProvider { /** * Register the application services. * * @return void */ public function register() { $this->app->singleton(LoggingAspect::class, function ($app) { return new LoggingAspect($app->make(LoggerInterface::class)); }); $this->app->tag([LoggingAspect::class], 'goaop.aspect'); }
您可以在注册函数中添加新的 Provider 和 LoggingAspect!
许可证
此桥接器受 MIT 许可证的保护。请参阅根目录中的完整 LICENSE。