Skip to content

Commit

Permalink
Added database URL option, added RealtimeDb.rules
Browse files Browse the repository at this point in the history
  • Loading branch information
plokko committed Nov 26, 2023
1 parent 656224b commit 9d72a47
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 33 deletions.
77 changes: 51 additions & 26 deletions src/IO/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,101 @@
use GuzzleHttp\Client;
use Plokko\Firebase\ServiceAccount;

class Database{
class Database
{
/**@var \Plokko\Firebase\ServiceAccount **/
private $serviceAccount;
/**@var string **/
private $baseUrl;

/**@var \GuzzleHttp\ClientInterface **/
private $client;
private $debug=false;
private $debug = false;

/**
* @param ServiceAccount $serviceAccount Service account
* @param string $databaseUrl specify database URL, if not set https://<project_id>.firebaseio.com/ will be used
*/
function __construct(ServiceAccount $serviceAccount, $databaseUrl=null)
function __construct(ServiceAccount $serviceAccount, $databaseUrl = null)
{
$this->serviceAccount = $serviceAccount;
$this->baseUrl = $databaseUrl?: 'https://'.$serviceAccount->getProjectId().'.firebaseio.com/';
$this->baseUrl = $databaseUrl !== null ? $databaseUrl . '/' : ('https://' . $serviceAccount->getProjectId() . '.firebaseio.com/');
}

/**
* @param $path
* Return the reference of a path
* @param string $path
* @return Reference
*/
function getReference($path){
return new Reference($this,$path);
function getReference($path)
{
return new Reference($this, $path);
}

function __get($path){
function __get($path)
{
return $this->getReference($path);
}
function __set($path,$value){
function __set($path, $value)
{

}

/**
* Get the http client instance
* @return \GuzzleHttp\ClientInterface
*/
private function getClient(){
if(!$this->client){
$this->client = $this->serviceAccount->authorize(['https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/firebase.database'], new Client(['debug'=>$this->debug]));
private function getClient(): \GuzzleHttp\ClientInterface
{
if (!$this->client) {
$this->client = $this->serviceAccount->authorize(['https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/firebase.database'], new Client(['debug' => $this->debug]));
}
return $this->client;
}

private function getUriPath($path){
return $this->baseUrl.trim($path,'/').'.json';
private function getUriPath($path)
{
return $this->baseUrl . trim($path, '/') . '.json';
}

function get($path){
$response = $this->getClient()->request('GET',$this->getUriPath($path));
return json_decode($response->getBody(),true);
function get($path)
{
$response = $this->getClient()->request('GET', $this->getUriPath($path));
return json_decode($response->getBody(), true);
}

function set($path,$value){
$this->getClient()->request('PUT',$this->getUriPath($path),['json'=>$value]);
function set($path, $value)
{
$this->getClient()->request('PUT', $this->getUriPath($path), ['json' => $value]);
}

function update($path,$value){
$this->getClient()->request('PATCH',$this->getUriPath($path),['json'=>$value]);
function update($path, $value)
{
$this->getClient()->request('PATCH', $this->getUriPath($path), ['json' => $value]);
}

function delete($path){
$this->getClient()->request('DELETE',$this->getUriPath($path),[]);
function delete($path)
{
$this->getClient()->request('DELETE', $this->getUriPath($path), []);
}


function setDebug($debug=false){
$this->debug=$debug;
$this->client=null;
function setDebug($debug = false)
{
$this->debug = $debug;
$this->client = null;
return $this;
}

function getRules(): DatabaseRules
{
$response = $this->getClient()->request('GET', $this->getUriPath('.settings/rules'));
$data = json_decode($response->getBody(), true);
return new DatabaseRules($this, $data);
}
function setRules(array $rules): void
{
$this->getClient()->request('PUT', $this->getUriPath('.settings/rules'), ['body' => json_encode($rules, JSON_PRETTY_PRINT)]);
}

}
89 changes: 89 additions & 0 deletions src/IO/DatabaseRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
namespace Plokko\Firebase\IO;

use ArrayAccess;
use GuzzleHttp\Client;
use JsonSerializable;
use Plokko\Firebase\ServiceAccount;

class DatabaseRules implements JsonSerializable, ArrayAccess
{
private Database $database;
private array $rules;

function __construct(Database $database, array $rules)
{
$this->database = $database;
$this->rules = $rules;
}

/**
* Save current rules to Firebase
*/
public function saveRules(): void
{
$this->database->setRules($this->toArray());
}

/**
* Replace rules with new ones
* @param array $newRules New rules to apply
*/
public function setRules(array $newRules): void
{
$this->rules = $newRules;
}

/**
* Get current rules as an Array
* @return array
*/
function getRules(): array
{
return $this->rules;
}

/**
* Get current rules as an Array
* @return array
*/
function toArray(): array
{
return $this->rules;
}

function jsonSerialize(): mixed
{
return $this->rules;
}


function &__get($path)
{
return $this->rules[$path];
}

function __set($path, $value)
{
$this->rules[$path] = $value;
}

function offsetExists($offset): bool
{
return isset($this->rules[$offset]);
}

function offsetGet($offset): mixed
{
return $this->rules[$offset];
}

function offsetSet($offset, $value): void
{
$this->rules[$offset] = $value;
}
function offsetUnset($offset): void
{
unset($this->rules[$offset]);
}
}
14 changes: 7 additions & 7 deletions src/IO/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
class Reference implements JsonSerializable
{
private
$db,
$path,
$db,
$path,

$data = null;
$data = null;

function __construct(Database $db, $path)
{
Expand All @@ -23,7 +23,7 @@ function __construct(Database $db, $path)
* @param $path string optional sub path to concatenate
* @return string
*/
function getPath($path = '')
function getPath($path = ''): string
{
return $this->path . '/' . trim($path, '/');
}
Expand All @@ -45,7 +45,7 @@ public function fetch()
* @param $path
* @return Reference
*/
function getReference($path)
function getReference($path): Reference
{
return new Reference($this->db, $this->path . '/' . $path);
}
Expand All @@ -65,7 +65,7 @@ function update($path, $value)
$this->db->update($this->getPath($path), $value);
}

function delete($path = '')
function delete($path = ''): void
{
$this->db->delete($this->getPath($path));
}
Expand All @@ -83,7 +83,7 @@ function __set($path, $value)
}


function toArray()
function toArray(): array
{
return $this->getData();
}
Expand Down

0 comments on commit 9d72a47

Please sign in to comment.