squirrelphp/queries-bundle
Composer 安装命令:
composer require squirrelphp/queries-bundle
包简介
Symfony integration of squirrelphp/queries - automatic assembling of decorated connections.
README 文档
README
Integration of squirrelphp/queries into Symfony through service tags and bundle configuration.
Installation
composer require squirrelphp/queries-bundle
Configuration
Enable the bundle in your project by adding Squirrel\QueriesBundle\SquirrelQueriesBundle to the list of bundles (usually in config/bundles.php).
Create a configuration file for your connections, here is an example:
squirrel_queries:
connections:
default:
type: 'mysql'
host: '%env(DB_HOST)'
port: '%env(DB_PORT)'
user: '%env(DB_USER)'
password: '%env(DB_PASSWORD)'
dbname: '%env(DB_DBNAME)'
error:
type: 'mariadb'
host: 'mariadb_database'
port: 9999
user: 'app'
password: 'hg84kdhgjg84'
dbname: 'app_prod'
ssl:
rootCertificatePath: '/home/app/ssl/ca.crt'
privateKeyPath: '/home/app/ssl/ca.key'
certificatePath: '/home/app/ssl/cert.crt'
verification: 'CaAndHostname'
sqlite:
type: 'sqlite'
path: '/home/database.db'
The example shows all possible values you can set (except for charset, which is explained further below but should ideally be omitted/left at the default).
type can only be either mysql, mariadb, pgsql or sqlite. mysql and mariadb are functionally equivalent.
Type sqlite only supports the optional path parameter to the database file, when not provided (or null) it creates an in-memory database.
For mysql, mariadb and pgsql connections the following values can be provided:
hostcan be a hostname or IP in order to connect to the database serverportis an integer and is optional (defaults to 3306 for mysql/mariadb, 5432 for postgresql)useris the username with which to connectpasswordis the password to connect withdbnameis the default database to open and is optional, by default (or with a value of null) no database is openedcharsetcan be provided and defaults toutf8mb4for mysql/mariadb andUTF8for postgresql, the recommendation is to leave it at the defaultsslsets additional values so the connection will be encrypted (if omitted or set to null no encryption is used):rootCertificatePathis the path to the root certificate file used by the database serverprivateKeyPathis the path to the private key the client sends to the database servercertificatePathis the path to the certificate file the client sends to the database serververificationcan be one of the following values:CaAndHostname(which is the default) enforces that the CA of the database server certificate matches the CA inrootCertificatePathand that the hostname of the server certificate matches the hostname inhostCaenforces that the CA of the database server certificate matches the CA inrootCertificatePath(the hostname is not verified) - beware that this does not work for mysql/mariadb and will throw an exception for those connections, because just checking the CA is not supported by PHP for mysql/mariadb (it works as expected for postgresql)Nonewill not check the CA or the hostname of the server certificate
The connection named default will be registered as Squirrel\Queries\DBInterface which you can then use as a type hint in your services. All connections also get registered as services that start with squirrel.connection., so in the above example the following services would be defined: squirrel.connection.default, squirrel.connection.error and squirrel.connection.sqlite.
Common behavior of all connections
The following options are hardcoded into all connections and mostly differ from the common defaults in PHP database connections (see squirrelphp/connection for more details):
- Emulation of prepares is turned off, so real query and values separation is enabled instead of emulating it (which is usually the default in PHP). You should not notice this in any way, even in terms of performance: it was tested, and when script and database are running in the same network there is no measureable difference. Your script and database would need to be apart by some distance for any possible effect to manifest. On the other hand, the separation of queries and values has undeniable security benefits and is the way the underlying database client libraries are designed to work.
- For MySQL/MariaDB, the "affected rows" reported for UPDATE queries are the "found rows" in the database, even if nothing changed by executing the UPDATE. By default with MySQL/MariaDB in PHP you get the "changed rows", which is a behavior no other database has or even supports, so MySQL/MariaDB is configured to behave more like any other database. Getting the "found rows" count can be useful information, while relying on the "changed rows" count relies on special behavior in one database system.
- Executing multiple statements in one query is disabled. Multiple statements per query were a source of security exploits in the past, are often not easy to port between different database systems and have little real world relevance. Use transactions instead, which is a guaranteed way to execute multiple statements together, or use parallel connections / multiple connections if speed is an issue.
Adding layers
By default, this bundle creates DBInterface services with an implementation layer and an error handling layer (see squirrelphp/queries for details).
If you want to add additional layers to decorate DBInterface, create a service for each additional layer and tag it with squirrel.layer. Make sure the service implements Squirrel\Queries\DBRawInterface and to add the trait Squirrel\Queries\DBPassToLowerLayerTrait in the service. Define a priority for the tag and set it to below zero if you want to inject it between the implementation and the error handler, or above zero if it should be above the error handler.
Example for the service definition of a logger which logs deadlocks / connection timeouts before the error handler automatically retries the query/transaction:
services:
Squirrel\QueriesBundle\Examples\SQLLogTemporaryFailuresListener:
tags:
- { name: squirrel.layer, priority: -250 }
Because the priority is below zero it is a layer beneath the error handler. You can find a possible implementation in the examples directory.
Symfony Profiler
When using Symfony Profiler this library offers similar integration like the DoctrineBundle automatically - so you can check what queries were sent to the database and how long they took.
squirrelphp/queries-bundle 适用场景与选型建议
squirrelphp/queries-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.46k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 04 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「symfony」 「php」 「abstraction」 「mysql」 「bundle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 squirrelphp/queries-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 squirrelphp/queries-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 squirrelphp/queries-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
The bundle for easy using json-rpc api on your project
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
Bundle Symfony DaplosBundle
统计信息
- 总下载量: 3.46k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 1
- 推荐数: 3
其他信息
- 授权协议: MIT
- 更新时间: 2019-04-23