-
-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 34a5e02
Showing
18 changed files
with
904 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
*.php diff=php | ||
|
||
/.gitignore export-ignore | ||
/.php_cs export-ignore | ||
/.travis.yml export-ignore | ||
|
||
/tests export-ignore | ||
|
||
/phpunit.xml.dist export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/build | ||
/vendor | ||
/composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
return Symfony\CS\Config\Config::create() | ||
->finder( | ||
Symfony\CS\Finder\DefaultFinder::create() | ||
->exclude('vendor') | ||
->exclude('build') | ||
->in(__DIR__) | ||
) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
language: php | ||
|
||
sudo: false | ||
|
||
install: composer update | ||
|
||
php: | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
|
||
cache: | ||
directories: | ||
- $HOME/.composer/cache | ||
|
||
matrix: | ||
fast_finish: true | ||
|
||
before_script: | ||
- echo 'date.timezone = "Europe/Berlin"' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini | ||
|
||
script: | ||
- phpunit | ||
|
||
notifications: | ||
email: | ||
- [email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# CHANGELOG | ||
|
||
## Unreleased | ||
|
||
* Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 kreait GmbH | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Firebase PHP Client | ||
|
||
[![Latest Stable Version](https://poser.pugx.org/kreait/firebase-php/v/stable.png)](https://packagist.org/packages/kreait/firebase-php) | ||
[![Build Status](https://secure.travis-ci.org/kreait/firebase-php.png?branch=master)](http://travis-ci.org/kreait/firebase-php) | ||
|
||
A PHP client library for [http://www.firebase.com](http://www.firebase.com). | ||
|
||
### Installation | ||
|
||
The recommended way to install Firebase is through | ||
[Composer](http://getcomposer.org). | ||
|
||
```bash | ||
# Install Composer | ||
curl -sS https://getcomposer.org/installer | php | ||
``` | ||
|
||
Next, run the Composer command to install the latest stable version: | ||
|
||
```bash | ||
composer require kreait/firebase-php | ||
``` | ||
|
||
After installing, you need to require Composer's autoloader: | ||
|
||
```php | ||
require 'vendor/autoload.php'; | ||
``` | ||
|
||
### Usage | ||
|
||
```php | ||
use Kreait\Firebase\Firebase; | ||
use Kreait\Firebase\Reference; | ||
|
||
$firebase = new Firebase('https://my-application-1234.firebaseio.com'); | ||
|
||
$allData = $firebase->get(); | ||
|
||
for ($i = 1; $i <= 5; $i++) { | ||
$firebase->set() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "kreait/firebase-php", | ||
"description": "Firebase REST API client", | ||
"keywords": ["firebase", "rest", "api", "http"], | ||
"homepage": "https://github.com/kreait/firebase-php", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Jérôme Gamez", | ||
"homepage": "https://github.com/jeromegamez", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.4", | ||
"ext-curl": "*", | ||
"egeloen/http-adapter": "~0.5@dev", | ||
"psr/log": "~1.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~4.4", | ||
"phpspec/prophecy-phpunit": "~1.0", | ||
"monolog/monolog": "~1.12", | ||
"guzzlehttp/guzzle": "~5.1" | ||
}, | ||
"autoload": { | ||
"psr-4": { "Kreait\\Firebase\\": "src/" } | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { "Kreait\\Firebase\\": "tests/" } | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "1.x-dev" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.4/phpunit.xsd" | ||
bootstrap="tests/bootstrap.php" | ||
colors="true"> | ||
<testsuites> | ||
<testsuite> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">.</directory> | ||
<exclude> | ||
<directory>./tests</directory> | ||
<directory>./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php | ||
/** | ||
* This file is part of the firebase-php package. | ||
* | ||
* For the full copyright and license information, please read the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Kreait\Firebase; | ||
|
||
use Ivory\HttpAdapter\CurlHttpAdapter; | ||
use Ivory\HttpAdapter\HttpAdapterException; | ||
use Ivory\HttpAdapter\HttpAdapterInterface; | ||
use Ivory\HttpAdapter\Message\RequestInterface; | ||
|
||
class Firebase implements FirebaseInterface | ||
{ | ||
use \Psr\Log\LoggerAwareTrait; | ||
|
||
/** | ||
* The HTTP adapter. | ||
* | ||
* @var HttpAdapterInterface | ||
*/ | ||
private $http; | ||
|
||
/** | ||
* Firebase client initialization. | ||
* | ||
* @param string $baseUrl The Firebase app base URL. | ||
* @param HttpAdapterInterface $http The HTTP adapter. | ||
* @throws FirebaseException When the base URL is not valid. | ||
*/ | ||
public function __construct($baseUrl, HttpAdapterInterface $http = null) | ||
{ | ||
$this->logger = new \Psr\Log\NullLogger(); | ||
$this->http = $http ?: new CurlHttpAdapter(); | ||
|
||
$configuration = $this->http->getConfiguration(); | ||
$configuration->setBaseUrl(Utils::normalizeBaseUrl($baseUrl)); | ||
$configuration->setKeepAlive(true); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get($location = null) | ||
{ | ||
return $this->send($location, RequestInterface::METHOD_GET); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function set($data, $location) | ||
{ | ||
return $this->send($location, RequestInterface::METHOD_PUT, $data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function push($data, $location) | ||
{ | ||
return $this->send($location, RequestInterface::METHOD_POST, $data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function update($data, $location) | ||
{ | ||
return $this->send($location, RequestInterface::METHOD_PATCH, $data); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function delete($location) | ||
{ | ||
return $this->send($location, RequestInterface::METHOD_DELETE); | ||
} | ||
|
||
/** | ||
* Sends the request and returns the processed response data. | ||
* | ||
* @param string $location The location. | ||
* @param string $method The HTTP method. | ||
* @param array|object|null $data The data. | ||
* | ||
* @throws FirebaseException | ||
* | ||
* @return array|string|void The processed response data. | ||
*/ | ||
private function send($location, $method, $data = null) | ||
{ | ||
$location = (string) $location; // In case it is null | ||
|
||
if ($data && !is_array($data) && !is_object($data)) { | ||
throw new \InvalidArgumentException(sprintf('array or object expected, %s given'), gettype($data)); | ||
} | ||
|
||
$relativeUrl = sprintf('/%s.json', Utils::normalizeLocation($location)); | ||
|
||
$headers = [ | ||
'accept' => 'application/json', | ||
'accept-charset' => 'utf-8', | ||
]; | ||
|
||
$this->logger->debug(sprintf('%s request to %s', $method, $relativeUrl), ['data_sent' => $data]); | ||
$request = $this->http->getConfiguration()->getMessageFactory()->createRequest( | ||
$relativeUrl, | ||
$method, | ||
RequestInterface::PROTOCOL_VERSION_1_1, | ||
$headers, | ||
json_encode($data) | ||
); | ||
|
||
try { | ||
$response = $this->http->sendRequest($request); | ||
} catch (HttpAdapterException $e) { | ||
$response = $e->hasResponse() ? $e->getResponse() : null; | ||
throw FirebaseException::httpError($request, $response); | ||
} | ||
|
||
switch ($response->getStatusCode()) { | ||
case 400: | ||
$this->logger->warning('Invalid location or PUT/POST data'); | ||
throw FirebaseException::invalidDataOrLocation($request, $response); | ||
case 403: | ||
$this->logger->warning('Forbidden'); | ||
throw FirebaseException::forbiddenAction($request, $response); | ||
case 404: | ||
$this->logger->warning('Request made of HTTP instead of HTTPS'); | ||
throw FirebaseException::requestMadeOverHttpsInsteadOfHttp($request, $response); | ||
case 417: | ||
$this->logger->warning('No namespace specified'); | ||
throw FirebaseException::noNameSpaceSpecified($request, $response); | ||
} | ||
|
||
if (!$response->hasBody()) { | ||
$this->logger->debug( | ||
sprintf('Received valid, empty response from %s request to %s', $method, $relativeUrl) | ||
); | ||
|
||
return; | ||
} | ||
|
||
$contents = $response->getBody()->getContents(); | ||
$this->logger->debug( | ||
sprintf('Received valid response from %s request to %s', $method, $relativeUrl), | ||
['data_received' => $contents] | ||
); | ||
|
||
return json_decode($contents, true); | ||
} | ||
} |
Oops, something went wrong.