ahsankhatri/firestore-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

ahsankhatri/firestore-php

最新稳定版本:v2.0.4

Composer 安装命令:

composer require ahsankhatri/firestore-php

包简介

Firestore PHP Client

README 文档

README

Latest Version on Packagist Total Downloads License

This package is totally based on Firestore REST API

Authentication / Generate API Key

  1. Visit Google Cloud Firestore API
  2. Select your desired project.
  3. Select Credentials from left menu and select API Key from Server key or Create your own credentials

Installation

You can install the package via composer:

composer require ahsankhatri/firestore-php

or install it by adding it to composer.json then run composer update

"require": {
    "ahsankhatri/firestore-php": "^2.0",
}

Dependencies

The bindings require the following extensions in order to work properly:

If you use Composer, these dependencies should be handled automatically. If you install manually, you'll want to make sure that these extensions are available.

Usage

Initialization

$firestoreClient = new FirestoreClient('project-id', 'AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', [
    'database' => '(default)',
]);

Adding a document

$firestoreClient->addDocument($collection, [
    'booleanTrue' => true,
    'booleanFalse' => false,
    'null' => null,
    'string' => 'abc123',
    'integer' => 123456,
    'arrayRaw' => [
        'string' => 'abc123',
    ],
    'bytes' => new FirestoreBytes('bytesdata'),
    'array' => new FirestoreArray([
        'string' => 'abc123',
    ]),
    'reference' => new FirestoreReference('/users/23'),
    'object' => new FirestoreObject(['nested1' => new FirestoreObject(['nested2' => new FirestoreObject(['nested3' => 'test'])])]),
    'timestamp' => new FirestoreTimestamp,
    'geopoint' => new FirestoreGeoPoint(1,1),
]);

NOTE: Pass third argument if you want your custom document id to set else auto-id will generate it for you.

Or

$document = new FirestoreDocument;
$document->setObject('sdf', new FirestoreObject(['nested1' => new FirestoreObject(['nested2' => new FirestoreObject(['nested3' => 'test'])])]));
$document->setBoolean('booleanTrue', true);
$document->setBoolean('booleanFalse', false);
$document->setNull('null', null);
$document->setString('string', 'abc123');
$document->setInteger('integer', 123456);
$document->setArray('arrayRaw', ['string'=>'abc123']);
$document->setBytes('bytes', new FirestoreBytes('bytesdata'));
$document->setArray('arrayObject', new FirestoreArray(['string' => 'abc123']));
$document->setTimestamp('timestamp', new FirestoreTimestamp);
$document->setGeoPoint('geopoint', new FirestoreGeoPoint(1.11,1.11));

$firestoreClient->addDocument($collection, $document, 'customDocumentId');

And..

$document->fillValues([
    'string' => 'abc123',
    'boolean' => true,
]);

Inserting/Updating a document

  • Update (Merge) or Insert document

Following will merge document (if exist) else insert the data.

$firestoreClient->updateDocument($documentRoot, [
    'newFieldToAdd' => new FirestoreTimestamp(new DateTime('2018-04-20 15:00:00')),
    'existingFieldToRemove' => new FirestoreDeleteAttribute
]);

NOTE: Passing 3rd argument as a boolean true will force check that document must exist and vice-versa in order to perform update operation.

For example: If you want to update document only if exist else MrShan0\PHPFirestore\Exceptions\Client\NotFound (Exception) will be thrown.

$firestoreClient->updateDocument($documentPath, [
    'newFieldToAdd' => new FirestoreTimestamp(new DateTime('2018-04-20 15:00:00')),
    'existingFieldToRemove' => new FirestoreDeleteAttribute
], true);
  • Overwirte or Insert document
$firestoreClient->setDocument($collection, $documentId, [
    'newFieldToAdd' => new FirestoreTimestamp(new DateTime('2018-04-20 15:00:00')),
    'existingFieldToRemove' => new FirestoreDeleteAttribute
], [
    'exists' => true, // Indicate document must exist
]);

Deleting a document

$collection = 'collection/document/innerCollection';
$firestoreClient->deleteDocument($collection, $documentId);

List documents with pagination (or custom parameters)

$collections = $firestoreClient->listDocuments('users', [
    'pageSize' => 1,
    'pageToken' => 'nextpagetoken'
]);

Note: You can pass custom parameters as supported by firestore list document

Get field from document

$document->get('bytes')->parseValue(); // will return bytes decoded value.

// Catch field that doesn't exist in document
try {
    $document->get('allowed_notification');
} catch (\MrShan0\PHPFirestore\Exceptions\Client\FieldNotFound $e) {
    // Set default value
}

Firebase Authentication

Sign in with Email and Password.

$firestoreClient
    ->authenticator()
    ->signInEmailPassword('testuser@example.com', 'abc123');

Sign in Anonymously.

$firestoreClient
    ->authenticator()
    ->signInAnonymously();

Retrieve Auth Token

$authToken = $firestoreClient->authenticator()->getAuthToken();

TODO

  • Added delete attribute support.
  • Add Support for Object, Boolean, Null, String, Integer, Array, Timestamp, GeoPoint, Bytes
  • Add Exception Handling.
  • List all documents.
  • List all collections.
  • Filters and pagination support.
  • Structured Query support.
  • Transaction support.
  • Indexes support.
  • Entire collection delete support.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email ahsankhatri1992@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

ahsankhatri/firestore-php 适用场景与选型建议

ahsankhatri/firestore-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 160.21k 次下载、GitHub Stars 达 60, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「google」 「firebase」 「firestore」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 160.21k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 61
  • 点击次数: 16
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 60
  • Watchers: 9
  • Forks: 45
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 未知