spatie / laravel-sql-commenter
为Laravel执行的SQL查询添加注释
Requires
- php: ^8.1
- illuminate/contracts: ^9.0|^10.0|^11.0
- spatie/backtrace: ^1.2
- spatie/invade: ^1.1|^2.0
- spatie/laravel-package-tools: ^1.12
Requires (Dev)
- larastan/larastan: ^2.1
- nunomaduro/collision: ^6.2|^8.0
- orchestra/testbench: ^7.6|^8.0|^9.0
- pestphp/pest: ^1.21|^2.0
- pestphp/pest-plugin-laravel: ^1.2|^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.1
- phpunit/phpunit: ^9.5|^10.0
- spatie/laravel-ray: ^1.29
README
此包可以为Laravel执行的查询添加注释。这些注释将使用sqlcommenter格式,该格式被各种工具和服务理解,例如PlanetScale的Query Insights。
这是默认查询的示例
select * from users
使用此包,将添加类似这样的注释。
select * from "users"/*controller='UsersController',action='index'*/;
注释可以轻松地让您定位代码库中查询的来源。
支持我们
我们投入了大量资源来创建一流的开放式源代码包。您可以通过购买我们的付费产品之一来支持我们。
我们非常感谢您从家乡给我们寄来明信片,注明您正在使用我们的哪个包。您可以在我们的联系页面找到我们的地址。我们将在我们的虚拟明信片墙上发布所有收到的明信片。
安装
您可以通过Composer安装此包
composer require spatie/laravel-sql-commenter
可选地,您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="sql-commenter-config"
这是发布配置文件的内容
return [ /* * When set to true, comments will be added to all your queries */ 'enabled' => true, /* * These classes add comments to an executed query. */ 'commenters' => [ Spatie\SqlCommenter\Commenters\ControllerCommenter::class => ['includeNamespace' => false], Spatie\SqlCommenter\Commenters\RouteCommenter::class, Spatie\SqlCommenter\Commenters\JobCommenter::class => ['includeNamespace' => false], Spatie\SqlCommenter\Commenters\FileCommenter::class => [ 'backtraceLimit' => 20, 'excludePathSegments' => [], 'useRelativePath' => false, ], Spatie\SqlCommenter\Commenters\CurrentUserCommenter::class, // Spatie\SqlCommenter\Commenters\FrameworkVersionCommenter::class, // Spatie\SqlCommenter\Commenters\DbDriverCommenter::class, ], /* * If you need fine-grained control over the logging, you can extend * the SqlCommenter class and specify your custom class here */ 'commenter_class' => Spatie\SqlCommenter\SqlCommenter::class, ];
使用方法
安装包后,注释将自动添加。通过发布配置文件,您可以选择将哪些内容添加到注释中。
添加任意注释
如果您想向SqlComment添加其他任意注释,可以使用addComment
方法。所提供的注释将被添加到下一个执行的查询中。
use Spatie\SqlCommenter\SqlCommenter; app(SqlCommenter::class)->addComment('foo', 'bar'); // select * from "users"/*foo='bar'*/;
动态启用和禁用添加注释
您可以动态地启用和禁用查询日志。
假设您只想在代码库的某个部分添加注释。首先,您需要将sql-commenter
配置文件中enabled
键的值设置为false
。这将阻止包向所有查询添加注释。在您想添加注释的部分之前,调用SqlCommenter::enable()
,在结束时调用SqlCommenter::disable()
use \Spatie\SqlCommenter\SqlCommenter; // queries performed here won't have comments SqlCommenter::enable(); // queries performed here will have comments SqlCommenter::disable(); // queries performed here won't have comments
添加您自己的评论者
如果您想将注释添加到所有执行的查询中,您可以创建自己的Commentator
类。它应该实现Spatie\SqlCommenter\Commenters\Commenter
接口。`comments`函数应返回一个或多个`Spatie\SqlCommenter\Comment`。
这里是一个示例
namespace App\Support\SqlCommenters; use Illuminate\Database\Connection; use Spatie\SqlCommenter\Comment; class MyCustomCommenter implements Commenter { /** @return Comment|array<Comment>|null */ public function comments(string $query, Connection $connection): Comment|array|null { return new Comment('my-custom-key', 'my-custom-value'); } }
测试
composer test
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
安全漏洞
有关如何报告安全漏洞的详细信息,请参阅我们的安全策略。
鸣谢
许可协议
麻省理工学院许可证(MIT)。请参阅许可证文件获取更多信息。