承接 pdo-factory/pdo-factory 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pdo-factory/pdo-factory

Composer 安装命令:

composer require pdo-factory/pdo-factory

包简介

Factory class for creating PDO objects using credentials in external config files

README 文档

README

Synopsis

PHP Script

<?php
  $pdoFactory = new PdoFactory('etc/databases.ini');

  $pdo = $pdoFactory->createPDO('server1');
  
  $pdo->prepare('select * from table1');
  
  ...

Database configuration file etc/databases.ini

[server1]
driver=pgsql
host=localhost
dbname=db
username=user
password='secret password'

Overview

pdo-factory allows you to store your database connection information, including username and passwords in files that are separate to your codebase. Being in separate files, they can be omitted from your version control, and different connection information can be used depending on whether you are working in development, testing or production.

The configuration format is simple, and directly relates to the configuration options supported by the PDO driver itself. You can also specify PDO Attributes that can be assigned to the connection.

The library is also framework agnostic, meaning you can implement it in to any project, big or small.

Installation

To install, just do the usual:

$ composer require pdo-factory/pdo-factory

Usage

The following explains the API and how to use the library.

Constructor

public function __construct(string $source = 'etc/pdofactory.ini')

The constructor takes a single argument being the path to the configuration file that stores all the connection information. If a relative path is provided, then the library will attempt to locate the file relative to the path of the script itself, and if it is not found, move repeatedly up the directory tree until it finds a matching config file. For example if the path specified is etc/database.ini, and the path of the PHP script is /www/project/www/index.php, then pdo-factory will search using the following precendence until it finds a matching file:

  1. /www/project/www/etc/database.ini
  2. /www/project/etc/database.ini
  3. /www/etc/database.ini
  4. /etc/database.ini

This can be useful if you wish to have specific config files per project that can override more general config files. It also means if you place your project's code in different spots, you don't have to change paths to your config files (i.e. they can be relative within your project).

A common pattern would be to create an etc directory under your project directory and put your pdo-factory configuration files there. Then call pdo-factory's constructor as so:

$pdoFactory = new PdoFactory('etc/database.ini');

If you don't provide a path at all:

$pdoFactory = new PdoFactory();

Then the default path is etc/pdofactory.ini

Configuration File Format

The configuration parameters available in the config file vary according to the PDO driver that you are using. For convenience, the following links to the specific parameters for all the PDO supported database drivers:

  1. Postgresql
  2. MariaDB/Mysql
  3. Sqlite
  4. Oracle
  5. MSSQL (SQLSRV)
  6. MSSQL (DBLIB)

To specify the parameters, you have a choice of encoding. Presently, Microsoft INI file format and JSON format is supported. The library is modular so you can add your own encoding formats, such as YAML, .env and whatever else suits your project.

INI File

Here is an example config file using the INI format for a Postgresql Database:

[server1]
driver=pgsql
host=localhost
dbname=db
username=user
password=pass
attribute[PDO::ATTR_TIMEOUT]=30
attribute[PDO::ATTR_ERRMODE]=PDO::ERRMODE_EXCEPTION

JSON File

Here is the same example config file instead using JSON format:

{
  "server1": {
    "driver": "pgsql",
    "host": "localhost",
    "dbname": "db",
    "username": "user",
    "password": "pass",
    "attribute": {
      "PDO::ATTR_TIMEOUT": 30,
      "PDO::ATTR_ERRMODE": "PDO::ERRMODE_EXCEPTION"
    }
  }
}

Contributions

Contributions are most welcome via pull requests on Github. The following is an existing list of missing features, and most of which are quite trivial to add.

TODOs

  1. Add driver support for
  • Oracle
  • Mysql/MariaDB
  • MSSQL (DBLIB)
  1. Add encoding support for
  • YAML
  • .env
  1. Further improve unit testing

Licence

Copyright (c) 2018 Damien Clark

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

pdo-factory/pdo-factory 适用场景与选型建议

pdo-factory/pdo-factory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 33 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 07 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 pdo-factory/pdo-factory 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 pdo-factory/pdo-factory 我们能提供哪些服务?
定制开发 / 二次开发

基于 pdo-factory/pdo-factory 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 33
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2018-07-31