devbry/firebase-php

Firebase PHP 客户端

2.0.0 2015-03-04 13:35 UTC

This package is not auto-updated.

Last update: 2024-10-02 10:29:59 UTC


README

Build Status

Scrutinizer Quality Score

基于 Firebase REST API

Packagist 上可用。

使用 Composer 将 Firebase PHP 添加到您的项目中

cd <your_project>
composer require ktamas77/firebase-php dev-master

有关 Composer 的更多信息,请访问 getcomposer.org

示例

const DEFAULT_URL = 'https://kidsplace.firebaseio.com/';
const DEFAULT_TOKEN = 'MqL0c8tKCtheLSYcygYNtGhU8Z2hULOFs9OKPdEp';
const DEFAULT_PATH = '/firebase/example';

$firebase = new \Firebase\FirebaseLib(DEFAULT_URL, DEFAULT_TOKEN);

// --- storing an array ---
$test = array(
    "foo" => "bar",
    "i_love" => "lamp",
    "id" => 42
);
$dateTime = new DateTime();
$firebase->set(DEFAULT_PATH . '/' . $dateTime->format('c'), $test);

// --- storing a string ---
$firebase->set(DEFAULT_PATH . '/name/contact001', "John Doe");

// --- reading the stored string ---
$name = $firebase->get(DEFAULT_PATH . '/name/contact001');

支持的命令

// -- Firebase API commands

$firebase->set($path, $value);   // stores data in Firebase
$value = $firebase->get($path);  // reads a value from Firebase
$firebase->delete($path);        // deletes value from Firebase
$firebase->update($path, $data); // updates data in Firebase
$firebase->push($path, $data);   // push data to Firebase

// -- Firebase PHP Library commands

$firebase->setToken($token);     // set up Firebase token
$firebase->setBaseURI($uri);     // set up Firebase base URI (root node)
$firebase->setTimeOut($seconds); // set up maximum timeout / request

有关更多详细信息,请参阅 Firebase REST API 文档

Firebase PHP Stub

已创建 Firebase PHP Stub 以允许与 phpunit 集成,而无需实际与 FirebaseIO 交互。

要在您的应用程序和测试中使用 firebaseLib 和 firebaseStub,您必须将 Firebase 对象传递给您的应用程序。

例如,如果您的当前代码是

public function setFirebaseValue($path, $value) {
  $firebase = new Firebase('https://radiant-fire-2427.firebaseio.com', 'czvEX8vMU8FZn4wYCvf466P3J6zH5ZlKQeuwxmEZ');
  $firebase->set($path, $value);
}

您将将其更改为

public function setFirebaseValue($path, $value, $firebase) {
  $firebase->set($path, $value);
}

然后您的 phpunit 测试将如下所示

<?php
  require_once '<path>/lib/firebaseInterface.php';
  require_once '<path>/lib/firebaseStub.php';

  class MyClass extends PHPUnit_Framework_TestCase
  {
    public function testSetFirebaseValue() {
      $myClass = new MyClass();
      $firebaseStub = new FirebaseStub($uri, $token);
      $myClass->setFirebaseValue($path, $value, $firebaseStub);
    }
  }
?>

单元测试

所有单元测试都位于 "/tests" 目录中。由于使用了接口,测试必须在隔离模式下运行。

由于实际 cURL 调用到实时 firebaseIO 账户,firebaseLib 测试具有固有的延迟。可以通过运行以下命令执行 firebaseLib 测试

$ phpunit tests/firebaseTest.php

可以通过运行以下命令执行 FirebaseStub 测试

$ phpunit tests/firebaseStubTest.php

MIT 许可证 (MIT)

Copyright (c) 2012-2015 Tamas Kalman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.