kainiklas / laravel-strict-mode
Laravel 包,用于启用 Eloquent '严格模式' 和其他安全方法。
2.0.0
2023-04-06 19:56 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.14
Requires (Dev)
- laravel/pint: ^1.8
- nunomaduro/collision: ^7.4
- nunomaduro/larastan: ^2.5
- orchestra/testbench: ^8.3
- pestphp/pest: ^2.4
- pestphp/pest-plugin-faker: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.2
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2024-09-09 12:22:31 UTC
README
启用以下可配置的 Eloquent "严格模式" 功能
- 防止延迟加载(N+1)
- 非生产环境:抛出
Illuminate\Database\LazyLoadingViolationException
异常 - 生产环境:将警告写入日志
- 非生产环境:抛出
- 防止使用未从数据库获取或模型上不存在的数据模型属性时默认为 NULL
- 抛出
Illuminate\Database\Eloquent\MissingAttributeException
异常(所有环境) - 或者:您可以将配置设置为将警告写入日志
- 抛出
- 防止在创建或更新模型时由于 $fillable 数组中缺少属性而丢失属性
- 抛出
Illuminate\Database\Eloquent\MassAssignmentException
异常(所有环境) - 或者:您可以将配置设置为将警告写入日志
- 抛出
启用以下可配置的安全方法
- 长时间运行的命令监控
- 将警告写入日志,包括命令名称、用户和持续时间,持续时间超过指定的阈值(默认:5000ms)
- 长时间运行的请求监控
- 将警告写入日志,包括请求 URL、用户和持续时间,持续时间超过指定的阈值(默认:5000ms)
- 长时间运行的单个 DB 查询监控
- 将信息写入日志,包括 SQL 查询和持续时间,持续时间超过指定的阈值(默认:1000ms)
- 长时间运行的 DB 连接监控
- 将信息写入日志,包括 DB 连接名称,持续时间超过指定的阈值(默认:2000ms)
- 内存堆大小监控
- 将警告写入日志,包括内存堆大小超过指定的阈值(默认:50MB)时的情况
安装
需要 Laravel 版本 ^10.0。
您可以通过 composer 安装此包
composer require kainiklas/laravel-strict-mode
可选地,您可以发布配置文件。
php artisan vendor:publish --tag="laravel-strict-mode-config"
您可以使用环境变量来影响行为。
这是已发布的配置文件的内容。
return [ /** * Throws Illuminate\Database\LazyLoadingViolationException if model is lazy loaded. * Exception is only thrown if log_lazy_loading is set to false. */ 'prevent_lazy_loading' => env( 'PREVENT_LAZY_LOADING', true, ), /** * Lazy Loading violation is logged. No exception is thrown. * Only works, if prevent_lazy_loading is true. */ 'log_lazy_loading' => env( 'LOG_LAZY_LOADING', env('APP_ENV') == 'production' ), /** * Prevent non-fillable attributes from being silently discarded. * Instead, throws Illuminate\Database\Eloquent\MassAssignmentException. * Exception is only thrown if log_prevent_silently_discarding_attributes is false. */ 'prevent_silently_discarding_attributes' => env( 'PREVENT_SILENTLY_DISCARDING_ATTRIBUTES', true ), /** * Log warning Illuminate\Database\Eloquent\MassAssignmentException * instead of throwing the exception. * Only works if prevent_silently_discarding_attributes is true. */ 'log_prevent_silently_discarding_attributes' => env( 'LOG_PREVENT_SILENTLY_DISCARDING_ATTRIBUTES', false ), /** * If activated an Illuminate\Database\Eloquent\MissingAttributeException * is thrown whenever an attribute is accessed which is not present in the model, * instead of falling back to NULL. * * Exception is only thrown if log_prevent_accessing_missing_attributes is false. */ 'prevent_accessing_missing_attributes' => env( 'PREVENT_ACCESSING_MISSING_ATTRIBUTES', true ), /** * Log warning Illuminate\Database\Eloquent\MissingAttributeException * instead of throwing the exception. * Only works if prevent_accessing_missing_attributes is true. */ 'log_prevent_accessing_missing_attributes' => env( 'LOG_PREVENT_ACCESSING_MISSING_ATTRIBUTES', false ), /** * Logs a warning if a command runs longer than the specified threshold. */ 'log_long_running_command' => env( 'LOG_LONG_RUNNING_COMMAND', true ), /** * Threshold for long running command in milliseconds [ms]. */ 'log_long_running_command_threshold' => env( 'LOG_LONG_RUNNING_COMMAND_THRESHOLD', 5000 // [ms] ), /** * Logs a warning if a HTTP request runs longer than the specified threshold. */ 'log_long_running_request' => env( 'LOG_LONG_RUNNING_REQUEST', true ), /** * Threshold for long running HTTP request in milliseconds [ms]. */ 'log_long_running_request_threshold' => env( 'LOG_LONG_RUNNING_REQUEST_THRESHOLD', 5000 // [ms] ), /** * Logs a warning if a DB connection runs longer than the specified threshold. */ 'log_long_running_total_db_query' => env( 'LOG_LONG_RUNNING_TOTAL_DB_QUERY', true ), /** * Threshold for long running db connection in milliseconds [ms]. */ 'log_long_running_total_db_query_threshold' => env( 'LOG_LONG_RUNNING_TOTAL_DB_QUERY_THRESHOLD', 2000 // [ms] ), /** * Logs a warning if a single DB Query runs longer than the specified threshold. */ 'log_long_running_single_db_query' => env( 'LOG_LONG_RUNNING_SINGLE_DB_QUERY', true ), /** * Threshold for long running single DB Query in milliseconds [ms]. */ 'log_long_running_single_db_query_threshold' => env( 'LOG_LONG_RUNNING_SINGLE_DB_QUERY_THRESHOLD', 1000 // [ms] ), /** * Logs a warning if a request cycle consumed more memory than the specified threshold. */ 'log_memory_heap_size' => env( 'LOG_MEMORY_HEAP_SIZE', true ), /** * Threshold for memory heap size in Megabytes [MB]. */ 'log_memory_heap_size_threshold' => env( 'LOG_MEMORY_HEAP_SIZE_THRESHOLD', 50 // [MB] ), ];
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
请查看 我们的安全策略 了解如何报告安全漏洞。
鸣谢
本包基于以下文章
致谢
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 了解更多信息。