Skip to content

Commit 34a5e02

Browse files
committed
Initial commit
0 parents  commit 34a5e02

18 files changed

+904
-0
lines changed

.gitattributes

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.php diff=php
2+
3+
/.gitignore export-ignore
4+
/.php_cs export-ignore
5+
/.travis.yml export-ignore
6+
7+
/tests export-ignore
8+
9+
/phpunit.xml.dist export-ignore

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/build
2+
/vendor
3+
/composer.lock

.php_cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return Symfony\CS\Config\Config::create()
4+
->finder(
5+
Symfony\CS\Finder\DefaultFinder::create()
6+
->exclude('vendor')
7+
->exclude('build')
8+
->in(__DIR__)
9+
)
10+
;

.travis.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
language: php
2+
3+
sudo: false
4+
5+
install: composer update
6+
7+
php:
8+
- 5.4
9+
- 5.5
10+
- 5.6
11+
12+
cache:
13+
directories:
14+
- $HOME/.composer/cache
15+
16+
matrix:
17+
fast_finish: true
18+
19+
before_script:
20+
- echo 'date.timezone = "Europe/Berlin"' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
21+
22+
script:
23+
- phpunit
24+
25+
notifications:
26+
email:
27+

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# CHANGELOG
2+
3+
## Unreleased
4+
5+
* Initial release

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 kreait GmbH
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Firebase PHP Client
2+
3+
[![Latest Stable Version](https://poser.pugx.org/kreait/firebase-php/v/stable.png)](https://packagist.org/packages/kreait/firebase-php)
4+
[![Build Status](https://secure.travis-ci.org/kreait/firebase-php.png?branch=master)](http://travis-ci.org/kreait/firebase-php)
5+
6+
A PHP client library for [http://www.firebase.com](http://www.firebase.com).
7+
8+
### Installation
9+
10+
The recommended way to install Firebase is through
11+
[Composer](http://getcomposer.org).
12+
13+
```bash
14+
# Install Composer
15+
curl -sS https://getcomposer.org/installer | php
16+
```
17+
18+
Next, run the Composer command to install the latest stable version:
19+
20+
```bash
21+
composer require kreait/firebase-php
22+
```
23+
24+
After installing, you need to require Composer's autoloader:
25+
26+
```php
27+
require 'vendor/autoload.php';
28+
```
29+
30+
### Usage
31+
32+
```php
33+
use Kreait\Firebase\Firebase;
34+
use Kreait\Firebase\Reference;
35+
36+
$firebase = new Firebase('https://my-application-1234.firebaseio.com');
37+
38+
$allData = $firebase->get();
39+
40+
for ($i = 1; $i <= 5; $i++) {
41+
$firebase->set()
42+
}

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "kreait/firebase-php",
3+
"description": "Firebase REST API client",
4+
"keywords": ["firebase", "rest", "api", "http"],
5+
"homepage": "https://github.com/kreait/firebase-php",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Jérôme Gamez",
10+
"homepage": "https://github.com/jeromegamez",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"require": {
15+
"php": ">=5.4",
16+
"ext-curl": "*",
17+
"egeloen/http-adapter": "~0.5@dev",
18+
"psr/log": "~1.0"
19+
},
20+
"require-dev": {
21+
"phpunit/phpunit": "~4.4",
22+
"phpspec/prophecy-phpunit": "~1.0",
23+
"monolog/monolog": "~1.12",
24+
"guzzlehttp/guzzle": "~5.1"
25+
},
26+
"autoload": {
27+
"psr-4": { "Kreait\\Firebase\\": "src/" }
28+
},
29+
"autoload-dev": {
30+
"psr-4": { "Kreait\\Firebase\\": "tests/" }
31+
},
32+
"extra": {
33+
"branch-alias": {
34+
"dev-master": "1.x-dev"
35+
}
36+
}
37+
}

phpunit.xml.dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.4/phpunit.xsd"
5+
bootstrap="tests/bootstrap.php"
6+
colors="true">
7+
<testsuites>
8+
<testsuite>
9+
<directory>./tests</directory>
10+
</testsuite>
11+
</testsuites>
12+
13+
<filter>
14+
<whitelist>
15+
<directory suffix=".php">.</directory>
16+
<exclude>
17+
<directory>./tests</directory>
18+
<directory>./vendor</directory>
19+
</exclude>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

src/Firebase.php

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
/**
3+
* This file is part of the firebase-php package.
4+
*
5+
* For the full copyright and license information, please read the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
namespace Kreait\Firebase;
10+
11+
use Ivory\HttpAdapter\CurlHttpAdapter;
12+
use Ivory\HttpAdapter\HttpAdapterException;
13+
use Ivory\HttpAdapter\HttpAdapterInterface;
14+
use Ivory\HttpAdapter\Message\RequestInterface;
15+
16+
class Firebase implements FirebaseInterface
17+
{
18+
use \Psr\Log\LoggerAwareTrait;
19+
20+
/**
21+
* The HTTP adapter.
22+
*
23+
* @var HttpAdapterInterface
24+
*/
25+
private $http;
26+
27+
/**
28+
* Firebase client initialization.
29+
*
30+
* @param string $baseUrl The Firebase app base URL.
31+
* @param HttpAdapterInterface $http The HTTP adapter.
32+
* @throws FirebaseException When the base URL is not valid.
33+
*/
34+
public function __construct($baseUrl, HttpAdapterInterface $http = null)
35+
{
36+
$this->logger = new \Psr\Log\NullLogger();
37+
$this->http = $http ?: new CurlHttpAdapter();
38+
39+
$configuration = $this->http->getConfiguration();
40+
$configuration->setBaseUrl(Utils::normalizeBaseUrl($baseUrl));
41+
$configuration->setKeepAlive(true);
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function get($location = null)
48+
{
49+
return $this->send($location, RequestInterface::METHOD_GET);
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function set($data, $location)
56+
{
57+
return $this->send($location, RequestInterface::METHOD_PUT, $data);
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function push($data, $location)
64+
{
65+
return $this->send($location, RequestInterface::METHOD_POST, $data);
66+
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
public function update($data, $location)
72+
{
73+
return $this->send($location, RequestInterface::METHOD_PATCH, $data);
74+
}
75+
76+
/**
77+
* {@inheritdoc}
78+
*/
79+
public function delete($location)
80+
{
81+
return $this->send($location, RequestInterface::METHOD_DELETE);
82+
}
83+
84+
/**
85+
* Sends the request and returns the processed response data.
86+
*
87+
* @param string $location The location.
88+
* @param string $method The HTTP method.
89+
* @param array|object|null $data The data.
90+
*
91+
* @throws FirebaseException
92+
*
93+
* @return array|string|void The processed response data.
94+
*/
95+
private function send($location, $method, $data = null)
96+
{
97+
$location = (string) $location; // In case it is null
98+
99+
if ($data && !is_array($data) && !is_object($data)) {
100+
throw new \InvalidArgumentException(sprintf('array or object expected, %s given'), gettype($data));
101+
}
102+
103+
$relativeUrl = sprintf('/%s.json', Utils::normalizeLocation($location));
104+
105+
$headers = [
106+
'accept' => 'application/json',
107+
'accept-charset' => 'utf-8',
108+
];
109+
110+
$this->logger->debug(sprintf('%s request to %s', $method, $relativeUrl), ['data_sent' => $data]);
111+
$request = $this->http->getConfiguration()->getMessageFactory()->createRequest(
112+
$relativeUrl,
113+
$method,
114+
RequestInterface::PROTOCOL_VERSION_1_1,
115+
$headers,
116+
json_encode($data)
117+
);
118+
119+
try {
120+
$response = $this->http->sendRequest($request);
121+
} catch (HttpAdapterException $e) {
122+
$response = $e->hasResponse() ? $e->getResponse() : null;
123+
throw FirebaseException::httpError($request, $response);
124+
}
125+
126+
switch ($response->getStatusCode()) {
127+
case 400:
128+
$this->logger->warning('Invalid location or PUT/POST data');
129+
throw FirebaseException::invalidDataOrLocation($request, $response);
130+
case 403:
131+
$this->logger->warning('Forbidden');
132+
throw FirebaseException::forbiddenAction($request, $response);
133+
case 404:
134+
$this->logger->warning('Request made of HTTP instead of HTTPS');
135+
throw FirebaseException::requestMadeOverHttpsInsteadOfHttp($request, $response);
136+
case 417:
137+
$this->logger->warning('No namespace specified');
138+
throw FirebaseException::noNameSpaceSpecified($request, $response);
139+
}
140+
141+
if (!$response->hasBody()) {
142+
$this->logger->debug(
143+
sprintf('Received valid, empty response from %s request to %s', $method, $relativeUrl)
144+
);
145+
146+
return;
147+
}
148+
149+
$contents = $response->getBody()->getContents();
150+
$this->logger->debug(
151+
sprintf('Received valid response from %s request to %s', $method, $relativeUrl),
152+
['data_received' => $contents]
153+
);
154+
155+
return json_decode($contents, true);
156+
}
157+
}

0 commit comments

Comments
 (0)